Skip to content

Commit

Permalink
Define constants for the various utility levels
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed May 24, 2018
1 parent 470d32e commit 6efa3af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 8 additions & 4 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import { deprecated } from '@wordpress/utils';
const MAX_RECENT_BLOCKS = 9;
export const POST_UPDATE_TRANSACTION_ID = 'post-update';
const PERMALINK_POSTNAME_REGEX = /%(?:postname|pagename)%/;
export const INSERTER_UTILITY_HIGH = 3;
export const INSERTER_UTILITY_MEDIUM = 2;
export const INSERTER_UTILITY_LOW = 1;
export const INSERTER_UTILITY_NONE = 0;

/**
* Shared reference to an empty array for cases where it is important to avoid
Expand Down Expand Up @@ -1336,13 +1340,13 @@ export const getInserterItems = createSelector(

const calculateUtility = ( category, count, isContextual ) => {
if ( isContextual ) {
return 3;
return INSERTER_UTILITY_HIGH;
} else if ( count > 0 ) {
return 2;
return INSERTER_UTILITY_MEDIUM;
} else if ( category === 'common' ) {
return 1;
return INSERTER_UTILITY_LOW;
}
return 0;
return INSERTER_UTILITY_NONE;
};

const calculateFrecency = ( time, count ) => {
Expand Down
15 changes: 9 additions & 6 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ const {
isPermalinkEditable,
getPermalink,
getPermalinkParts,
INSERTER_UTILITY_HIGH,
INSERTER_UTILITY_MEDIUM,
INSERTER_UTILITY_LOW,
} = selectors;

describe( 'selectors', () => {
Expand Down Expand Up @@ -2918,7 +2921,7 @@ describe( 'selectors', () => {
expect( testBlockBItem.isDisabled ).toBe( true );
} );

it( 'should give common blocks a medium utility', () => {
it( 'should give common blocks a low utility', () => {
const state = {
editor: {
present: {
Expand All @@ -2939,10 +2942,10 @@ describe( 'selectors', () => {
};
const items = getInserterItems( state );
const testBlockBItem = items.find( ( item ) => item.id === 'core/test-block-b' );
expect( testBlockBItem.utility ).toBe( 1 );
expect( testBlockBItem.utility ).toBe( INSERTER_UTILITY_LOW );
} );

it( 'should give used blocks a high utility and set a frecency', () => {
it( 'should give used blocks a medium utility and set a frecency', () => {
const state = {
editor: {
present: {
Expand All @@ -2965,11 +2968,11 @@ describe( 'selectors', () => {
};
const items = getInserterItems( state );
const sharedBlock2Item = items.find( ( item ) => item.id === 'core/test-block-b' );
expect( sharedBlock2Item.utility ).toBe( 2 );
expect( sharedBlock2Item.utility ).toBe( INSERTER_UTILITY_MEDIUM );
expect( sharedBlock2Item.frecency ).toBe( 2.5 );
} );

it( 'should give contextual blocks the highest utility', () => {
it( 'should give contextual blocks a high utility', () => {
const state = {
editor: {
present: {
Expand All @@ -2994,7 +2997,7 @@ describe( 'selectors', () => {
};
const items = getInserterItems( state, 'block1' );
const testBlockCItem = items.find( ( item ) => item.id === 'core/test-block-c' );
expect( testBlockCItem.utility ).toBe( 3 );
expect( testBlockCItem.utility ).toBe( INSERTER_UTILITY_HIGH );
} );
} );

Expand Down

0 comments on commit 6efa3af

Please sign in to comment.