mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-15 11:46:55 +00:00
ae44042cbf
Change-Id: Ic0a47bd468532824e8648c3f6371cc403896603c
64 lines
1.2 KiB
JavaScript
64 lines
1.2 KiB
JavaScript
import registerChangeListener from '../../src/changeListener';
|
|
|
|
const stubStore = ( () => {
|
|
let state;
|
|
|
|
return {
|
|
getState() {
|
|
return state;
|
|
},
|
|
setState( value ) {
|
|
state = value;
|
|
}
|
|
};
|
|
} )();
|
|
|
|
QUnit.module( 'ext.popups/changeListener' );
|
|
|
|
QUnit.test( 'it should only call the callback when the state has changed', function ( assert ) {
|
|
const spy = this.sandbox.spy();
|
|
let boundChangeListener;
|
|
|
|
stubStore.subscribe = ( changeListener ) => {
|
|
boundChangeListener = changeListener;
|
|
};
|
|
|
|
registerChangeListener( stubStore, spy );
|
|
|
|
assert.expect( 4, 'All assertions are executed.' );
|
|
|
|
stubStore.setState( {} );
|
|
|
|
if ( !boundChangeListener ) {
|
|
assert.fail( 'The change listener was not bound.' );
|
|
}
|
|
boundChangeListener();
|
|
boundChangeListener();
|
|
|
|
assert.strictEqual( spy.callCount, 1, 'The spy was called once.' );
|
|
assert.ok(
|
|
spy.calledWith(
|
|
undefined, // The initial internal state of the change listener.
|
|
{}
|
|
),
|
|
'The spy was called with the correct arguments.'
|
|
);
|
|
|
|
stubStore.setState( {
|
|
foo: 'bar'
|
|
} );
|
|
|
|
boundChangeListener();
|
|
|
|
assert.strictEqual( spy.callCount, 2, 'The spy was called twice.' );
|
|
assert.ok(
|
|
spy.calledWith(
|
|
{},
|
|
{
|
|
foo: 'bar'
|
|
}
|
|
),
|
|
'The spy was called with the correct arguments.'
|
|
);
|
|
} );
|