mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-12-26 22:12:54 +00:00
883c8c1aca
Change-Id: I055517998ed06fccdf50ec31251ea6aa9040abb5
62 lines
1.2 KiB
JavaScript
62 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 );
|
|
|
|
stubStore.setState( {} );
|
|
|
|
if ( !boundChangeListener ) {
|
|
assert.true( false, 'The change listener was not bound.' );
|
|
}
|
|
boundChangeListener();
|
|
boundChangeListener();
|
|
|
|
assert.strictEqual( spy.callCount, 1, 'The spy was called once.' );
|
|
assert.true(
|
|
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.true(
|
|
spy.calledWith(
|
|
{},
|
|
{
|
|
foo: 'bar'
|
|
}
|
|
),
|
|
'The spy was called with the correct arguments.'
|
|
);
|
|
} );
|