2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-04-07 19:19:07 +00:00
|
|
|
QUnit.module( 'mmv.ui', QUnit.newMwEnvironment( {
|
|
|
|
setup: function () {
|
|
|
|
this.clock = this.sandbox.useFakeTimers();
|
|
|
|
}
|
|
|
|
} ) );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'handleEvent()', function ( assert ) {
|
2014-03-26 23:59:04 +00:00
|
|
|
var element = new mw.mmv.ui.Element( $( '<div>' ) );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-03-26 23:59:04 +00:00
|
|
|
element.handleEvent( 'mmv-foo', function () {
|
|
|
|
assert.ok( true, 'Event is handled' );
|
|
|
|
} );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-03-26 23:59:04 +00:00
|
|
|
$( document ).trigger( new $.Event( 'mmv-foo' ) );
|
|
|
|
|
|
|
|
element.clearEvents();
|
|
|
|
|
|
|
|
$( document ).trigger( new $.Event( 'mmv-foo' ) );
|
2014-02-17 15:09:23 +00:00
|
|
|
} );
|
2014-04-02 00:07:51 +00:00
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'setInlineStyle()', function ( assert ) {
|
2014-04-02 00:07:51 +00:00
|
|
|
var element = new mw.mmv.ui.Element( $( '<div>' ) ),
|
2019-02-05 23:59:52 +00:00
|
|
|
$testDiv = $( '<div>' ).attr( 'id', 'mmv-testdiv' ).text( '!!!' ).appendTo( '#qunit-fixture' );
|
2014-04-02 00:07:51 +00:00
|
|
|
|
|
|
|
assert.ok( $testDiv.is( ':visible' ), 'Test div is visible' );
|
|
|
|
|
2014-04-04 22:02:16 +00:00
|
|
|
element.setInlineStyle( 'test', '#mmv-testdiv { display: none; }' );
|
2014-04-02 00:07:51 +00:00
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( $testDiv.is( ':visible' ), false, 'Test div is hidden by inline style' );
|
2014-04-02 00:07:51 +00:00
|
|
|
|
2014-04-04 22:02:16 +00:00
|
|
|
element.setInlineStyle( 'test', null );
|
2014-04-02 00:07:51 +00:00
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( $testDiv.is( ':visible' ), true, 'Test div is visible again' );
|
2014-04-02 00:07:51 +00:00
|
|
|
} );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'setTimer()/clearTimer()/resetTimer()', function ( assert ) {
|
2014-04-07 19:19:07 +00:00
|
|
|
var element = new mw.mmv.ui.Element( $( '<div>' ) ),
|
|
|
|
element2 = new mw.mmv.ui.Element( $( '<div>' ) ),
|
|
|
|
spy = this.sandbox.spy(),
|
|
|
|
spy2 = this.sandbox.spy();
|
|
|
|
|
|
|
|
element.setTimer( 'foo', spy, 10 );
|
|
|
|
this.clock.tick( 100 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called, true, 'Timeout callback was called' );
|
|
|
|
assert.strictEqual( spy.calledOnce, true, 'Timeout callback was called once' );
|
|
|
|
assert.strictEqual( spy.calledOn( element ), true, 'Timeout callback was called on the element' );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
|
|
|
spy = this.sandbox.spy();
|
|
|
|
element.setTimer( 'foo', spy, 10 );
|
|
|
|
element.setTimer( 'foo', spy2, 20 );
|
|
|
|
this.clock.tick( 100 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called, false, 'Old timeout callback was not called after update' );
|
|
|
|
assert.strictEqual( spy2.called, true, 'New timeout callback was called after update' );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
|
|
|
spy = this.sandbox.spy();
|
|
|
|
spy2 = this.sandbox.spy();
|
|
|
|
element.setTimer( 'foo', spy, 10 );
|
|
|
|
element.setTimer( 'bar', spy2, 20 );
|
|
|
|
this.clock.tick( 100 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called && spy2.called, true, 'Timeouts with different names do not conflict' );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
|
|
|
spy = this.sandbox.spy();
|
|
|
|
spy2 = this.sandbox.spy();
|
|
|
|
element.setTimer( 'foo', spy, 10 );
|
|
|
|
element2.setTimer( 'foo', spy2, 20 );
|
|
|
|
this.clock.tick( 100 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called && spy2.called, true, 'Timeouts in different elements do not conflict' );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
|
|
|
spy = this.sandbox.spy();
|
|
|
|
element.setTimer( 'foo', spy, 10 );
|
|
|
|
element.clearTimer( 'foo' );
|
|
|
|
this.clock.tick( 100 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called, false, 'Timeout is invalidated by clearing' );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
|
|
|
spy = this.sandbox.spy();
|
|
|
|
element.setTimer( 'foo', spy, 100 );
|
|
|
|
this.clock.tick( 80 );
|
|
|
|
element.resetTimer( 'foo' );
|
|
|
|
this.clock.tick( 80 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called, false, 'Timeout is reset' );
|
2014-04-07 19:19:07 +00:00
|
|
|
this.clock.tick( 80 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called, true, 'Timeout works after reset' );
|
2014-04-07 19:19:07 +00:00
|
|
|
|
|
|
|
spy = this.sandbox.spy();
|
|
|
|
element.setTimer( 'foo', spy, 100 );
|
|
|
|
this.clock.tick( 80 );
|
|
|
|
element.resetTimer( 'foo', 200 );
|
|
|
|
this.clock.tick( 180 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called, false, 'Timeout is reset to the designated delay' );
|
2014-04-07 19:19:07 +00:00
|
|
|
this.clock.tick( 80 );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( spy.called, true, 'Timeout works after changing the delay' );
|
2014-04-07 19:19:07 +00:00
|
|
|
} );
|
2014-10-03 09:15:28 +00:00
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'correctEW()', function ( assert ) {
|
2014-10-03 09:15:28 +00:00
|
|
|
var element = new mw.mmv.ui.Element( $( '<div>' ) );
|
|
|
|
|
|
|
|
element.isRTL = this.sandbox.stub().returns( true );
|
|
|
|
|
|
|
|
assert.strictEqual( element.correctEW( 'e' ), 'w', 'e (east) is flipped' );
|
|
|
|
assert.strictEqual( element.correctEW( 'ne' ), 'nw', 'ne (northeast) is flipped' );
|
|
|
|
assert.strictEqual( element.correctEW( 'W' ), 'E', 'uppercase is flipped' );
|
|
|
|
assert.strictEqual( element.correctEW( 's' ), 's', 'non-horizontal directions are ignored' );
|
|
|
|
|
|
|
|
element.isRTL.returns( false );
|
|
|
|
|
|
|
|
assert.strictEqual( element.correctEW( 'e' ), 'e', 'no flipping in LTR documents' );
|
|
|
|
} );
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|