This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a function to save a new field value
Also, add a unit test for that. @todo: test the 'happy path' of that function.
- Loading branch information
Showing
9 changed files
with
139 additions
and
63 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
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,40 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { dispatch, select } = wp.data; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import getBlockFromContent from './getBlockFromContent'; | ||
|
||
/** | ||
* Parses the block from the post content into an object. | ||
* | ||
* @param {string} fieldSlug The slug of the field. | ||
* @param {string} key The key of the field value to change, like 'label'. | ||
* @param {string} value The new field value. | ||
* @return {boolean} Whether saving the field value succeeded. | ||
*/ | ||
const saveFieldValue = ( fieldSlug, key, value ) => { | ||
if ( ! fieldSlug ) { | ||
return false; | ||
} | ||
|
||
const content = select( 'core/editor' ).getEditedPostContent(); | ||
const block = getBlockFromContent( content ) || {}; | ||
if ( ! block.hasOwnProperty( 'fields' ) ) { | ||
block.fields = {}; | ||
} | ||
|
||
if ( ! block.fields.hasOwnProperty( fieldSlug ) || 'object' !== typeof block.fields[ fieldSlug ] ) { | ||
return false; | ||
} | ||
|
||
block.fields[ fieldSlug ][ key ] = value; | ||
dispatch( 'core/editor' ).editPost( { content: JSON.stringify( [ block ] ) } ); | ||
dispatch( 'core/block-editor' ).resetBlocks( [] ); // Prevent the block editor from overwriting the saved content. | ||
return true; | ||
}; | ||
|
||
export default saveFieldValue; |
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 saveFieldValue from '../saveFieldValue'; | ||
|
||
describe( 'saveFieldValue', () => { | ||
it( 'should not update the post if the field argument is an empty string', () => { | ||
saveFieldValue( '', 'example-email', 'you@example.com' ); | ||
expect( global.wp.data.dispatch( 'core/editor' ).editPost ).toHaveBeenCalledTimes( 0 ); | ||
} ); | ||
|
||
it( 'should not attempt to update the post if there is no fields index', () => { | ||
saveFieldValue( 'real-field-slug', 'example-email', 'you@example.com' ); | ||
expect( global.wp.data.dispatch( 'core/editor' ).editPost ).toHaveBeenCalledTimes( 0 ); | ||
} ); | ||
|
||
it( 'should not update the post if the field does not exist yet', () => { | ||
saveFieldValue( 'real-field-slug', 'example-email', 'you@example.com' ); | ||
expect( global.wp.data.dispatch( 'core/editor' ).editPost ).toHaveBeenCalledTimes( 0 ); | ||
} ); | ||
} ); |
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 |
---|---|---|
@@ -1,5 +1,19 @@ | ||
// Mock wp object. | ||
|
||
global.wp = { | ||
blocks: { registerBlockType: jest.fn() }, | ||
blocks: { | ||
registerBlockType: jest.fn(), | ||
}, | ||
data: { | ||
dispatch: ( store ) => { | ||
if ( 'core/editor' === store ) { | ||
return { editPost: jest.fn() }; | ||
} | ||
}, | ||
select: ( store ) => { | ||
if ( 'core/editor' === store ) { | ||
return { getEditedPostContent: jest.fn() }; | ||
} | ||
}, | ||
} | ||
}; |
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