2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-11-20 23:39:29 +00:00
|
|
|
QUnit.module( 'mmv.logging.ViewLogger', QUnit.newMwEnvironment( {
|
|
|
|
setup: function () {
|
|
|
|
this.clock = this.sandbox.useFakeTimers();
|
2017-05-15 13:01:23 +00:00
|
|
|
|
|
|
|
// since jQuery 2/3, $.now will capture a reference to Date.now
|
|
|
|
// before above fake timer gets a chance to override it, so I'll
|
|
|
|
// override that new behavior in order to run these tests...
|
|
|
|
// @see https://github.com/sinonjs/lolex/issues/76
|
|
|
|
this.oldNow = $.now;
|
2017-05-04 20:48:57 +00:00
|
|
|
$.now = function () { return +( new Date() ); };
|
2017-05-15 13:01:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
teardown: function () {
|
|
|
|
$.now = this.oldNow;
|
|
|
|
this.clock.restore();
|
2014-11-20 23:39:29 +00:00
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'unview()', function ( assert ) {
|
2019-02-05 23:59:52 +00:00
|
|
|
var logger = { log: function () {} },
|
|
|
|
viewLogger = new mw.mmv.logging.ViewLogger( { recordVirtualViewBeaconURI: function () {} }, {}, logger );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
this.sandbox.stub( logger, 'log' );
|
|
|
|
|
|
|
|
viewLogger.unview();
|
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( logger.log.called, false, 'action logger not called' );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
viewLogger.setLastViewLogged( false );
|
|
|
|
viewLogger.unview();
|
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( logger.log.called, false, 'action logger not called' );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
viewLogger.setLastViewLogged( true );
|
|
|
|
viewLogger.unview();
|
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( logger.log.calledOnce, true, 'action logger called' );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
viewLogger.unview();
|
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( logger.log.calledOnce, true, 'action logger not called again' );
|
2014-11-20 23:39:29 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'focus and blur', function ( assert ) {
|
2020-01-08 19:27:58 +00:00
|
|
|
var $fakeWindow = $( '<div>' ),
|
|
|
|
viewLogger = new mw.mmv.logging.ViewLogger( { recordVirtualViewBeaconURI: function () {} }, $fakeWindow, { log: function () {} } );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
2019-02-05 23:59:52 +00:00
|
|
|
this.clock.tick( 1 ); // This is just so that the timer ticks up in the fake timer environment
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
viewLogger.attach();
|
|
|
|
|
|
|
|
this.clock.tick( 5 );
|
|
|
|
|
2020-01-08 19:27:58 +00:00
|
|
|
$fakeWindow.triggerHandler( 'blur' );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
this.clock.tick( 2 );
|
|
|
|
|
2020-01-08 19:27:58 +00:00
|
|
|
$fakeWindow.triggerHandler( 'focus' );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
this.clock.tick( 3 );
|
|
|
|
|
2020-01-08 19:27:58 +00:00
|
|
|
$fakeWindow.triggerHandler( 'blur' );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
this.clock.tick( 4 );
|
|
|
|
|
|
|
|
assert.strictEqual( viewLogger.viewDuration, 8, 'Only focus duration was logged' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'stopViewDuration before startViewDuration', function ( assert ) {
|
2019-02-05 23:59:52 +00:00
|
|
|
var viewLogger = new mw.mmv.logging.ViewLogger( { recordVirtualViewBeaconURI: function () {} }, {}, { log: function () {} } );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
2019-02-05 23:59:52 +00:00
|
|
|
this.clock.tick( 1 ); // This is just so that the timer ticks up in the fake timer environment
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
viewLogger.stopViewDuration();
|
|
|
|
|
|
|
|
this.clock.tick( 2 );
|
|
|
|
|
|
|
|
viewLogger.startViewDuration();
|
|
|
|
|
|
|
|
this.clock.tick( 3 );
|
|
|
|
|
|
|
|
viewLogger.stopViewDuration();
|
|
|
|
|
|
|
|
assert.strictEqual( viewLogger.viewDuration, 3, 'Only last timeframe was logged' );
|
|
|
|
} );
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|