Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Directory: Refactor the reducer by breaking out the block management actions into their own reducer. #19330

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/block-directory/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const downloadableBlocks = ( state = {
hasPermission: true,
filterValue: undefined,
isRequestingDownloadableBlocks: true,
installedBlockTypes: [],
}, action ) => {
switch ( action.type ) {
case 'FETCH_DOWNLOADABLE_BLOCKS' :
Expand All @@ -33,6 +32,24 @@ export const downloadableBlocks = ( state = {
hasPermission: true,
isRequestingDownloadableBlocks: false,
};
}
return state;
};

/**
* Reducer managing the installation and deletion of blocks.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export const blockManagement = ( state = {
hasPermission: true,
talldan marked this conversation as resolved.
Show resolved Hide resolved
installedBlockTypes: [],
items: [],
talldan marked this conversation as resolved.
Show resolved Hide resolved
}, action ) => {
switch ( action.type ) {
case 'SET_INSTALL_BLOCKS_PERMISSION' :
return {
...state,
Expand All @@ -44,7 +61,6 @@ export const downloadableBlocks = ( state = {
...state,
installedBlockTypes: [ ...state.installedBlockTypes, action.item ],
};

case 'REMOVE_INSTALLED_BLOCK_TYPE' :
return {
...state,
Expand All @@ -56,4 +72,5 @@ export const downloadableBlocks = ( state = {

export default combineReducers( {
downloadableBlocks,
blockManagement,
talldan marked this conversation as resolved.
Show resolved Hide resolved
} );
23 changes: 23 additions & 0 deletions packages/block-directory/src/store/test/fixtures/index.js
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',
};
107 changes: 107 additions & 0 deletions packages/block-directory/src/store/test/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
import {
downloadableBlocks,
blockManagement,
} from '../reducer';
import { installedItem, downloadableBlock } from './fixtures';

describe( 'state', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see these tests!

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 set state to reflect user not having permissions', () => {
const initialState = deepFreeze( {
items: [ downloadableBlock ],
} );
const state = blockManagement( initialState, {
type: 'SET_INSTALL_BLOCKS_PERMISSION',
hasPermission: false,
} );

expect( state.hasPermission ).toBe( false );
expect( state.items ).toHaveLength( 0 );
} );

it( 'should set state to reflect user having permissions', () => {
const initialState = deepFreeze( {
items: [ downloadableBlock ],
} );
const state = blockManagement( initialState, {
type: 'SET_INSTALL_BLOCKS_PERMISSION',
hasPermission: true,
} );

expect( state.hasPermission ).toBe( true );
expect( state.items ).toHaveLength( 1 );
} );

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 );
} );
} );
} );