File size: 4,399 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import deepFreeze from 'deep-freeze';
import { ADMIN_MENU_RECEIVE, ADMIN_MENU_REQUEST } from 'calypso/state/action-types';
import { serialize, deserialize } from 'calypso/state/utils';
import { menus as menusReducer, requesting as requestingReducer } from '../reducer';
import menuFixture from './fixture/menu-fixture';
describe( 'reducer', () => {
describe( 'menus reducer', () => {
test( 'returns default state when no arguments provided', () => {
const defaultState = deepFreeze( {} );
expect( menusReducer( undefined, {} ) ).toEqual( defaultState );
} );
test( 'adds menu to state keyed by provided siteId', () => {
const action = {
type: ADMIN_MENU_RECEIVE,
siteId: 123456,
menu: menuFixture,
};
const initialState = deepFreeze( {} );
expect( menusReducer( initialState, action ) ).toEqual( {
123456: menuFixture,
} );
} );
test( 'updates menu when there already is a menu', () => {
const originalMenu = [
{
icon: 'dashicons-feedback',
slug: 'the-original-menu-item',
title: 'The Original Menu Item',
type: 'menu-item',
url: 'https://examplewebsite.wordpress.com/wp-admin/admin.php?page=original',
},
];
const newMenu = menuFixture;
// Populate initial state with a different menu from
// the one we will be replacing it with.
const initialState = deepFreeze( {
123456: originalMenu,
} );
const action = {
type: ADMIN_MENU_RECEIVE,
siteId: 123456,
menu: newMenu,
};
// Check the menu updates to reflect the new menu.
expect( menusReducer( initialState, action ) ).toEqual( {
123456: newMenu,
} );
} );
describe( 'persistence', () => {
test( 'correctly serializes state for persistence', () => {
const initialState = deepFreeze( {
123456: menuFixture,
} );
const serializationResult = serialize( menusReducer, initialState ).root();
expect( serializationResult ).toEqual( {
123456: menuFixture,
} );
} );
test( 'correctly loads valid persisted state', () => {
const stateToDeserialize = deepFreeze( {
123456: menuFixture,
} );
expect( deserialize( menusReducer, stateToDeserialize ) ).toEqual( {
123456: menuFixture,
} );
} );
test.each( [
{
12345: [
{
title: 'Menu Item',
},
],
},
{
12345: [
{
title: 'Menu Item',
type: 'menu-item',
children: [
{
title: 'Child Menu',
type: 'sub-menu-item',
},
],
},
],
},
[
{
title: 'Hello world',
},
],
] )(
'loads default state when persisted state fails schema validation',
( persistedState ) => {
// Disable console here as `isValidStateWithSchema` util emits logs.
// see: https://github.com/Automattic/wp-calypso/blob/02c3a452881ff89fce240c09d16874c0c4e4d429/client/state/utils/schema-utils.js#L14
jest.spyOn( console, 'warn' ).mockImplementation( () => {} );
// This is the state that will be returned for the `deserialize` call
// if the persisted state fails schema validation
const defaultReducerState = deepFreeze( {} );
// This is the state to be validated against the schema.
const stateToDeserialize = deepFreeze( persistedState );
const state = deserialize( menusReducer, stateToDeserialize );
expect( state ).toEqual( defaultReducerState );
}
);
} );
} );
describe( 'requesting reducer', () => {
test( 'returns default state when no action provided', () => {
expect( requestingReducer( undefined, {} ) ).toBe( false );
} );
test( 'returns true for ADMIN_MENU_REQUEST action', () => {
const initialState = deepFreeze( false );
expect(
requestingReducer( initialState, {
type: ADMIN_MENU_REQUEST,
} )
).toBe( true );
} );
test( 'resets to false for ADMIN_MENU_RECEIVE action', () => {
const initialState = deepFreeze( true );
expect(
requestingReducer( initialState, {
type: ADMIN_MENU_RECEIVE,
} )
).toBe( false );
} );
test.each( [ false, true ] )( 'ignores invalid action types', ( theInitialStateBool ) => {
const initialState = deepFreeze( theInitialStateBool );
expect(
requestingReducer( initialState, {
type: 'A_FAKE_ACTION_SHOULD_BE_IGNORED',
} )
).toBe( theInitialStateBool );
} );
} );
} );
|