From 35837cf623061eb413ff558e99c1921d931030b3 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 8 Nov 2018 11:28:04 -0500 Subject: [PATCH] Notices: Enforce string-y content by cast --- packages/notices/CHANGELOG.md | 4 ++++ packages/notices/src/store/actions.js | 5 +++++ packages/notices/src/store/test/actions.js | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/packages/notices/CHANGELOG.md b/packages/notices/CHANGELOG.md index 4eaea38b2b4ddd..2c0866a0e002d3 100644 --- a/packages/notices/CHANGELOG.md +++ b/packages/notices/CHANGELOG.md @@ -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) diff --git a/packages/notices/src/store/actions.js b/packages/notices/src/store/actions.js index cf967d45a9cd14..5bbe001b4d4ca8 100644 --- a/packages/notices/src/store/actions.js +++ b/packages/notices/src/store/actions.js @@ -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 { diff --git a/packages/notices/src/store/test/actions.js b/packages/notices/src/store/test/actions.js index 6e25fdb5e5dcd0..4f5515362682bd 100644 --- a/packages/notices/src/store/test/actions.js +++ b/packages/notices/src/store/test/actions.js @@ -38,6 +38,27 @@ describe( 'actions', () => { } ); } ); + it( 'normalizes content to string', () => { + const result = createNotice( status, Hello ); + + 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 = {