Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/logging/mmv.logging.ViewLogger.test.js

raw symbols SCIP
1 const { ViewLogger } = require( 'mmv' );
2
3 ( function () {
4 QUnit.module( 'mmv.logging.ViewLogger', QUnit.newMwEnvironment( {
5 beforeEach: function () {
6 this.clock = this.sandbox.useFakeTimers();
7
8 // since jQuery 2/3, $.now will capture a reference to Date.now
9 // before above fake timer gets a chance to override it, so I'll
10 // override that new behavior in order to run these tests...
11 // @see https://github.com/sinonjs/lolex/issues/76
12 this.oldNow = $.now;
13 $.now = () => Date.now();
14 },
15
16 afterEach: function () {
17 $.now = this.oldNow;
18 this.clock.restore();
19 }
20 } ) );
21
22 QUnit.test( 'focus and blur', function ( assert ) {
23 const $fakeWindow = $( '<div>' );
24 const viewLogger = new ViewLogger( $fakeWindow );
25
26 this.clock.tick( 1 ); // This is just so that the timer ticks up in the fake timer environment
27
28 viewLogger.attach();
29
30 this.clock.tick( 5 );
31
32 $fakeWindow.triggerHandler( 'blur' );
33
34 this.clock.tick( 2 );
35
36 $fakeWindow.triggerHandler( 'focus' );
37
38 this.clock.tick( 3 );
39
40 $fakeWindow.triggerHandler( 'blur' );
41
42 this.clock.tick( 4 );
43
44 assert.strictEqual( viewLogger.viewDuration, 8, 'Only focus duration was logged' );
45 } );
46
47 QUnit.test( 'stopViewDuration before startViewDuration', function ( assert ) {
48 const viewLogger = new ViewLogger( {} );
49
50 this.clock.tick( 1 ); // This is just so that the timer ticks up in the fake timer environment
51
52 viewLogger.stopViewDuration();
53
54 this.clock.tick( 2 );
55
56 viewLogger.startViewDuration();
57
58 this.clock.tick( 3 );
59
60 viewLogger.stopViewDuration();
61
62 assert.strictEqual( viewLogger.viewDuration, 3, 'Only last timeframe was logged' );
63 } );
64 }() );