-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add new function to register a new block category #1732
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a295973
Add new function to register a new block category
kuoko 28d7867
Fixed eslint validation
kuoko 381895c
Add unit tests for categories.js
kuoko cc495fe
Add new function to set the display order for an existing block categ…
kuoko 7d0a741
Changed some test-unit messages
kuoko 8b6d651
Removed blank lines
kuoko 803c3d9
Fixed test unit
kuoko 0f4e2a9
Add spaces
kuoko f5f593f
Add @wordpress/ prefix for the i18n dependency
kuoko 0df4b5c
Return modified categories array sortCategoriesBy and setCategoryOrder
kuoko c574c2a
Add more test-unit
kuoko 617eb05
Use full-lenght definition category (not cat) - Change sortCategories…
kuoko c8a9af4
documentation and lingo tweaks
kuoko a4d0e37
Resolve conflict
kuoko 775cf03
Make categories const, explicit categoryNamePattern regexp as costant…
kuoko 02a365a
Remove all the methods used for sorting categories
kuoko 9afca9d
Remove identifiers deleted from the categories module
kuoko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
@@ -20,6 +22,14 @@ const categories = [ | |
{ slug: 'reusable-blocks', title: __( 'Saved Blocks' ) }, | ||
]; | ||
|
||
/** | ||
* @type {RegExp} | ||
* @const | ||
* | ||
* Category names must be a combination of lower-case letters, numbers, and hypens | ||
*/ | ||
const categoryNamePattern = /^[a-z0-9-]+$/; | ||
|
||
/** | ||
* Returns all the block categories. | ||
* | ||
|
@@ -28,3 +38,48 @@ const categories = [ | |
export function getCategories() { | ||
return categories; | ||
} | ||
|
||
/** | ||
* Register a new block Category. | ||
* | ||
* @param {Array} category e.g {slug: 'custom', title: __('Custom Blocks')} | ||
* | ||
* @return {Array} categories | ||
*/ | ||
export function registerCategory( category ) { | ||
if ( ! category ) { | ||
console.error( | ||
'The block Category must be defined' | ||
); | ||
return; | ||
} | ||
if ( ! category.slug ) { | ||
console.error( | ||
'The block Category slug must be defined' | ||
); | ||
return; | ||
} | ||
if ( ! categoryNamePattern.test( category.slug ) ) { | ||
console.error( | ||
'The block Category slug must not contain characters which are invalid for urls' | ||
); | ||
return; | ||
} | ||
|
||
if ( categories.some( x => x.slug === category.slug ) ) { | ||
console.error( | ||
'The block Category "' + category.slug + '" is already registered' | ||
); | ||
return; | ||
} | ||
if ( ! category.title ) { | ||
console.error( | ||
'The block Category title must be defined' | ||
); | ||
return; | ||
} | ||
|
||
categories.push( category ); | ||
return categories; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh typing…we either get it in a robust and helpful way or we manually force it in unreliable and costly ways… (this is not a comment on the code submission, but just me noting a place where having static typing could save us so much effort and provide us so much help) |
||
|
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,69 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
registerCategory, | ||
} from '../categories'; | ||
|
||
describe( 'categories', () => { | ||
const error = console.error; | ||
|
||
// Reset block state before each test. | ||
beforeEach( () => { | ||
console.error = jest.fn(); | ||
} ); | ||
|
||
afterEach( () => { | ||
console.error = error; | ||
} ); | ||
|
||
describe( 'registerCategory()', () => { | ||
it( 'should reject empty categories', () => { | ||
const categories = registerCategory(); | ||
expect( console.error ).toHaveBeenCalledWith( 'The block Category must be defined' ); | ||
expect( categories ).toBeUndefined(); | ||
} ); | ||
|
||
it( 'should reject categories with empty slug', () => { | ||
const categories = registerCategory( { slug: '', title: __( 'Custom Blocks' ) } ); | ||
expect( console.error ).toHaveBeenCalledWith( 'The block Category slug must be defined' ); | ||
expect( categories ).toBeUndefined(); | ||
} ); | ||
|
||
it( 'should reject categories with slug not defined', () => { | ||
const categories = registerCategory( { title: __( 'Custom Blocks' ) } ); | ||
expect( console.error ).toHaveBeenCalledWith( 'The block Category slug must be defined' ); | ||
expect( categories ).toBeUndefined(); | ||
} ); | ||
|
||
it( 'should reject categories with invalid slug', () => { | ||
const categories = registerCategory( { slug: 'custom blocks', title: __( 'Custom Blocks' ) } ); | ||
expect( console.error ).toHaveBeenCalledWith( 'The block Category slug must not contain characters which are invalid for urls' ); | ||
expect( categories ).toBeUndefined(); | ||
} ); | ||
|
||
it( 'should reject categories with empty title', () => { | ||
const categories = registerCategory( { slug: 'custom-blocks', title: '' } ); | ||
expect( console.error ).toHaveBeenCalledWith( 'The block Category title must be defined' ); | ||
expect( categories ).toBeUndefined(); | ||
} ); | ||
|
||
it( 'should store the new category', () => { | ||
const categories = registerCategory( { slug: 'custom-blocks', title: 'Custom Blocks' } ); | ||
expect( categories ).toEqual( jasmine.arrayContaining( [ { slug: 'custom-blocks', title: 'Custom Blocks' } ] ) ); | ||
} ); | ||
|
||
it( 'should reject categories already registered', () => { | ||
const categories = registerCategory( { slug: 'custom-blocks', title: 'Custom Blocks' } ); | ||
expect( console.error ).toHaveBeenCalledWith( 'The block Category "custom-blocks" is already registered' ); | ||
expect( categories ).toBeUndefined(); | ||
} ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also possible to achieve a very similar result using this code:
Usage:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, since JavaScript functions are much cheaper than PHP functions we can wrap via composition and keep different logic separate 😄
and without
lodash
it's easy to recreateonce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmsnell thanks for making it even simpler :)