2017-07-28 17:32:46 +00:00
|
|
|
import registerChangeListener from '../../src/changeListener';
|
2017-02-15 15:22:50 +00:00
|
|
|
|
2018-03-19 19:39:41 +00:00
|
|
|
const stubStore = ( () => {
|
2018-03-15 16:30:11 +00:00
|
|
|
let state;
|
2017-02-15 15:22:50 +00:00
|
|
|
|
|
|
|
return {
|
2018-03-14 22:04:59 +00:00
|
|
|
getState() {
|
2017-02-15 15:22:50 +00:00
|
|
|
return state;
|
|
|
|
},
|
2018-03-14 22:04:59 +00:00
|
|
|
setState( value ) {
|
2017-02-15 15:22:50 +00:00
|
|
|
state = value;
|
|
|
|
}
|
|
|
|
};
|
2018-03-14 23:50:09 +00:00
|
|
|
} )();
|
2017-02-15 15:22:50 +00:00
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/changeListener' );
|
|
|
|
|
|
|
|
QUnit.test( 'it should only call the callback when the state has changed', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const spy = this.sandbox.spy();
|
|
|
|
let boundChangeListener;
|
2017-02-15 15:22:50 +00:00
|
|
|
|
2018-03-14 23:50:09 +00:00
|
|
|
stubStore.subscribe = ( changeListener ) => {
|
2017-02-15 15:22:50 +00:00
|
|
|
boundChangeListener = changeListener;
|
|
|
|
};
|
|
|
|
|
|
|
|
registerChangeListener( stubStore, spy );
|
|
|
|
|
|
|
|
stubStore.setState( {} );
|
|
|
|
|
2018-05-08 19:48:17 +00:00
|
|
|
if ( !boundChangeListener ) {
|
2022-02-09 15:09:19 +00:00
|
|
|
assert.true( false, 'The change listener was not bound.' );
|
2018-05-08 19:48:17 +00:00
|
|
|
}
|
2017-02-15 15:22:50 +00:00
|
|
|
boundChangeListener();
|
|
|
|
boundChangeListener();
|
|
|
|
|
2018-05-08 19:48:17 +00:00
|
|
|
assert.strictEqual( spy.callCount, 1, 'The spy was called once.' );
|
2022-02-09 15:09:19 +00:00
|
|
|
assert.true(
|
2018-05-08 19:48:17 +00:00
|
|
|
spy.calledWith(
|
|
|
|
undefined, // The initial internal state of the change listener.
|
|
|
|
{}
|
|
|
|
),
|
|
|
|
'The spy was called with the correct arguments.'
|
|
|
|
);
|
2017-02-15 15:22:50 +00:00
|
|
|
|
|
|
|
stubStore.setState( {
|
|
|
|
foo: 'bar'
|
|
|
|
} );
|
|
|
|
|
|
|
|
boundChangeListener();
|
|
|
|
|
2018-05-08 19:48:17 +00:00
|
|
|
assert.strictEqual( spy.callCount, 2, 'The spy was called twice.' );
|
2022-02-09 15:09:19 +00:00
|
|
|
assert.true(
|
2018-05-08 19:48:17 +00:00
|
|
|
spy.calledWith(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
foo: 'bar'
|
|
|
|
}
|
|
|
|
),
|
|
|
|
'The spy was called with the correct arguments.'
|
|
|
|
);
|
2017-02-15 15:22:50 +00:00
|
|
|
} );
|