mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-12-04 20:18:23 +00:00
57762e0417
Bug: T165036 Change-Id: I17d374eaac6627ca6a8ba178862b2a9cff2538c0
55 lines
970 B
JavaScript
55 lines
970 B
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 );
|
|
|
|
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'
|
|
}
|
|
) );
|
|
} );
|