2014-11-20 23:39:29 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
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
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
QUnit.test( 'unview()', 4, function ( assert ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var logger = { log: $.noop },
|
2016-07-18 13:49:27 +00:00
|
|
|
viewLogger = new mw.mmv.logging.ViewLogger( { recordVirtualViewBeaconURI: $.noop }, {}, logger );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
this.sandbox.stub( logger, 'log' );
|
|
|
|
|
|
|
|
viewLogger.unview();
|
|
|
|
|
|
|
|
assert.ok( !logger.log.called, 'action logger not called' );
|
|
|
|
|
|
|
|
viewLogger.setLastViewLogged( false );
|
|
|
|
viewLogger.unview();
|
|
|
|
|
|
|
|
assert.ok( !logger.log.called, 'action logger not called' );
|
|
|
|
|
|
|
|
viewLogger.setLastViewLogged( true );
|
|
|
|
viewLogger.unview();
|
|
|
|
|
|
|
|
assert.ok( logger.log.calledOnce, 'action logger called' );
|
|
|
|
|
|
|
|
viewLogger.unview();
|
|
|
|
|
|
|
|
assert.ok( logger.log.calledOnce, 'action logger not called again' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'focus and blur', 1, function ( assert ) {
|
|
|
|
var fakeWindow = $( '<div>' ),
|
2016-07-18 13:49:27 +00:00
|
|
|
viewLogger = new mw.mmv.logging.ViewLogger( { recordVirtualViewBeaconURI: $.noop }, fakeWindow, { log: $.noop } );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
this.clock.tick( 1 ); // This is just so that $.now() > 0 in the fake timer environment
|
|
|
|
|
|
|
|
viewLogger.attach();
|
|
|
|
|
|
|
|
this.clock.tick( 5 );
|
|
|
|
|
|
|
|
fakeWindow.triggerHandler( 'blur' );
|
|
|
|
|
|
|
|
this.clock.tick( 2 );
|
|
|
|
|
|
|
|
fakeWindow.triggerHandler( 'focus' );
|
|
|
|
|
|
|
|
this.clock.tick( 3 );
|
|
|
|
|
|
|
|
fakeWindow.triggerHandler( 'blur' );
|
|
|
|
|
|
|
|
this.clock.tick( 4 );
|
|
|
|
|
|
|
|
assert.strictEqual( viewLogger.viewDuration, 8, 'Only focus duration was logged' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'stopViewDuration before startViewDuration', 1, function ( assert ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
var viewLogger = new mw.mmv.logging.ViewLogger( { recordVirtualViewBeaconURI: $.noop }, {}, { log: $.noop } );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
|
|
|
this.clock.tick( 1 ); // This is just so that $.now() > 0 in the fake timer environment
|
|
|
|
|
|
|
|
viewLogger.stopViewDuration();
|
|
|
|
|
|
|
|
this.clock.tick( 2 );
|
|
|
|
|
|
|
|
viewLogger.startViewDuration();
|
|
|
|
|
|
|
|
this.clock.tick( 3 );
|
|
|
|
|
|
|
|
viewLogger.stopViewDuration();
|
|
|
|
|
|
|
|
assert.strictEqual( viewLogger.viewDuration, 3, 'Only last timeframe was logged' );
|
|
|
|
} );
|
|
|
|
}( mediaWiki, jQuery ) );
|