mediawiki-extensions-Popups/tests/node-qunit/reducers/nextState.test.js
Thiemo Kreuz 51dd4b2807 Make nextState support non-flat updates
The nextState() function was not able to understand updates that
are deeper than a single level. Example:

nextState( state, { pagePreviews: { enabled: true } } )

Before, this would replace whatever was in the "pagePreviews"
property with { enabled: true }, but not merge it. In some places
a single level of recursion was done manually because of this.
This can be removed now.

This is done in preparation for splitting the "enabled" flag into
separate ones for each popup type.

Bug: T277639
Change-Id: I35911c18018ba7cd1633a4c882b978656c3fee36
2021-04-12 13:46:18 +02:00

62 lines
1.4 KiB
JavaScript

import nextState from '../../../src/reducers/nextState';
QUnit.module( 'reducers/nextState' );
QUnit.test( 'with scalar values', ( assert ) => {
const tests = [
{
before: {},
updates: {},
after: {}
},
{
before: { unchanged: true, changed: false },
updates: { new: true, changed: true },
after: { unchanged: true, changed: true, new: true }
},
{
before: { a: { b: { unchanged: true, changed: false } } },
updates: { a: { b: { new: true, changed: true } } },
after: { a: { b: { unchanged: true, changed: true, new: true } } }
}
];
for ( const i in tests ) {
const test = tests[ i ];
assert.deepEqual(
nextState( test.before, test.updates ),
test.after,
'Test case #' + i
);
}
} );
QUnit.test( 'original state object should not change', ( assert ) => {
const state = {},
after = nextState( state, { changed: true } );
assert.deepEqual(
state,
{},
'original state is unchanged'
);
assert.deepEqual(
after,
{ changed: true },
'new state is different'
);
} );
QUnit.test( 'Element instances should not be cloned', ( assert ) => {
const element = document.createElement( 'a' ),
newElement = document.createElement( 'b' ),
state = { element },
after = nextState( state, { element: newElement } );
assert.ok(
state.element === element,
'original state is unchanged'
);
assert.ok(
after.element === newElement,
'Element instance was not cloned'
);
} );