Add tests for experimental add product task

This commit is contained in:
Chi-Hsuan Huang 2022-04-27 15:21:30 +08:00
parent cbfc393c6a
commit 6248683042
2 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,80 @@
/**
* External dependencies
*/
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import { Products } from '../';
import { productTypes } from '../constants';
import { getAdminSetting } from '~/utils/admin-settings';
jest.mock( '@wordpress/data', () => ( {
...jest.requireActual( '@wordpress/data' ),
useSelect: jest.fn(),
} ) );
jest.mock( '~/utils/admin-settings', () => ( {
getAdminSetting: jest.fn(),
} ) );
describe( 'Products', () => {
beforeEach( () => {
jest.clearAllMocks();
} );
it( 'should render all products types without view less button when onboardingData.profile.productType is null', () => {
( getAdminSetting as jest.Mock ).mockImplementation( () => ( {
profile: {
product_types: null,
},
} ) );
const { queryByText } = render( <Products /> );
productTypes.forEach( ( { title } ) => {
expect( queryByText( title ) ).toBeInTheDocument();
} );
expect(
queryByText( 'View more product types' )
).not.toBeInTheDocument();
} );
it( 'should render digital products type with view more button', () => {
( getAdminSetting as jest.Mock ).mockImplementation( () => ( {
profile: {
product_types: [ 'downloads' ],
},
} ) );
const { queryByText, queryByRole } = render( <Products /> );
expect( queryByText( 'Digital product' ) ).toBeInTheDocument();
expect( queryByRole( 'menu' )?.childElementCount ).toBe( 1 );
expect( queryByText( 'View more product types' ) ).toBeInTheDocument();
} );
it( 'should render all products type when clicking view more button', async () => {
( getAdminSetting as jest.Mock ).mockImplementation( () => ( {
profile: {
product_types: [ 'downloads' ],
},
} ) );
const { queryByText, getByRole, queryByRole } = render( <Products /> );
expect( queryByText( 'View more product types' ) ).toBeInTheDocument();
userEvent.click(
getByRole( 'button', { name: 'View more product types' } )
);
await waitFor( () =>
expect( queryByRole( 'menu' )?.childElementCount ).toBe(
productTypes.length
)
);
expect( queryByText( 'View less product types' ) ).toBeInTheDocument();
} );
} );

View File

@ -0,0 +1,41 @@
/**
* Internal dependencies
*/
import { getProductTypes, getSurfacedProductKeys } from '../utils';
import { productTypes, onboardingProductTypesToSurfaced } from '../constants';
describe( 'getProductTypes', () => {
it( 'should return the product types', () => {
expect( getProductTypes() ).toEqual( productTypes );
} );
it( 'should return the product types without excluded items', () => {
expect(
getProductTypes( [ 'external', 'digital' ] ).map( ( p ) => p.key )
).toEqual( [ 'physical', 'variable', 'subscription', 'grouped' ] );
} );
} );
describe( 'getSurfacedProductKeys', () => {
test.each( [
{
selectedTypes: [ 'physical' ],
expected: onboardingProductTypesToSurfaced.physical,
},
{
selectedTypes: [ 'physical', 'downloads' ],
expected: onboardingProductTypesToSurfaced[ 'downloads,physical' ],
},
{
selectedTypes: [],
expected: productTypes.map( ( p ) => p.key ),
},
] )(
'should return expected surfaced product keys when onboarding product type contains $selected',
( { selectedTypes, expected } ) => {
expect( getSurfacedProductKeys( selectedTypes ) ).toEqual(
expected
);
}
);
} );