Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ class NoticesList extends Component {
//and from the old component. When all notices are moved to redux store, this component
//needs to be updated.
noticesList = noticesList.concat(
this.props.storeNotices.map( function ( notice, index ) {
this.props.storeNotices.map( function ( notice ) {
return (
<SimpleNotice
key={ 'notice-' + index }
key={ notice.instanceId }
status={ notice.status }
duration={ notice.duration || null }
showDismiss={ notice.showDismiss }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function removeNotice( noticeId ) {
export function createNotice( status, text, options = {} ) {
const notice = {
noticeId: options.id || uniqueId(),
// React key. Callers reuse noticeId for a replacement notice, which must still remount so its dismiss timer starts.
instanceId: uniqueId( 'notice-instance-' ),
duration: options.duration,
showDismiss: typeof options.showDismiss === 'boolean' ? options.showDismiss : true,
isPersistent: options.isPersistent || false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { createStore } from 'redux';
import { act, render, screen } from 'test/test-utils';
import NoticesList from '../index.jsx';
import { createNotice, removeNotice } from '../state/notices/actions';
import reducer from '../state/notices/reducer';

/**
* Mount the notices list against a real notices store.
*
* @return {object} The store, so the test can dispatch into it.
*/
function mountNotices() {
const store = createStore( reducer );
render( <NoticesList />, { store } );
return store;
}

describe( 'NoticesList', () => {
beforeEach( () => {
jest.useFakeTimers();
} );

afterEach( () => {
jest.useRealTimers();
} );

it( 'dismisses a notice that replaced an earlier one under the same id', () => {
const store = mountNotices();

act( () => {
store.dispatch( createNotice( 'is-info', 'Testing your connection…', { id: 'connection' } ) );
store.dispatch( createNotice( 'is-info', 'Activating Image CDN…', { id: 'module-toggle' } ) );
} );
expect( screen.getByText( 'Activating Image CDN…' ) ).toBeInTheDocument();

act( () => {
store.dispatch( removeNotice( 'module-toggle' ) );
store.dispatch(
createNotice( 'is-success', 'Image CDN has been activated.', {
id: 'module-toggle',
duration: 2000,
} )
);
} );
expect( screen.getByText( 'Image CDN has been activated.' ) ).toBeInTheDocument();

act( () => {
jest.advanceTimersByTime( 2000 );
} );
expect( screen.queryByText( 'Image CDN has been activated.' ) ).not.toBeInTheDocument();
expect( screen.getByText( 'Testing your connection…' ) ).toBeInTheDocument();
} );

it( 'dismisses a notice that replaced an earlier one under a different id', () => {
const store = mountNotices();

act( () => {
store.dispatch( createNotice( 'is-info', 'Testing your connection…', { id: 'connection' } ) );
store.dispatch( createNotice( 'is-info', 'Updating settings…', { id: 'setting-update' } ) );
} );

act( () => {
store.dispatch( removeNotice( 'setting-update' ) );
store.dispatch(
createNotice( 'is-success', 'Updated settings.', {
id: 'setting-update-success',
duration: 2000,
} )
);
} );
expect( screen.getByText( 'Updated settings.' ) ).toBeInTheDocument();

act( () => {
jest.advanceTimersByTime( 2000 );
} );
expect( screen.queryByText( 'Updated settings.' ) ).not.toBeInTheDocument();
expect( screen.getByText( 'Testing your connection…' ) ).toBeInTheDocument();
} );

it( 'dismisses a notice added while another one is already on screen', () => {
const store = mountNotices();

act( () => {
store.dispatch( createNotice( 'is-info', 'Testing your connection…', { id: 'connection' } ) );
} );

act( () => {
store.dispatch(
createNotice( 'is-success', 'Site is verified.', { id: 'verify', duration: 2000 } )
);
} );
expect( screen.getByText( 'Site is verified.' ) ).toBeInTheDocument();

act( () => {
jest.advanceTimersByTime( 2000 );
} );
expect( screen.queryByText( 'Site is verified.' ) ).not.toBeInTheDocument();
expect( screen.getByText( 'Testing your connection…' ) ).toBeInTheDocument();
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const AllowList = class extends Component {
this.props.removeNotice( 'module-setting-update' );
this.props.createNotice( 'is-success', __( 'Updated Settings.', 'jetpack' ), {
id: 'module-setting-update-success',
duration: 2000,
} );
} )
.catch( error => {
Expand Down
1 change: 1 addition & 0 deletions projects/plugins/jetpack/_inc/client/security/waf.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const Waf = class extends Component {
this.props.removeNotice( 'module-setting-update' );
this.props.createNotice( 'is-success', __( 'Updated Settings.', 'jetpack' ), {
id: 'module-setting-update-success',
duration: 2000,
} );
} )
.catch( error => {
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/fix-toast-auto-dismiss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Settings: Dismiss success notices automatically instead of leaving them on screen.
Loading