-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Directory: Refactor the reducer by breaking out the block manag…
…ement actions into their own reducer. (#19330) * Block Directory: Refactory the reducer by break out the block management actions into their own reducer. * Moved hasPermission into its own reducer. * Also remove the 'items' list as it's not being used * Update the getInstalledBlockTypes selector to point to the new reducer that manages installs. * Update typo in test. * Remove the lodash dependency in the selectors. It isn\'t necessary.
- Loading branch information
1 parent
17ef62d
commit 4cd1492
Showing
5 changed files
with
180 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const downloadableBlock = { | ||
name: 'boxer/boxer', | ||
title: 'Boxer', | ||
description: 'Boxer is a Block that puts your WordPress posts into boxes on a page.', | ||
id: 'boxer-block', | ||
rating: 5, | ||
ratingCount: 1, | ||
activeInstalls: 0, | ||
authorBlockRating: 5, | ||
authorBlockCount: '1', | ||
author: 'CK Lee', | ||
icon: 'block-default', | ||
assets: [ | ||
'https://plugins.svn.wordpress.org/boxer-block/trunk/build/index.js', | ||
'https://plugins.svn.wordpress.org/boxer-block/trunk/build/view.js', | ||
], | ||
humanizedUpdated: '3 months ago', | ||
}; | ||
|
||
export const installedItem = { | ||
id: 'boxer-block', | ||
name: 'boxer/boxer', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import deepFreeze from 'deep-freeze'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
downloadableBlocks, | ||
blockManagement, | ||
hasPermission, | ||
} from '../reducer'; | ||
import { installedItem, downloadableBlock } from './fixtures'; | ||
|
||
describe( 'state', () => { | ||
describe( 'downloadableBlocks()', () => { | ||
it( 'should update state to reflect active search', () => { | ||
const initialState = deepFreeze( { | ||
isRequestingDownloadableBlocks: false, | ||
} ); | ||
const state = downloadableBlocks( initialState, { | ||
type: 'FETCH_DOWNLOADABLE_BLOCKS', | ||
} ); | ||
|
||
expect( state.isRequestingDownloadableBlocks ).toBe( true ); | ||
} ); | ||
|
||
it( 'should update state to reflect search results have returned', () => { | ||
const state = downloadableBlocks( undefined, { | ||
type: 'RECEIVE_DOWNLOADABLE_BLOCKS', | ||
filterValue: downloadableBlock.title, | ||
downloadableBlocks: [ downloadableBlock ], | ||
} ); | ||
|
||
expect( state.isRequestingDownloadableBlocks ).toBe( false ); | ||
} ); | ||
|
||
it( 'should set user\'s search term and save results', () => { | ||
const state = downloadableBlocks( undefined, { | ||
type: 'RECEIVE_DOWNLOADABLE_BLOCKS', | ||
filterValue: downloadableBlock.title, | ||
downloadableBlocks: [ downloadableBlock ], | ||
} ); | ||
expect( state.results ).toHaveProperty( downloadableBlock.title ); | ||
expect( state.results[ downloadableBlock.title ] ).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 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'blockManagement()', () => { | ||
it( 'should start with an empty installedBlockTypes List', () => { | ||
const state = blockManagement( undefined, { | ||
type: 'NOOP_TYPE', | ||
} ); | ||
expect( state.installedBlockTypes ).toEqual( [] ); | ||
} ); | ||
|
||
it( 'should add item to the installedBlockTypesList', () => { | ||
const initialState = deepFreeze( { installedBlockTypes: [] } ); | ||
const state = blockManagement( initialState, { | ||
type: 'ADD_INSTALLED_BLOCK_TYPE', | ||
item: installedItem, | ||
} ); | ||
|
||
expect( state.installedBlockTypes ).toHaveLength( 1 ); | ||
} ); | ||
|
||
it( 'should remove item from the installedBlockTypesList', () => { | ||
const initialState = deepFreeze( { | ||
installedBlockTypes: [ installedItem ], | ||
} ); | ||
const state = blockManagement( initialState, { | ||
type: 'REMOVE_INSTALLED_BLOCK_TYPE', | ||
item: installedItem, | ||
} ); | ||
|
||
expect( state.installedBlockTypes ).toHaveLength( 0 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'hasPermission()', () => { | ||
it( 'should update permissions appropriately', () => { | ||
const state = hasPermission( true, { | ||
type: 'SET_INSTALL_BLOCKS_PERMISSION', | ||
hasPermission: false, | ||
} ); | ||
|
||
expect( state ).toBe( false ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
getInstalledBlockTypes, | ||
} from '../selectors'; | ||
|
||
describe( 'selectors', () => { | ||
describe( 'getInstalledBlockTypes', () => { | ||
it( 'should retrieve the block types that are installed', () => { | ||
const blockTypes = [ 'fake-type' ]; | ||
const state = { | ||
blockManagement: { | ||
installedBlockTypes: blockTypes, | ||
}, | ||
}; | ||
const installedBlockTypes = getInstalledBlockTypes( state ); | ||
expect( installedBlockTypes ).toEqual( blockTypes ); | ||
} ); | ||
} ); | ||
} ); |