From 466628434af0659a8a96f509410dbea1892d86bb Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Mon, 1 Jun 2020 13:26:51 +0900 Subject: [PATCH 01/12] Change the way we check for pending searches from boolean to integer. --- packages/block-directory/src/store/reducer.js | 6 +++--- packages/block-directory/src/store/selectors.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-directory/src/store/reducer.js b/packages/block-directory/src/store/reducer.js index 52e83a8e4db982..c3c15f2a90e191 100644 --- a/packages/block-directory/src/store/reducer.js +++ b/packages/block-directory/src/store/reducer.js @@ -14,7 +14,7 @@ import { combineReducers } from '@wordpress/data'; export const downloadableBlocks = ( state = { results: {}, - isRequestingDownloadableBlocks: true, + pendingBlockRequests: 0, }, action ) => { @@ -22,7 +22,7 @@ export const downloadableBlocks = ( case 'FETCH_DOWNLOADABLE_BLOCKS': return { ...state, - isRequestingDownloadableBlocks: true, + pendingBlockRequests: state.pendingBlockRequests + 1, }; case 'RECEIVE_DOWNLOADABLE_BLOCKS': return { @@ -31,7 +31,7 @@ export const downloadableBlocks = ( ...state.results, [ action.filterValue ]: action.downloadableBlocks, }, - isRequestingDownloadableBlocks: false, + pendingBlockRequests: state.pendingBlockRequests - 1, }; } return state; diff --git a/packages/block-directory/src/store/selectors.js b/packages/block-directory/src/store/selectors.js index 81d6e8ed89c1ed..3a0d9a92decdf4 100644 --- a/packages/block-directory/src/store/selectors.js +++ b/packages/block-directory/src/store/selectors.js @@ -16,7 +16,7 @@ import hasBlockType from './utils/has-block-type'; * @return {Array} Downloadable blocks */ export function isRequestingDownloadableBlocks( state ) { - return state.downloadableBlocks.isRequestingDownloadableBlocks; + return state.downloadableBlocks.pendingBlockRequests > 0; } /** From 556b6df181c1aca4f9c2c360db9cd1dd2bed2f55 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Mon, 1 Jun 2020 13:39:56 +0900 Subject: [PATCH 02/12] Fix reducer tests. --- packages/block-directory/src/store/test/reducer.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/block-directory/src/store/test/reducer.js b/packages/block-directory/src/store/test/reducer.js index 897217ea0f85b4..173fa265bb404b 100644 --- a/packages/block-directory/src/store/test/reducer.js +++ b/packages/block-directory/src/store/test/reducer.js @@ -18,25 +18,29 @@ describe( 'state', () => { describe( 'downloadableBlocks()', () => { it( 'should update state to reflect active search', () => { const initialState = deepFreeze( { - isRequestingDownloadableBlocks: false, + pendingBlockRequests: 0, } ); const state = downloadableBlocks( initialState, { type: 'FETCH_DOWNLOADABLE_BLOCKS', filterValue: 'test', } ); - expect( state.isRequestingDownloadableBlocks ).toEqual( true ); + expect( state.pendingBlockRequests ).toEqual( 1 ); } ); it( 'should update state to reflect search results have returned', () => { const query = downloadableBlock.title; - const state = downloadableBlocks( undefined, { + const initialState = deepFreeze( { + pendingBlockRequests: 1, + } ); + + const state = downloadableBlocks( initialState, { type: 'RECEIVE_DOWNLOADABLE_BLOCKS', filterValue: query, downloadableBlocks: [ downloadableBlock ], } ); - expect( state.isRequestingDownloadableBlocks ).toEqual( false ); + expect( state.pendingBlockRequests ).toEqual( 0 ); } ); it( "should set user's search term and save results", () => { From 622f44f68a21345ab6990a27e99716b31dbc2e92 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Mon, 1 Jun 2020 13:40:16 +0900 Subject: [PATCH 03/12] Add tests for the selector. --- .../src/store/test/selectors.js | 70 ++++--------------- 1 file changed, 12 insertions(+), 58 deletions(-) diff --git a/packages/block-directory/src/store/test/selectors.js b/packages/block-directory/src/store/test/selectors.js index 690bec529018a8..754509afb602d1 100644 --- a/packages/block-directory/src/store/test/selectors.js +++ b/packages/block-directory/src/store/test/selectors.js @@ -15,6 +15,7 @@ import { getNewBlockTypes, getUnusedBlockTypes, isInstalling, + isRequestingDownloadableBlocks, } from '../selectors'; describe( 'selectors', () => { @@ -31,73 +32,27 @@ describe( 'selectors', () => { } ); } ); - describe( 'getNewBlockTypes', () => { - it( 'should retrieve the block types that are installed and in the post content', () => { - getNewBlockTypes.registry = { - select: jest.fn( () => ( { getBlocks: () => blockList } ) ), - }; + describe( 'isRequestingDownloadableBlocks', () => { + it( 'should return false if there are no calls pending', () => { const state = { - blockManagement: { - installedBlockTypes: [ - blockTypeInstalled, - blockTypeUnused, - ], + downloadableBlocks: { + pendingBlockRequests: 0, }, }; - const blockTypes = getNewBlockTypes( state ); - expect( blockTypes ).toHaveLength( 1 ); - expect( blockTypes[ 0 ] ).toEqual( blockTypeInstalled ); - } ); + const isRequesting = isRequestingDownloadableBlocks( state ); - it( 'should return an empty array if no blocks are used', () => { - getNewBlockTypes.registry = { - select: jest.fn( () => ( { getBlocks: () => [] } ) ), - }; - const state = { - blockManagement: { - installedBlockTypes: [ - blockTypeInstalled, - blockTypeUnused, - ], - }, - }; - const blockTypes = getNewBlockTypes( state ); - expect( blockTypes ).toHaveLength( 0 ); + expect( isRequesting ).toEqual( false ); } ); - } ); - describe( 'getUnusedBlockTypes', () => { - it( 'should retrieve the block types that are installed but not used', () => { - getUnusedBlockTypes.registry = { - select: jest.fn( () => ( { getBlocks: () => blockList } ) ), - }; + it( 'should return true if at least one call is pending', () => { const state = { - blockManagement: { - installedBlockTypes: [ - blockTypeInstalled, - blockTypeUnused, - ], + downloadableBlocks: { + pendingBlockRequests: 1, }, }; - const blockTypes = getUnusedBlockTypes( state ); - expect( blockTypes ).toHaveLength( 1 ); - expect( blockTypes[ 0 ] ).toEqual( blockTypeUnused ); - } ); + const isRequesting = isRequestingDownloadableBlocks( state ); - it( 'should return all block types if no blocks are used', () => { - getUnusedBlockTypes.registry = { - select: jest.fn( () => ( { getBlocks: () => [] } ) ), - }; - const state = { - blockManagement: { - installedBlockTypes: [ - blockTypeInstalled, - blockTypeUnused, - ], - }, - }; - const blockTypes = getUnusedBlockTypes( state ); - expect( blockTypes ).toHaveLength( 2 ); + expect( isRequesting ).toEqual( true ); } ); } ); @@ -143,7 +98,6 @@ describe( 'selectors', () => { describe( 'getDownloadableBlocks', () => { const state = { downloadableBlocks: { - isRequestingDownloadableBlocks: false, results: { boxer: [ downloadableBlock ], }, From 04be9d93cf3f5b2124a5d57286cea5b6568a8d23 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Mon, 1 Jun 2020 14:21:00 +0900 Subject: [PATCH 04/12] Change the pending search parameter and add tests to the reducer. --- packages/block-directory/src/store/reducer.js | 6 +-- .../block-directory/src/store/selectors.js | 2 +- .../block-directory/src/store/test/reducer.js | 47 +++++++++++++++++-- .../src/store/test/selectors.js | 4 +- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/packages/block-directory/src/store/reducer.js b/packages/block-directory/src/store/reducer.js index c3c15f2a90e191..a8489f32331fe9 100644 --- a/packages/block-directory/src/store/reducer.js +++ b/packages/block-directory/src/store/reducer.js @@ -14,7 +14,7 @@ import { combineReducers } from '@wordpress/data'; export const downloadableBlocks = ( state = { results: {}, - pendingBlockRequests: 0, + pendingSearchRequests: 0, }, action ) => { @@ -22,7 +22,7 @@ export const downloadableBlocks = ( case 'FETCH_DOWNLOADABLE_BLOCKS': return { ...state, - pendingBlockRequests: state.pendingBlockRequests + 1, + pendingSearchRequests: state.pendingSearchRequests + 1, }; case 'RECEIVE_DOWNLOADABLE_BLOCKS': return { @@ -31,7 +31,7 @@ export const downloadableBlocks = ( ...state.results, [ action.filterValue ]: action.downloadableBlocks, }, - pendingBlockRequests: state.pendingBlockRequests - 1, + pendingSearchRequests: state.pendingSearchRequests - 1, }; } return state; diff --git a/packages/block-directory/src/store/selectors.js b/packages/block-directory/src/store/selectors.js index 3a0d9a92decdf4..09f380ee154e01 100644 --- a/packages/block-directory/src/store/selectors.js +++ b/packages/block-directory/src/store/selectors.js @@ -16,7 +16,7 @@ import hasBlockType from './utils/has-block-type'; * @return {Array} Downloadable blocks */ export function isRequestingDownloadableBlocks( state ) { - return state.downloadableBlocks.pendingBlockRequests > 0; + return state.downloadableBlocks.pendingSearchRequests > 0; } /** diff --git a/packages/block-directory/src/store/test/reducer.js b/packages/block-directory/src/store/test/reducer.js index 173fa265bb404b..574813a3b39cde 100644 --- a/packages/block-directory/src/store/test/reducer.js +++ b/packages/block-directory/src/store/test/reducer.js @@ -18,20 +18,19 @@ describe( 'state', () => { describe( 'downloadableBlocks()', () => { it( 'should update state to reflect active search', () => { const initialState = deepFreeze( { - pendingBlockRequests: 0, + pendingSearchRequests: 0, } ); const state = downloadableBlocks( initialState, { type: 'FETCH_DOWNLOADABLE_BLOCKS', - filterValue: 'test', } ); - expect( state.pendingBlockRequests ).toEqual( 1 ); + expect( state.pendingSearchRequests ).toEqual( 1 ); } ); it( 'should update state to reflect search results have returned', () => { const query = downloadableBlock.title; const initialState = deepFreeze( { - pendingBlockRequests: 1, + pendingSearchRequests: 1, } ); const state = downloadableBlocks( initialState, { @@ -40,7 +39,7 @@ describe( 'state', () => { downloadableBlocks: [ downloadableBlock ], } ); - expect( state.pendingBlockRequests ).toEqual( 0 ); + expect( state.pendingSearchRequests ).toEqual( 0 ); } ); it( "should set user's search term and save results", () => { @@ -62,6 +61,44 @@ describe( 'state', () => { expect( Object.keys( updatedState.results ) ).toHaveLength( 2 ); } ); + + it( 'should increment block requests', () => { + const initialState = { + pendingSearchRequests: 0, + }; + + // Simulate the first call. + const state = downloadableBlocks( initialState, { + type: 'FETCH_DOWNLOADABLE_BLOCKS', + } ); + + // Simulate a second call. + const finalState = downloadableBlocks( state, { + type: 'FETCH_DOWNLOADABLE_BLOCKS', + } ); + + expect( finalState.pendingSearchRequests ).toEqual( 2 ); + } ); + it( 'should decrement block requests', () => { + const initialState = { + pendingSearchRequests: 2, + }; + + // Simulate the first call response + const state = downloadableBlocks( initialState, { + type: 'RECEIVE_DOWNLOADABLE_BLOCKS', + filterValue: 'Test', + downloadableBlocks: [], + } ); + + const finalState = downloadableBlocks( state, { + type: 'RECEIVE_DOWNLOADABLE_BLOCKS', + filterValue: 'Test 1', + downloadableBlocks: [], + } ); + + expect( finalState.pendingSearchRequests ).toEqual( 0 ); + } ); } ); describe( 'blockManagement()', () => { diff --git a/packages/block-directory/src/store/test/selectors.js b/packages/block-directory/src/store/test/selectors.js index 754509afb602d1..1b23dd222bfc16 100644 --- a/packages/block-directory/src/store/test/selectors.js +++ b/packages/block-directory/src/store/test/selectors.js @@ -36,7 +36,7 @@ describe( 'selectors', () => { it( 'should return false if there are no calls pending', () => { const state = { downloadableBlocks: { - pendingBlockRequests: 0, + pendingSearchRequests: 0, }, }; const isRequesting = isRequestingDownloadableBlocks( state ); @@ -47,7 +47,7 @@ describe( 'selectors', () => { it( 'should return true if at least one call is pending', () => { const state = { downloadableBlocks: { - pendingBlockRequests: 1, + pendingSearchRequests: 1, }, }; const isRequesting = isRequestingDownloadableBlocks( state ); From a0f62aa3e74d1982893ad4e00db50a0ecbf78fec Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Mon, 1 Jun 2020 14:35:01 +0900 Subject: [PATCH 05/12] Add space between tests to maintain spacing consistency. --- packages/block-directory/src/store/test/reducer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-directory/src/store/test/reducer.js b/packages/block-directory/src/store/test/reducer.js index 574813a3b39cde..fcbe7f77a6e01e 100644 --- a/packages/block-directory/src/store/test/reducer.js +++ b/packages/block-directory/src/store/test/reducer.js @@ -78,7 +78,8 @@ describe( 'state', () => { } ); expect( finalState.pendingSearchRequests ).toEqual( 2 ); - } ); + } ); + it( 'should decrement block requests', () => { const initialState = { pendingSearchRequests: 2, From 134b1dd4a6e66514bf1359bdd620fe5bc7636fa8 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Thu, 4 Jun 2020 11:57:50 +0900 Subject: [PATCH 06/12] Change structure of downloadable blocks to use store results & pending status. --- .../downloadable-blocks-panel/index.js | 2 +- packages/block-directory/src/store/reducer.js | 19 ++-- .../block-directory/src/store/selectors.js | 19 +++- .../block-directory/src/store/test/reducer.js | 106 ++++++++---------- .../src/store/test/selectors.js | 26 +++-- 5 files changed, 88 insertions(+), 84 deletions(-) diff --git a/packages/block-directory/src/components/downloadable-blocks-panel/index.js b/packages/block-directory/src/components/downloadable-blocks-panel/index.js index 08d346d6774a58..df2060eb265816 100644 --- a/packages/block-directory/src/components/downloadable-blocks-panel/index.js +++ b/packages/block-directory/src/components/downloadable-blocks-panel/index.js @@ -94,7 +94,7 @@ export default compose( [ const downloadableItems = hasPermission ? getDownloadableBlocks( filterValue ) : []; - const isLoading = isRequestingDownloadableBlocks(); + const isLoading = isRequestingDownloadableBlocks( filterValue ); return { downloadableItems, diff --git a/packages/block-directory/src/store/reducer.js b/packages/block-directory/src/store/reducer.js index a8489f32331fe9..fabc863e72a482 100644 --- a/packages/block-directory/src/store/reducer.js +++ b/packages/block-directory/src/store/reducer.js @@ -11,27 +11,22 @@ import { combineReducers } from '@wordpress/data'; * * @return {Object} Updated state. */ -export const downloadableBlocks = ( - state = { - results: {}, - pendingSearchRequests: 0, - }, - action -) => { +export const downloadableBlocks = ( state = {}, action ) => { switch ( action.type ) { case 'FETCH_DOWNLOADABLE_BLOCKS': return { ...state, - pendingSearchRequests: state.pendingSearchRequests + 1, + [ action.filterValue ]: { + isRequesting: true, + }, }; case 'RECEIVE_DOWNLOADABLE_BLOCKS': return { ...state, - results: { - ...state.results, - [ action.filterValue ]: action.downloadableBlocks, + [ action.filterValue ]: { + results: action.downloadableBlocks, + isRequesting: false, }, - pendingSearchRequests: state.pendingSearchRequests - 1, }; } return state; diff --git a/packages/block-directory/src/store/selectors.js b/packages/block-directory/src/store/selectors.js index 09f380ee154e01..1e9e93b31bbb37 100644 --- a/packages/block-directory/src/store/selectors.js +++ b/packages/block-directory/src/store/selectors.js @@ -12,11 +12,19 @@ import hasBlockType from './utils/has-block-type'; * Returns true if application is requesting for downloadable blocks. * * @param {Object} state Global application state. + * @param {string} filterValue Search string. + * * * @return {Array} Downloadable blocks */ -export function isRequestingDownloadableBlocks( state ) { - return state.downloadableBlocks.pendingSearchRequests > 0; +export function isRequestingDownloadableBlocks( state, filterValue ) { + if ( + ! state.downloadableBlocks[ filterValue ] || + ! state.downloadableBlocks[ filterValue ].isRequesting + ) { + return false; + } + return state.downloadableBlocks[ filterValue ].isRequesting; } /** @@ -28,10 +36,13 @@ export function isRequestingDownloadableBlocks( state ) { * @return {Array} Downloadable blocks */ export function getDownloadableBlocks( state, filterValue ) { - if ( ! state.downloadableBlocks.results[ filterValue ] ) { + if ( + ! state.downloadableBlocks[ filterValue ] || + ! state.downloadableBlocks[ filterValue ].results + ) { return []; } - return state.downloadableBlocks.results[ filterValue ]; + return state.downloadableBlocks[ filterValue ].results; } /** diff --git a/packages/block-directory/src/store/test/reducer.js b/packages/block-directory/src/store/test/reducer.js index fcbe7f77a6e01e..388c35c03498d6 100644 --- a/packages/block-directory/src/store/test/reducer.js +++ b/packages/block-directory/src/store/test/reducer.js @@ -17,21 +17,24 @@ import { blockTypeInstalled, downloadableBlock } from './fixtures'; describe( 'state', () => { describe( 'downloadableBlocks()', () => { it( 'should update state to reflect active search', () => { - const initialState = deepFreeze( { - pendingSearchRequests: 0, - } ); + const initialState = {}; + const blockName = 'Awesome Block'; + const state = downloadableBlocks( initialState, { type: 'FETCH_DOWNLOADABLE_BLOCKS', + filterValue: blockName, } ); - expect( state.pendingSearchRequests ).toEqual( 1 ); + expect( state[ blockName ].isRequesting ).toEqual( true ); } ); it( 'should update state to reflect search results have returned', () => { const query = downloadableBlock.title; - const initialState = deepFreeze( { - pendingSearchRequests: 1, - } ); + const initialState = { + [ query ]: { + isRequesting: true, + }, + }; const state = downloadableBlocks( initialState, { type: 'RECEIVE_DOWNLOADABLE_BLOCKS', @@ -39,66 +42,51 @@ describe( 'state', () => { downloadableBlocks: [ downloadableBlock ], } ); - expect( state.pendingSearchRequests ).toEqual( 0 ); + expect( state[ query ].isRequesting ).toEqual( false ); } ); it( "should set user's search term and save results", () => { const query = downloadableBlock.title; - const state = downloadableBlocks( undefined, { - type: 'RECEIVE_DOWNLOADABLE_BLOCKS', - filterValue: query, - downloadableBlocks: [ downloadableBlock ], - } ); - expect( state.results ).toHaveProperty( query ); - expect( state.results[ query ] ).toHaveLength( 1 ); - - // It should append to the results - const updatedState = downloadableBlocks( state, { - type: 'RECEIVE_DOWNLOADABLE_BLOCKS', - filterValue: 'Test 1', - downloadableBlocks: [ downloadableBlock ], - } ); - - expect( Object.keys( updatedState.results ) ).toHaveLength( 2 ); + const state = downloadableBlocks( + {}, + { + type: 'RECEIVE_DOWNLOADABLE_BLOCKS', + filterValue: query, + downloadableBlocks: [ downloadableBlock ], + } + ); + expect( state ).toHaveProperty( query ); + expect( state[ query ].results ).toHaveLength( 1 ); } ); - it( 'should increment block requests', () => { - const initialState = { - pendingSearchRequests: 0, - }; - - // Simulate the first call. - const state = downloadableBlocks( initialState, { - type: 'FETCH_DOWNLOADABLE_BLOCKS', - } ); + it( 'should set query to reflect its pending status', () => { + const filterValue = 'Awesome Block'; // Simulate a second call. - const finalState = downloadableBlocks( state, { - type: 'FETCH_DOWNLOADABLE_BLOCKS', - } ); - - expect( finalState.pendingSearchRequests ).toEqual( 2 ); - } ); - - it( 'should decrement block requests', () => { - const initialState = { - pendingSearchRequests: 2, - }; - - // Simulate the first call response - const state = downloadableBlocks( initialState, { - type: 'RECEIVE_DOWNLOADABLE_BLOCKS', - filterValue: 'Test', - downloadableBlocks: [], - } ); - - const finalState = downloadableBlocks( state, { - type: 'RECEIVE_DOWNLOADABLE_BLOCKS', - filterValue: 'Test 1', - downloadableBlocks: [], - } ); - - expect( finalState.pendingSearchRequests ).toEqual( 0 ); + const stateAfterRequest = downloadableBlocks( + {}, + { + type: 'FETCH_DOWNLOADABLE_BLOCKS', + filterValue, + } + ); + + expect( stateAfterRequest[ filterValue ].isRequesting ).toEqual( + true + ); + + const stateAfterResponse = downloadableBlocks( + {}, + { + type: 'RECEIVE_DOWNLOADABLE_BLOCKS', + filterValue, + downloadableBlocks: [], + } + ); + + expect( stateAfterResponse[ filterValue ].isRequesting ).toEqual( + false + ); } ); } ); diff --git a/packages/block-directory/src/store/test/selectors.js b/packages/block-directory/src/store/test/selectors.js index 1b23dd222bfc16..28406d92ad0119 100644 --- a/packages/block-directory/src/store/test/selectors.js +++ b/packages/block-directory/src/store/test/selectors.js @@ -34,23 +34,33 @@ describe( 'selectors', () => { describe( 'isRequestingDownloadableBlocks', () => { it( 'should return false if there are no calls pending', () => { + const filterValue = 'Awesome Block'; + const state = { - downloadableBlocks: { - pendingSearchRequests: 0, - }, + downloadableBlocks: {}, }; - const isRequesting = isRequestingDownloadableBlocks( state ); + const isRequesting = isRequestingDownloadableBlocks( + state, + filterValue + ); expect( isRequesting ).toEqual( false ); } ); it( 'should return true if at least one call is pending', () => { + const filterValue = 'Awesome Block'; + const state = { downloadableBlocks: { - pendingSearchRequests: 1, + [ filterValue ]: { + isRequesting: true, + }, }, }; - const isRequesting = isRequestingDownloadableBlocks( state ); + const isRequesting = isRequestingDownloadableBlocks( + state, + filterValue + ); expect( isRequesting ).toEqual( true ); } ); @@ -98,8 +108,8 @@ describe( 'selectors', () => { describe( 'getDownloadableBlocks', () => { const state = { downloadableBlocks: { - results: { - boxer: [ downloadableBlock ], + boxer: { + results: [ downloadableBlock ], }, }, }; From 812d6d7efbef84a693b0ea360d5c294bb1d096b0 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Thu, 4 Jun 2020 13:03:13 +0900 Subject: [PATCH 07/12] Clean up variable name. --- packages/block-directory/src/store/test/reducer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-directory/src/store/test/reducer.js b/packages/block-directory/src/store/test/reducer.js index 388c35c03498d6..abd72ed2422ae1 100644 --- a/packages/block-directory/src/store/test/reducer.js +++ b/packages/block-directory/src/store/test/reducer.js @@ -18,14 +18,14 @@ describe( 'state', () => { describe( 'downloadableBlocks()', () => { it( 'should update state to reflect active search', () => { const initialState = {}; - const blockName = 'Awesome Block'; + const filterValue = 'Awesome Block'; const state = downloadableBlocks( initialState, { type: 'FETCH_DOWNLOADABLE_BLOCKS', - filterValue: blockName, + filterValue, } ); - expect( state[ blockName ].isRequesting ).toEqual( true ); + expect( state[ filterValue ].isRequesting ).toEqual( true ); } ); it( 'should update state to reflect search results have returned', () => { From d2d2db3276fe5491d19ae23984b8cd3b42c2da94 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Mon, 8 Jun 2020 10:23:46 +0900 Subject: [PATCH 08/12] Remove redundant test. --- .../block-directory/src/store/test/reducer.js | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/packages/block-directory/src/store/test/reducer.js b/packages/block-directory/src/store/test/reducer.js index abd72ed2422ae1..f2c9886de95de2 100644 --- a/packages/block-directory/src/store/test/reducer.js +++ b/packages/block-directory/src/store/test/reducer.js @@ -58,36 +58,6 @@ describe( 'state', () => { expect( state ).toHaveProperty( query ); expect( state[ query ].results ).toHaveLength( 1 ); } ); - - it( 'should set query to reflect its pending status', () => { - const filterValue = 'Awesome Block'; - - // Simulate a second call. - const stateAfterRequest = downloadableBlocks( - {}, - { - type: 'FETCH_DOWNLOADABLE_BLOCKS', - filterValue, - } - ); - - expect( stateAfterRequest[ filterValue ].isRequesting ).toEqual( - true - ); - - const stateAfterResponse = downloadableBlocks( - {}, - { - type: 'RECEIVE_DOWNLOADABLE_BLOCKS', - filterValue, - downloadableBlocks: [], - } - ); - - expect( stateAfterResponse[ filterValue ].isRequesting ).toEqual( - false - ); - } ); } ); describe( 'blockManagement()', () => { From 3461cd3e0fb4a74fcad9d12932e5c4817241a072 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Mon, 8 Jun 2020 10:39:15 +0900 Subject: [PATCH 09/12] Add test case for retrieving request status and update text to be clearer. --- .../src/store/test/selectors.js | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/block-directory/src/store/test/selectors.js b/packages/block-directory/src/store/test/selectors.js index 28406d92ad0119..148dc7494c086c 100644 --- a/packages/block-directory/src/store/test/selectors.js +++ b/packages/block-directory/src/store/test/selectors.js @@ -33,7 +33,7 @@ describe( 'selectors', () => { } ); describe( 'isRequestingDownloadableBlocks', () => { - it( 'should return false if there are no calls pending', () => { + it( 'should return false if no requests have been made for the block', () => { const filterValue = 'Awesome Block'; const state = { @@ -47,7 +47,25 @@ describe( 'selectors', () => { expect( isRequesting ).toEqual( false ); } ); - it( 'should return true if at least one call is pending', () => { + it( 'should return false if there are no pending requests for the block', () => { + const filterValue = 'Awesome Block'; + + const state = { + downloadableBlocks: { + [ filterValue ]: { + isRequesting: false, + }, + }, + }; + const isRequesting = isRequestingDownloadableBlocks( + state, + filterValue + ); + + expect( isRequesting ).toEqual( false ); + } ); + + it( 'should return true if the block has a pending request', () => { const filterValue = 'Awesome Block'; const state = { @@ -55,6 +73,9 @@ describe( 'selectors', () => { [ filterValue ]: { isRequesting: true, }, + 'previous-search-keyword': { + isRequesting: false, + }, }, }; const isRequesting = isRequestingDownloadableBlocks( From 19d7d93b2080d966202718d2a99a5f5c98348f25 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Tue, 9 Jun 2020 10:32:08 +0900 Subject: [PATCH 10/12] Deep freeze initial state for downloadable blocks test. --- .../block-directory/src/store/test/reducer.js | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/block-directory/src/store/test/reducer.js b/packages/block-directory/src/store/test/reducer.js index f2c9886de95de2..14ccd5e2eb66c0 100644 --- a/packages/block-directory/src/store/test/reducer.js +++ b/packages/block-directory/src/store/test/reducer.js @@ -17,7 +17,7 @@ import { blockTypeInstalled, downloadableBlock } from './fixtures'; describe( 'state', () => { describe( 'downloadableBlocks()', () => { it( 'should update state to reflect active search', () => { - const initialState = {}; + const initialState = deepFreeze( {} ); const filterValue = 'Awesome Block'; const state = downloadableBlocks( initialState, { @@ -30,11 +30,11 @@ describe( 'state', () => { it( 'should update state to reflect search results have returned', () => { const query = downloadableBlock.title; - const initialState = { + const initialState = deepFreeze( { [ query ]: { isRequesting: true, }, - }; + } ); const state = downloadableBlocks( initialState, { type: 'RECEIVE_DOWNLOADABLE_BLOCKS', @@ -47,14 +47,12 @@ describe( 'state', () => { it( "should set user's search term and save results", () => { const query = downloadableBlock.title; - const state = downloadableBlocks( - {}, - { - type: 'RECEIVE_DOWNLOADABLE_BLOCKS', - filterValue: query, - downloadableBlocks: [ downloadableBlock ], - } - ); + const initialState = deepFreeze( {} ); + const state = downloadableBlocks( initialState, { + type: 'RECEIVE_DOWNLOADABLE_BLOCKS', + filterValue: query, + downloadableBlocks: [ downloadableBlock ], + } ); expect( state ).toHaveProperty( query ); expect( state[ query ].results ).toHaveLength( 1 ); } ); From 015127520b1ef6a295859aabc917d5f1217a5f3b Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Tue, 16 Jun 2020 10:53:58 +0900 Subject: [PATCH 11/12] Add back in tests removed in rebase. --- .../src/store/test/selectors.js | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/packages/block-directory/src/store/test/selectors.js b/packages/block-directory/src/store/test/selectors.js index 148dc7494c086c..ff2b6911d60458 100644 --- a/packages/block-directory/src/store/test/selectors.js +++ b/packages/block-directory/src/store/test/selectors.js @@ -102,6 +102,76 @@ describe( 'selectors', () => { } ); } ); + describe( 'getNewBlockTypes', () => { + it( 'should retrieve the block types that are installed and in the post content', () => { + getNewBlockTypes.registry = { + select: jest.fn( () => ( { getBlocks: () => blockList } ) ), + }; + const state = { + blockManagement: { + installedBlockTypes: [ + blockTypeInstalled, + blockTypeUnused, + ], + }, + }; + const blockTypes = getNewBlockTypes( state ); + expect( blockTypes ).toHaveLength( 1 ); + expect( blockTypes[ 0 ] ).toEqual( blockTypeInstalled ); + } ); + + it( 'should return an empty array if no blocks are used', () => { + getNewBlockTypes.registry = { + select: jest.fn( () => ( { getBlocks: () => [] } ) ), + }; + const state = { + blockManagement: { + installedBlockTypes: [ + blockTypeInstalled, + blockTypeUnused, + ], + }, + }; + const blockTypes = getNewBlockTypes( state ); + expect( blockTypes ).toHaveLength( 0 ); + } ); + } ); + + describe( 'getUnusedBlockTypes', () => { + it( 'should retrieve the block types that are installed but not used', () => { + getUnusedBlockTypes.registry = { + select: jest.fn( () => ( { getBlocks: () => blockList } ) ), + }; + const state = { + blockManagement: { + installedBlockTypes: [ + blockTypeInstalled, + blockTypeUnused, + ], + }, + }; + const blockTypes = getUnusedBlockTypes( state ); + expect( blockTypes ).toHaveLength( 1 ); + expect( blockTypes[ 0 ] ).toEqual( blockTypeUnused ); + } ); + + it( 'should return all block types if no blocks are used', () => { + getUnusedBlockTypes.registry = { + select: jest.fn( () => ( { getBlocks: () => [] } ) ), + }; + const state = { + blockManagement: { + installedBlockTypes: [ + blockTypeInstalled, + blockTypeUnused, + ], + }, + }; + const blockTypes = getUnusedBlockTypes( state ); + expect( blockTypes ).toHaveLength( 2 ); + } ); + } ); + describe( 'getErrorNoticeForBlock', () => { const state = { errorNotices: { From df0d8ec15fa1281edfaaa8746f3e4a1d35fad968 Mon Sep 17 00:00:00 2001 From: dufresnesteven Date: Tue, 16 Jun 2020 11:18:10 +0900 Subject: [PATCH 12/12] Move the test back into its area. --- .../src/store/test/selectors.js | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/block-directory/src/store/test/selectors.js b/packages/block-directory/src/store/test/selectors.js index ff2b6911d60458..14d72e7c7e178b 100644 --- a/packages/block-directory/src/store/test/selectors.js +++ b/packages/block-directory/src/store/test/selectors.js @@ -87,21 +87,6 @@ describe( 'selectors', () => { } ); } ); - describe( 'getErrorNotices', () => { - const state = { - errorNotices: { - 'block/has-error': 'Error notice', - }, - }; - - it( 'should retrieve all error notices', () => { - const errorNotices = getErrorNotices( state ); - expect( errorNotices ).toEqual( { - 'block/has-error': 'Error notice', - } ); - } ); - } ); - describe( 'getNewBlockTypes', () => { it( 'should retrieve the block types that are installed and in the post content', () => { getNewBlockTypes.registry = { @@ -172,6 +157,21 @@ describe( 'selectors', () => { } ); } ); + describe( 'getErrorNotices', () => { + const state = { + errorNotices: { + 'block/has-error': 'Error notice', + }, + }; + + it( 'should retrieve all error notices', () => { + const errorNotices = getErrorNotices( state ); + expect( errorNotices ).toEqual( { + 'block/has-error': 'Error notice', + } ); + } ); + } ); + describe( 'getErrorNoticeForBlock', () => { const state = { errorNotices: {