From 92214a5e65e831759a14de2c1015431a794eb192 Mon Sep 17 00:00:00 2001 From: Marko Andrijasevic Date: Thu, 12 Mar 2020 19:59:04 +0100 Subject: [PATCH] Carry over relevant Preferenes tests from edit-post package --- packages/edit-site/src/store/test/actions.js | 16 ++++++ packages/edit-site/src/store/test/reducer.js | 32 ++++++++++++ .../edit-site/src/store/test/selectors.js | 51 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 packages/edit-site/src/store/test/actions.js create mode 100644 packages/edit-site/src/store/test/reducer.js create mode 100644 packages/edit-site/src/store/test/selectors.js diff --git a/packages/edit-site/src/store/test/actions.js b/packages/edit-site/src/store/test/actions.js new file mode 100644 index 0000000000000..c2a7d440207fb --- /dev/null +++ b/packages/edit-site/src/store/test/actions.js @@ -0,0 +1,16 @@ +/** + * Internal dependencies + */ +import { toggleFeature } from '../actions'; + +describe( 'actions', () => { + describe( 'toggleFeature', () => { + it( 'should return TOGGLE_FEATURE action', () => { + const feature = 'name'; + expect( toggleFeature( feature ) ).toEqual( { + type: 'TOGGLE_FEATURE', + feature, + } ); + } ); + } ); +} ); diff --git a/packages/edit-site/src/store/test/reducer.js b/packages/edit-site/src/store/test/reducer.js new file mode 100644 index 0000000000000..687eca3d39a1a --- /dev/null +++ b/packages/edit-site/src/store/test/reducer.js @@ -0,0 +1,32 @@ +/** + * External dependencies + */ +import deepFreeze from 'deep-freeze'; + +/** + * Internal dependencies + */ +import { preferences } from '../reducer'; +import { PREFERENCES_DEFAULTS } from '../defaults'; + +describe( 'state', () => { + describe( 'preferences()', () => { + it( 'should apply all defaults', () => { + const state = preferences( undefined, {} ); + + expect( state ).toEqual( PREFERENCES_DEFAULTS ); + } ); + + it( 'should toggle a feature flag', () => { + const state = preferences( + deepFreeze( { features: { chicken: true } } ), + { + type: 'TOGGLE_FEATURE', + feature: 'chicken', + } + ); + + expect( state.features ).toEqual( { chicken: false } ); + } ); + } ); +} ); diff --git a/packages/edit-site/src/store/test/selectors.js b/packages/edit-site/src/store/test/selectors.js new file mode 100644 index 0000000000000..82424b3658948 --- /dev/null +++ b/packages/edit-site/src/store/test/selectors.js @@ -0,0 +1,51 @@ +/** + * Internal dependencies + */ +import { isFeatureActive } from '../selectors'; + +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: { + features: { + chicken: true, + }, + }, + }; + + expect( isFeatureActive( state, 'chicken' ) ).toBe( true ); + } ); + + it( 'should return false if feature is not active', () => { + const state = { + preferences: { + features: { + chicken: false, + }, + }, + }; + + expect( isFeatureActive( state, 'chicken' ) ).toBe( false ); + } ); + + it( 'should return false if feature is not referred', () => { + const state = { + preferences: { + features: {}, + }, + }; + + expect( isFeatureActive( state, 'chicken' ) ).toBe( false ); + } ); + } ); +} );