From fbeb92572d07b007b5916c04cb191f0ee79a20d0 Mon Sep 17 00:00:00 2001 From: juliajforesti Date: Thu, 14 Sep 2023 16:46:27 -0300 Subject: [PATCH] chore: improve AutoComplete tests and stories --- .../AutoComplete/AutoComplete.spec.tsx | 34 +- .../AutoComplete/AutoComplete.stories.tsx | 119 ++--- .../__snapshots__/AutoComplete.spec.tsx.snap | 425 ++++++++++++++++++ 3 files changed, 496 insertions(+), 82 deletions(-) create mode 100644 packages/fuselage/src/components/AutoComplete/__snapshots__/AutoComplete.spec.tsx.snap diff --git a/packages/fuselage/src/components/AutoComplete/AutoComplete.spec.tsx b/packages/fuselage/src/components/AutoComplete/AutoComplete.spec.tsx index 23be74c4b1..61d95ddecd 100644 --- a/packages/fuselage/src/components/AutoComplete/AutoComplete.spec.tsx +++ b/packages/fuselage/src/components/AutoComplete/AutoComplete.spec.tsx @@ -1,18 +1,34 @@ +import { composeStories } from '@storybook/testing-react'; import { render } from '@testing-library/react'; +import { axe } from 'jest-axe'; import React from 'react'; import { withResizeObserverMock } from 'testing-utils/mocks/withResizeObserverMock'; -import { AutoComplete } from '.'; +import * as stories from './AutoComplete.stories'; + +const testCases = Object.values(composeStories(stories)).map((Story) => [ + Story.storyName || 'Story', + Story, +]); withResizeObserverMock(); -it('renders without crashing', () => { - render( - null} - onChange={jest.fn()} - /> +describe('[AutoComplete Rendering]', () => { + test.each(testCases)( + `renders %s without crashing`, + async (_storyname, Story) => { + const tree = render(); + expect(tree.baseElement).toMatchSnapshot(); + } + ); + + test.each(testCases)( + '%s should have no a11y violations', + async (_storyname, Story) => { + const { container } = render(); + + const results = await axe(container); + expect(results).toHaveNoViolations(); + } ); }); diff --git a/packages/fuselage/src/components/AutoComplete/AutoComplete.stories.tsx b/packages/fuselage/src/components/AutoComplete/AutoComplete.stories.tsx index 0de0c3a79a..ef7b59bed9 100644 --- a/packages/fuselage/src/components/AutoComplete/AutoComplete.stories.tsx +++ b/packages/fuselage/src/components/AutoComplete/AutoComplete.stories.tsx @@ -7,11 +7,11 @@ import { Stories, PRIMARY_STORY, } from '@storybook/addon-docs'; -import type { ComponentMeta } from '@storybook/react'; +import type { ComponentMeta, ComponentStory } from '@storybook/react'; import React, { useState } from 'react'; import { AutoComplete, Box, Chip, Avatar, Option } from '../..'; -import { exampleAvatar } from '../../../.storybook/helpers'; +import { exampleAvatar, DECORATOR_LABEL } from '../../../.storybook/helpers'; export default { title: 'Inputs/AutoComplete', @@ -42,96 +42,69 @@ const options = [ { value: '4', label: 'test4' }, ]; -export const AutoCompleteDefault = () => { +const Template: ComponentStory = ({ + value: defaultValue, + ...args +}) => { const [filter, setFilter] = useState(''); - const [value, setValue] = useState(''); + const [value, setValue] = useState(defaultValue || []); - const handleChangeRooms = (value: string | string[]) => { - setValue(value); - }; + const handleChangeRooms = (value: string | string[]) => setValue(value); return ( ); }; -export const AutoCompleteCustomSelected = () => { - const [filter, setFilter] = useState(''); - const [value, setValue] = useState('1'); - - const handleChangeRooms = (value: string | string[]) => { - setValue(value); - }; +export const Default = Template.bind({}); - return ( - ( - - {label} - - )} - /> - ); +export const CustomSelected = Template.bind({}); +CustomSelected.args = { + value: '1', + renderSelected: ({ selected: { label } }) => ( + + {label} + + ), }; -export const AutoCompleteMultiple = () => { - const [filter, setFilter] = useState(''); - const [value, setValue] = useState(['1', '3']); - - const handleChangeRooms = (value: string | string[]) => { - setValue(value); - }; - - return ( - - ); +export const Multiple = Template.bind({}); +Multiple.args = { + value: ['1', '3'], + multiple: true, }; -export const AutoCompleteMultipleCustomSelected = () => { - const [filter, setFilter] = useState(''); - const [value, setValue] = useState(['1', '3']); - const handleChangeRooms = (value: string | string[]) => { - setValue(value); - }; +export const MultipleCustomSelected = Template.bind({}); +MultipleCustomSelected.args = { + value: ['1', '3'], + multiple: true, + renderSelected: ({ selected: { value, label }, onRemove }) => ( + + + + {' '} + {label} + + + ), +}; - return ( - ( - - - - {' '} - {label} - - - )} - renderItem={({ value, label, ...props }) => ( -