Skip to content

Commit

Permalink
Notices: Enforce string-y content by cast
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 8, 2018
1 parent c5bc7b4 commit 35837cf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/notices/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- The `createNotice` can now optionally accept a WPNotice object as the sole argument.

### Bug Fixes

- While `createNotice` only explicitly supported content of type `string`, it was not previously enforced. This has been corrected.

## 1.0.2 (2018-11-03)

## 1.0.1 (2018-10-30)
Expand Down
5 changes: 5 additions & 0 deletions packages/notices/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export function* createNotice( statusOrNotice = DEFAULT_STATUS, content, options
actions = [],
} = options;

// The supported value shape of content is currently limited to plain text
// strings. To avoid setting expectation that e.g. a WPElement could be
// supported, cast to a string.
content = String( content );

yield { type: 'SPEAK', message: content };

yield {
Expand Down
21 changes: 21 additions & 0 deletions packages/notices/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ describe( 'actions', () => {
} );
} );

it( 'normalizes content to string', () => {
const result = createNotice( status, <strong>Hello</strong> );

expect( result.next().value ).toMatchObject( {
type: 'SPEAK',
message: expect.any( String ),
} );

expect( result.next().value ).toMatchObject( {
type: 'CREATE_NOTICE',
context: DEFAULT_CONTEXT,
notice: {
status,
content: expect.any( String ),
isDismissible: true,
id: expect.any( String ),
actions: [],
},
} );
} );

it( 'yields actions when options passed', () => {
const context = 'foo';
const options = {
Expand Down

0 comments on commit 35837cf

Please sign in to comment.