Skip to content

Commit

Permalink
Update selector to return static menu data if state is not populated …
Browse files Browse the repository at this point in the history
…from API
  • Loading branch information
getdave committed Sep 22, 2020
1 parent c789a35 commit 3fef9ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion client/state/admin-menu/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Internal dependencies
*/
import 'state/inline-help/init';
import fallbackResponse from '../fallback-data.json';

export function getAdminMenu( state, siteId ) {
const stateSlice = state?.adminMenu;
Expand All @@ -10,5 +11,12 @@ export function getAdminMenu( state, siteId ) {
return null;
}

return state.adminMenu[ siteId ] || null;
/**
* To ensure that a menu is always available in the UI even
* if the network fails on an uncached request we provide a
* set of static fallback data to render a basic menu. This
* avoids a situation where the user might be left with an
* empty menu.
*/
return state.adminMenu[ siteId ] || fallbackResponse;
}
8 changes: 4 additions & 4 deletions client/state/admin-menu/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import deepFreeze from 'deep-freeze';

import fallbackResponse from '../fallback-data.json';
/**
* Internal dependencies
*/
Expand All @@ -19,20 +19,20 @@ describe( 'selectors', () => {
expect( getAdminMenu( state, 123456 ) ).toEqual( null );
} );

test( 'returns null when siteId is not provided', () => {
test( 'returns null data when siteId is not provided', () => {
const state = {};

expect( getAdminMenu( state ) ).toEqual( null );
} );

test( 'returns null when requested siteId key is not present', () => {
test( 'returns fallback data when requested siteId key is not present', () => {
const state = {
adminMenu: {
56789: frozenFixture,
},
};

expect( getAdminMenu( state, 12345 ) ).toEqual( null );
expect( getAdminMenu( state, 12345 ) ).toEqual( fallbackResponse );
} );

test( 'returns menu data when siteId is present', () => {
Expand Down

0 comments on commit 3fef9ca

Please sign in to comment.