Skip to content

Commit

Permalink
Data: Avoid assuming persisted preferences shape (#14692)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored and gziolo committed Mar 29, 2019
1 parent 7431011 commit ae3a09e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ export const canInsertBlockType = createSelector(
* the number of inserts that have occurred.
*/
function getInsertUsage( state, id ) {
return state.preferences.insertUsage[ id ] || null;
return get( state.preferences.insertUsage, [ id ], null );
}

/**
Expand Down
8 changes: 5 additions & 3 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1927,9 +1927,11 @@ describe( 'selectors', () => {
{ id: 1, isTemporary: false, clientId: 'block1', title: 'Reusable Block 1' },
],
},
preferences: {
insertUsage: {},
},
// Intentionally include a test case which considers
// `insertUsage` as not present within preferences.
//
// See: https://github.com/WordPress/gutenberg/issues/14580
preferences: {},
blockListSettings: {},
};
const items = getInserterItems( state );
Expand Down
7 changes: 5 additions & 2 deletions packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ export function isEditorPanelEnabled( state, panelName ) {
*/
export function isEditorPanelOpened( state, panelName ) {
const panels = getPreference( state, 'panels' );
return panels[ panelName ] === true || get( panels, [ panelName, 'opened' ], false );
return (
get( panels, [ panelName ] ) === true ||
get( panels, [ panelName, 'opened' ] ) === true
);
}

/**
Expand All @@ -163,7 +166,7 @@ export function isModalActive( state, modalName ) {
* @return {boolean} Is active.
*/
export function isFeatureActive( state, feature ) {
return !! state.preferences.features[ feature ];
return get( state.preferences.features, [ feature ], false );
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/edit-post/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ describe( 'selectors', () => {
} );

describe( 'isEditorPanelOpened', () => {
it( 'is tolerant to an undefined panels preference', () => {
// See: https://github.com/WordPress/gutenberg/issues/14580
const state = {
preferences: {},
};

expect( isEditorPanelOpened( state, 'post-status' ) ).toBe( false );
} );

it( 'should return false by default', () => {
const state = {
preferences: {
Expand Down Expand Up @@ -333,6 +342,15 @@ describe( 'selectors', () => {
} );

describe( 'isFeatureActive', () => {
it( 'is tolerant to an undefined features preference', () => {
// See: https://github.com/WordPress/gutenberg/issues/14580
const state = {
preferences: {},
};

expect( isFeatureActive( state, 'chicken' ) ).toBe( false );
} );

it( 'should return true if feature is active', () => {
const state = {
preferences: {
Expand Down
4 changes: 2 additions & 2 deletions packages/nux/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import createSelector from 'rememo';
import { includes, difference, keys } from 'lodash';
import { includes, difference, keys, has } from 'lodash';

/**
* An object containing information about a guide.
Expand Down Expand Up @@ -55,7 +55,7 @@ export function isTipVisible( state, tipId ) {
return false;
}

if ( state.preferences.dismissedTips[ tipId ] ) {
if ( has( state.preferences.dismissedTips, [ tipId ] ) ) {
return false;
}

Expand Down
20 changes: 20 additions & 0 deletions packages/nux/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ describe( 'selectors', () => {
} );

describe( 'isTipVisible', () => {
it( 'is tolerant to individual preferences being undefined', () => {
// See: https://github.com/WordPress/gutenberg/issues/14580
const state = {
guides: [],
preferences: {},
};
expect( isTipVisible( state, 'test/tip' ) ).toBe( false );
} );

it( 'is tolerant to undefined dismissedTips', () => {
// See: https://github.com/WordPress/gutenberg/issues/14580
const state = {
guides: [],
preferences: {
areTipsEnabled: true,
},
};
expect( isTipVisible( state, 'test/tip' ) ).toBe( true );
} );

it( 'should return true by default', () => {
const state = {
guides: [],
Expand Down

0 comments on commit ae3a09e

Please sign in to comment.