|
| 1 | +import React from 'react'; |
| 2 | +import { screen, render, act } from '@testing-library/react'; |
| 3 | +import apiFetch from '@wordpress/api-fetch'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import OptimizedCheckoutNotice from '..'; |
| 6 | + |
| 7 | +jest.mock( '@wordpress/api-fetch' ); |
| 8 | + |
| 9 | +describe( 'OptimizedCheckoutNotice', () => { |
| 10 | + const globalValues = global.wc_stripe_settings_params; |
| 11 | + beforeEach( () => { |
| 12 | + apiFetch.mockImplementation( |
| 13 | + jest.fn( () => Promise.resolve( { data: {} } ) ) |
| 14 | + ); |
| 15 | + global.wc_stripe_settings_params = { |
| 16 | + ...globalValues, |
| 17 | + show_optimized_checkout_notice: true, |
| 18 | + }; |
| 19 | + } ); |
| 20 | + |
| 21 | + afterEach( () => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + global.wc_stripe_settings_params = globalValues; |
| 24 | + } ); |
| 25 | + |
| 26 | + it( 'should render the notice when OC is enabled', () => { |
| 27 | + render( <OptimizedCheckoutNotice isOCEnabled={ true } /> ); |
| 28 | + |
| 29 | + const noticeText = screen.queryAllByText( |
| 30 | + "You're using Stripe's Optimized Checkout Suite to dynamically display the most relevant payment methods you've enabled to each customer." |
| 31 | + )?.[ 0 ]; |
| 32 | + expect( noticeText ).toBeInTheDocument(); |
| 33 | + } ); |
| 34 | + |
| 35 | + it( 'should make an API call to dismiss the banner on button click', async () => { |
| 36 | + const dismissNoticeMock = jest.fn( () => |
| 37 | + Promise.resolve( { data: {} } ) |
| 38 | + ); |
| 39 | + apiFetch.mockImplementation( dismissNoticeMock ); |
| 40 | + |
| 41 | + render( <OptimizedCheckoutNotice isOCEnabled={ true } /> ); |
| 42 | + |
| 43 | + const dismissButton = screen.queryByRole( 'button', { |
| 44 | + 'aria-label': 'Dismiss the notice', |
| 45 | + } ); |
| 46 | + expect( dismissButton ).toBeInTheDocument(); |
| 47 | + await act( async () => { |
| 48 | + await userEvent.click( dismissButton ); |
| 49 | + } ); |
| 50 | + expect( dismissNoticeMock ).toHaveBeenCalled(); |
| 51 | + } ); |
| 52 | + |
| 53 | + it( 'should not render the notice when OC is disabled', () => { |
| 54 | + const { container } = render( |
| 55 | + <OptimizedCheckoutNotice isOCEnabled={ false } /> |
| 56 | + ); |
| 57 | + |
| 58 | + expect( container.firstChild ).toBeNull(); |
| 59 | + } ); |
| 60 | + |
| 61 | + it( 'should not render the notice when `show_optimized_checkout_notice` is false', () => { |
| 62 | + global.wc_stripe_settings_params = { |
| 63 | + ...globalValues, |
| 64 | + show_optimized_checkout_notice: false, |
| 65 | + }; |
| 66 | + |
| 67 | + const { container } = render( |
| 68 | + <OptimizedCheckoutNotice isOCEnabled={ true } /> |
| 69 | + ); |
| 70 | + |
| 71 | + expect( container.firstChild ).toBeNull(); |
| 72 | + } ); |
| 73 | +} ); |
0 commit comments