2017-07-28 17:32:46 +00:00
|
|
|
import registerChangeListener from '../../src/changeListener';
|
2017-02-15 15:22:50 +00:00
|
|
|
|
2018-03-15 16:30:11 +00:00
|
|
|
let stubStore = ( () => {
|
|
|
|
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-15 16:30:11 +00:00
|
|
|
let spy = this.sandbox.spy(),
|
2017-02-15 15:22:50 +00:00
|
|
|
boundChangeListener;
|
|
|
|
|
2018-03-14 23:50:09 +00:00
|
|
|
stubStore.subscribe = ( changeListener ) => {
|
2017-02-15 15:22:50 +00:00
|
|
|
boundChangeListener = changeListener;
|
|
|
|
};
|
|
|
|
|
|
|
|
registerChangeListener( stubStore, spy );
|
|
|
|
|
|
|
|
assert.expect( 4 );
|
|
|
|
|
|
|
|
stubStore.setState( {} );
|
|
|
|
|
|
|
|
boundChangeListener();
|
|
|
|
boundChangeListener();
|
|
|
|
|
|
|
|
assert.strictEqual( spy.callCount, 1 );
|
|
|
|
assert.ok( spy.calledWith(
|
|
|
|
undefined, // The initial internal state of the change listener.
|
|
|
|
{}
|
|
|
|
) );
|
|
|
|
|
|
|
|
stubStore.setState( {
|
|
|
|
foo: 'bar'
|
|
|
|
} );
|
|
|
|
|
|
|
|
boundChangeListener();
|
|
|
|
|
|
|
|
assert.strictEqual( spy.callCount, 2 );
|
|
|
|
assert.ok( spy.calledWith(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
foo: 'bar'
|
|
|
|
}
|
|
|
|
) );
|
|
|
|
} );
|