2017-07-28 17:32:46 +00:00
|
|
|
import registerChangeListener from '../../src/changeListener';
|
2017-02-15 15:22:50 +00:00
|
|
|
|
2017-07-28 17:32:46 +00:00
|
|
|
var stubStore = ( function () {
|
2017-02-15 15:22:50 +00:00
|
|
|
var state;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}() );
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/changeListener' );
|
|
|
|
|
|
|
|
QUnit.test( 'it should only call the callback when the state has changed', function ( assert ) {
|
|
|
|
var spy = this.sandbox.spy(),
|
|
|
|
boundChangeListener;
|
|
|
|
|
|
|
|
stubStore.subscribe = function ( changeListener ) {
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
) );
|
|
|
|
} );
|