Skip to content

Commit

Permalink
Change reducer and tests to account for notice type
Browse files Browse the repository at this point in the history
  • Loading branch information
opr committed Sep 16, 2022
1 parent aca88eb commit 207db72
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/notices/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const notices = onSubKey( 'context' )( ( state = [], action ) => {
return state.filter( ( { id } ) => id !== action.id );

case 'REMOVE_ALL_NOTICES':
return [];
return state.filter( ( { type } ) => type !== action.noticeType );
}

return state;
Expand Down
35 changes: 34 additions & 1 deletion packages/notices/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe( 'reducer', () => {
} );

let state = reducer( original, action );
state = reducer( state, removeAllNotices( 'bar' ) );
state = reducer( state, removeAllNotices( 'default', 'bar' ) );

expect( state ).toEqual( {
bar: [],
Expand All @@ -217,4 +217,37 @@ describe( 'reducer', () => {
],
} );
} );

it( 'should remove all notices of a given type', () => {
let action = createNotice( 'error', 'save error', {
id: 'global-error',
} );
const original = deepFreeze( reducer( undefined, action ) );

action = createNotice( 'success', 'successfully saved', {
type: 'snackbar',
id: 'snackbar-success',
} );

let state = reducer( original, action );
state = reducer( state, removeAllNotices( 'default' ) );

expect( state ).toEqual( {
[ DEFAULT_CONTEXT ]: [
{
id: 'snackbar-success',
content: 'successfully saved',
spokenMessage: 'successfully saved',
__unstableHTML: undefined,
status: 'success',
isDismissible: true,
actions: [],
type: 'snackbar',
icon: null,
explicitDismiss: false,
onDismiss: undefined,
},
],
} );
} );
} );

0 comments on commit 207db72

Please sign in to comment.