From 95a319b36dc510244bfcb094ef818d0d37b4b103 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Fri, 29 Apr 2022 12:52:41 +0800 Subject: [PATCH] Add tests for load sample products --- .../test/load-sample-product-modal.tsx | 16 ++++++++++ .../experimental-products/test/index.tsx | 30 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 plugins/woocommerce-admin/client/tasks/fills/components/test/load-sample-product-modal.tsx diff --git a/plugins/woocommerce-admin/client/tasks/fills/components/test/load-sample-product-modal.tsx b/plugins/woocommerce-admin/client/tasks/fills/components/test/load-sample-product-modal.tsx new file mode 100644 index 00000000000..f1d24d66194 --- /dev/null +++ b/plugins/woocommerce-admin/client/tasks/fills/components/test/load-sample-product-modal.tsx @@ -0,0 +1,16 @@ +/** + * External dependencies + */ +import { render } from '@testing-library/react'; + +/** + * Internal dependencies + */ +import LoadSampleProductModal from '../load-sample-product-modal'; + +describe( 'LoadSampleProductModal', () => { + it( 'should render LoadSampleProductModal', () => { + const { queryByText } = render( ); + expect( queryByText( 'Loading sample products' ) ).toBeInTheDocument(); + } ); +} ); diff --git a/plugins/woocommerce-admin/client/tasks/fills/experimental-products/test/index.tsx b/plugins/woocommerce-admin/client/tasks/fills/experimental-products/test/index.tsx index d5a8815fb85..a1e44f684e8 100644 --- a/plugins/woocommerce-admin/client/tasks/fills/experimental-products/test/index.tsx +++ b/plugins/woocommerce-admin/client/tasks/fills/experimental-products/test/index.tsx @@ -20,6 +20,13 @@ jest.mock( '~/utils/admin-settings', () => ( { getAdminSetting: jest.fn(), } ) ); +global.fetch = jest.fn().mockImplementation( () => + Promise.resolve( { + json: () => Promise.resolve( {} ), + status: 200, + } ) +); + describe( 'Products', () => { beforeEach( () => { jest.clearAllMocks(); @@ -75,4 +82,27 @@ describe( 'Products', () => { expect( queryByText( 'View less product types' ) ).toBeInTheDocument(); } ); + + it( 'should send a request to load sample products when the link is clicked', async () => { + const fetchMock = jest.spyOn( global, 'fetch' ); + const { queryByText, getByRole } = render( ); + + expect( queryByText( 'Load Sample Products' ) ).toBeInTheDocument(); + + userEvent.click( + getByRole( 'link', { name: 'Load Sample Products' } ) + ); + + await waitFor( () => + expect( fetchMock ).toHaveBeenCalledWith( + '/wc-admin/onboarding/tasks/import_sample_products?_locale=user', + { + body: undefined, + credentials: 'include', + headers: { Accept: 'application/json, */*;q=0.1' }, + method: 'POST', + } + ) + ); + } ); } );