2020-03-27 20:42:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-07-14 20:38:57 +00:00
|
|
|
import { createElement, Fragment } from '@wordpress/element';
|
2020-10-15 12:41:39 +00:00
|
|
|
import { render, waitFor } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2020-03-27 20:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { DismissModal } from '../index.js';
|
|
|
|
|
|
|
|
describe( 'Option Save events in DismissModal', () => {
|
|
|
|
const spyUpdateOptions = jest.fn();
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'Should save permanent dismissal', async () => {
|
|
|
|
const { getByRole } = render(
|
|
|
|
<Fragment>
|
|
|
|
<div id="woocommerce-admin-print-label" />
|
|
|
|
<DismissModal
|
|
|
|
visible={ true }
|
|
|
|
onClose={ jest.fn() }
|
|
|
|
onCloseAll={ jest.fn() }
|
|
|
|
trackElementClicked={ jest.fn() }
|
|
|
|
updateOptions={ spyUpdateOptions }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
2020-03-27 20:42:58 +00:00
|
|
|
);
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.click( getByRole( 'button', { name: "I don't need this" } ) );
|
|
|
|
|
|
|
|
await waitFor( () =>
|
|
|
|
expect( spyUpdateOptions ).toHaveBeenCalledWith( {
|
|
|
|
woocommerce_shipping_dismissed_timestamp: -1,
|
|
|
|
} )
|
|
|
|
);
|
2020-03-27 20:42:58 +00:00
|
|
|
} );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'Should save temporary dismissal', async () => {
|
2020-03-27 20:42:58 +00:00
|
|
|
// Mock Date.now() so a known timestamp will be saved.
|
|
|
|
const mockDate = 123456;
|
|
|
|
const realDateNow = Date.now.bind( global.Date );
|
|
|
|
global.Date.now = jest.fn( () => mockDate );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
const { getByRole } = render(
|
|
|
|
<Fragment>
|
|
|
|
<div id="woocommerce-admin-print-label" />
|
|
|
|
<DismissModal
|
|
|
|
visible={ true }
|
|
|
|
onClose={ jest.fn() }
|
|
|
|
onCloseAll={ jest.fn() }
|
|
|
|
trackElementClicked={ jest.fn() }
|
|
|
|
updateOptions={ spyUpdateOptions }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
userEvent.click( getByRole( 'button', { name: 'Remind me later' } ) );
|
|
|
|
|
|
|
|
await waitFor( () =>
|
|
|
|
expect( spyUpdateOptions ).toHaveBeenCalledWith( {
|
|
|
|
woocommerce_shipping_dismissed_timestamp: mockDate,
|
|
|
|
} )
|
|
|
|
);
|
2020-03-27 20:42:58 +00:00
|
|
|
|
|
|
|
// Restore Date.now().
|
|
|
|
global.Date.now = realDateNow;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'Tracking events in DismissModal', () => {
|
|
|
|
const trackElementClicked = jest.fn();
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
it( 'should record an event when user clicks "I don\'t need this"', async () => {
|
|
|
|
const { getByRole } = render(
|
|
|
|
<Fragment>
|
|
|
|
<div id="woocommerce-admin-print-label" />
|
|
|
|
<DismissModal
|
|
|
|
visible={ true }
|
|
|
|
onClose={ jest.fn() }
|
|
|
|
onCloseAll={ jest.fn() }
|
|
|
|
trackElementClicked={ trackElementClicked }
|
|
|
|
updateOptions={ jest.fn() }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
2020-03-27 20:42:58 +00:00
|
|
|
);
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.click( getByRole( 'button', { name: "I don't need this" } ) );
|
|
|
|
|
|
|
|
await waitFor( () =>
|
|
|
|
expect( trackElementClicked ).toHaveBeenCalledWith(
|
|
|
|
'shipping_banner_dismiss_modal_close_forever'
|
|
|
|
)
|
2020-03-27 20:42:58 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
it( 'should record an event when user clicks "Remind me later"', async () => {
|
|
|
|
const { getByRole } = render(
|
|
|
|
<Fragment>
|
|
|
|
<div id="woocommerce-admin-print-label" />
|
|
|
|
<DismissModal
|
|
|
|
visible={ true }
|
|
|
|
onClose={ jest.fn() }
|
|
|
|
onCloseAll={ jest.fn() }
|
|
|
|
trackElementClicked={ trackElementClicked }
|
|
|
|
updateOptions={ jest.fn() }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
userEvent.click( getByRole( 'button', { name: 'Remind me later' } ) );
|
|
|
|
|
|
|
|
await waitFor( () =>
|
|
|
|
expect( trackElementClicked ).toHaveBeenCalledWith(
|
|
|
|
'shipping_banner_dismiss_modal_remind_me_later'
|
|
|
|
)
|
2020-03-27 20:42:58 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'Dismissing modal', () => {
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'Should hide the banner by clicking permanent dismissal', async () => {
|
|
|
|
const { getByRole, getByTestId } = render(
|
|
|
|
<Fragment>
|
|
|
|
<div
|
|
|
|
id="woocommerce-admin-print-label"
|
|
|
|
data-testid="print-label"
|
|
|
|
/>
|
|
|
|
<DismissModal
|
|
|
|
visible={ true }
|
|
|
|
onClose={ jest.fn() }
|
|
|
|
onCloseAll={ jest.fn() }
|
|
|
|
trackElementClicked={ jest.fn() }
|
|
|
|
updateOptions={ jest.fn() }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
2020-03-27 20:42:58 +00:00
|
|
|
);
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.click( getByRole( 'button', { name: "I don't need this" } ) );
|
2020-03-27 20:42:58 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
await waitFor( () =>
|
|
|
|
expect( getByTestId( 'print-label' ) ).not.toBeVisible()
|
|
|
|
);
|
2020-03-27 20:42:58 +00:00
|
|
|
} );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'Should hide the banner by clicking temporary dismissal', async () => {
|
|
|
|
const { getByRole, getByTestId } = render(
|
|
|
|
<Fragment>
|
|
|
|
<div
|
|
|
|
id="woocommerce-admin-print-label"
|
|
|
|
data-testid="print-label"
|
|
|
|
/>
|
|
|
|
<DismissModal
|
|
|
|
visible={ true }
|
|
|
|
onClose={ jest.fn() }
|
|
|
|
onCloseAll={ jest.fn() }
|
|
|
|
trackElementClicked={ jest.fn() }
|
|
|
|
updateOptions={ jest.fn() }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
userEvent.click( getByRole( 'button', { name: 'Remind me later' } ) );
|
2020-03-27 20:42:58 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
await waitFor( () =>
|
|
|
|
expect( getByTestId( 'print-label' ) ).not.toBeVisible()
|
|
|
|
);
|
2020-03-27 20:42:58 +00:00
|
|
|
} );
|
|
|
|
} );
|