-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the category of FSE blocks from legacy to the updated ones
Context: #43198. - Also add a helper function for FSE blocks that can be used to check if old category names exist in the store and fall-backs to an older version(s) otherwise. This function should be used when the updated category is a completely new category being introduced by a new version of Gutenberg. You then pass this new category + the older categorie(s) that will be tried in sequence if the previous one was not found. - Tweaked the FSE Jest configuration to support TypeScript.
- Loading branch information
1 parent
ac79d2b
commit b0742d3
Showing
20 changed files
with
107 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const babelJest = require( 'babel-jest' ); | ||
|
||
module.exports = babelJest.createTransformer( { | ||
presets: [ '@wordpress/babel-preset-default', '@babel/preset-typescript' ], | ||
} ); |
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
33 changes: 33 additions & 0 deletions
33
apps/full-site-editing/full-site-editing-plugin/block-helpers/block-helpers.test.ts
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,33 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getCategoryWithFallbacks } from '.'; | ||
|
||
jest.mock( '@wordpress/blocks', () => ( { | ||
getCategories: () => [ { slug: 'foobar' }, { slug: 'barfoo' } ], | ||
} ) ); | ||
|
||
describe( 'getCategoryWithFallbacks', () => { | ||
describe( 'single category passed', () => { | ||
it( 'returns the category if it exists', () => { | ||
expect( getCategoryWithFallbacks( 'foobar' ) ).toBe( 'foobar' ); | ||
} ); | ||
it( 'throws an error if it does not exist', () => { | ||
expect( () => getCategoryWithFallbacks( 'nah' ) ).toThrow( /nah/ ); | ||
} ); | ||
} ); | ||
|
||
describe( 'multiple categories are passed', () => { | ||
it( 'throws an error if none of the categories exist', () => { | ||
expect( () => getCategoryWithFallbacks( 'nah', 'meh', 'wut', 'foo' ) ).toThrow( | ||
/nah,meh,wut,foo/ | ||
); | ||
} ); | ||
|
||
it( 'ignores all unexisting categories until it finds the *first one* that exists, then returns it', () => { | ||
expect( getCategoryWithFallbacks( 'foobar', 'meh', 'barfoo' ) ).toBe( 'foobar' ); | ||
expect( getCategoryWithFallbacks( 'nah', 'foobar', 'barfoo', 'foo' ) ).toBe( 'foobar' ); | ||
expect( getCategoryWithFallbacks( 'nah', 'meh', 'foobar' ) ).toBe( 'foobar' ); | ||
} ); | ||
} ); | ||
} ); |
35 changes: 35 additions & 0 deletions
35
apps/full-site-editing/full-site-editing-plugin/block-helpers/index.ts
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,35 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { getCategories } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Accepts an array of category names and returns the first one that | ||
* exists in the categories returned by `getCategories`. This allows | ||
* for a "graceful degradation" strategy to category names, where | ||
* we just add the new category name as the first item in the array | ||
* argument, and leave the old ones for environments where they still | ||
* exist and are used. | ||
* | ||
* @example | ||
* // Prefer passing the new category first in the array, followed by | ||
* // older fallback categories. Considering the 'new' category is | ||
* // registered: | ||
* getCategoryWithFallbacks( 'new', 'old', 'older' ); | ||
* // => 'new' | ||
* | ||
* @param {string[]} requestedCategories - an array of categories. | ||
* @returns {string} the first category name found. | ||
* @throws {Error} if the no categories could be found. | ||
*/ | ||
export function getCategoryWithFallbacks( ...requestedCategories: string[] ): string { | ||
const knownCategories = getCategories(); | ||
for ( const requestedCategory of requestedCategories ) { | ||
if ( knownCategories.some( ( { slug } ) => slug === requestedCategory ) ) { | ||
return requestedCategory; | ||
} | ||
} | ||
throw new Error( | ||
`Could not find a category from the provided list: ${ requestedCategories.join( ',' ) }` | ||
); | ||
} |
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
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
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 |
---|---|---|
|
@@ -28,5 +28,5 @@ addFilter( | |
|
||
registerBlockType( blockName, { | ||
...settings, | ||
category: 'layout', | ||
category: 'widgets', | ||
} ); |
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
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
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