Skip to content

Commit

Permalink
Add tests for styles and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
brookewp committed Dec 7, 2023
1 parent 2e1da1e commit 948a131
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions packages/components/src/custom-select-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ const props = {
{
key: 'color1',
name: 'amber',
className: 'amber-skies',
},
{
key: 'color2',
name: 'aquamarine',
style: {
backgroundColor: 'rgb(127, 255, 212)',
rotate: '13deg',
},
},
],
__nextUnconstrainedWidth: true,
Expand Down Expand Up @@ -109,6 +114,59 @@ describe.each( [
expect( currentSelectedItem ).toHaveTextContent( 'violets' );
} );

it( 'Should apply class only to options that have a className defined', async () => {
const user = userEvent.setup();
const customClass = 'amber-skies';

render( <CustomSelectControl { ...props } /> );

await user.click( screen.getByRole( 'button', { text: 'violets' } ) );

// return an array of items without a className added
const classlessItems = props.options.filter(
( option ) => option.className === undefined
);

// assert against filtered array
classlessItems.map( ( { name } ) =>
expect( screen.getByRole( 'option', { name } ) ).not.toHaveClass(
customClass
)
);

expect( screen.getByRole( 'option', { name: 'amber' } ) ).toHaveClass(
customClass
);
} );

it( 'Should apply styles only to options that have styles defined', async () => {
const user = userEvent.setup();
const customStyles =
'background-color: rgb(127, 255, 212); rotate: 13deg;';

render( <CustomSelectControl { ...props } /> );

await user.click( screen.getByRole( 'button', { text: 'violets' } ) );

// return an array of items without styles added
const unstyledItems = props.options.filter(
( option ) => option.style === undefined
);

// assert against filtered array
unstyledItems.map( ( { name } ) =>
expect( screen.getByRole( 'option', { name } ) ).not.toHaveStyle(
customStyles
)
);

expect(
screen.getByRole( 'option', {
name: 'aquamarine',
} )
).toHaveStyle( customStyles );
} );

it( 'does not show selected hint by default', () => {
render(
<CustomSelectControl
Expand Down

0 comments on commit 948a131

Please sign in to comment.