Add test for stack

This commit is contained in:
Chi-Hsuan Huang 2022-04-29 13:04:05 +08:00
parent 95a319b36d
commit f25383fc15
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
/**
* External dependencies
*/
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import Stack from '../stack';
import { productTypes } from '../constants';
describe( 'Stack', () => {
it( 'should render stack with given product type and two links', () => {
const { queryByText, queryAllByRole } = render(
<Stack
onClickLoadSampleProduct={ () => {} }
items={ [
{
...productTypes[ 0 ],
onClick: () => {},
},
] }
/>
);
expect( queryByText( productTypes[ 0 ].title ) ).toBeInTheDocument();
expect( queryAllByRole( 'link' ) ).toHaveLength( 2 );
} );
it( 'should call onClickLoadSampleProduct when the "Load Sample Products" link is clicked', async () => {
const onClickLoadSampleProduct = jest.fn();
const { getByRole } = render(
<Stack
onClickLoadSampleProduct={ onClickLoadSampleProduct }
items={ [
{
...productTypes[ 0 ],
onClick: () => {},
},
] }
/>
);
userEvent.click(
getByRole( 'link', { name: 'Load Sample Products' } )
);
await waitFor( () => expect( onClickLoadSampleProduct ).toBeCalled() );
} );
} );