From 8762a3baefcfc3b8d0eef521426bc891a7d14f7b Mon Sep 17 00:00:00 2001 From: Jean-Marc Millet Date: Wed, 9 Oct 2024 17:03:18 +0200 Subject: [PATCH] Add tests for accordion Add story and playground to guideline --- .../components/accordion/accordion.test.tsx | 69 +++++++++++++++++++ stories/Accordion/accordion.guideline.mdx | 8 ++- stories/Accordion/accordion.stories.tsx | 38 +++++++++- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/lib/components/accordion/accordion.test.tsx diff --git a/src/lib/components/accordion/accordion.test.tsx b/src/lib/components/accordion/accordion.test.tsx new file mode 100644 index 0000000000..0d834d1ece --- /dev/null +++ b/src/lib/components/accordion/accordion.test.tsx @@ -0,0 +1,69 @@ +import { render, screen, waitFor } from '@testing-library/react'; +import React, { useState } from 'react'; +import Accordion from './accordion.component'; +import userEvent from '@testing-library/user-event'; +import { QueryClient, QueryClientProvider } from 'react-query'; + +// export function mockOffsetSize(width: number, height: number) { +// const originalFunction = window.getComputedStyle; +// const spyGetComputedStyle = jest.spyOn(window, 'getComputedStyle'); +// spyGetComputedStyle.mockImplementation((elt, _) => { +// const originalStyle = originalFunction(elt); +// originalStyle.fontSize = '14px'; +// originalStyle.paddingLeft = '0px'; +// originalStyle.paddingRight = '0px'; +// originalStyle.paddingTop = '0px'; +// originalStyle.paddingBottom = '0px'; +// return originalStyle; +// }); +// } + +describe('Accordion', () => { + // beforeAll(() => { + // mockOffsetSize(1000, 1000); + // }); + const selectors = { + accordionToggle: () => screen.getByRole('button'), + accordionContainer: () => screen.getByRole('region'), + accordionContent: () => screen.queryByText(/Test content/i), + }; + const renderAccordion = () => { + const queryClient = new QueryClient(); + render( + + +
Test content
+
+
, + ); + }; + it('should render the Accordion component with title and content', () => { + renderAccordion(); + + const accordionToggle = selectors.accordionToggle(); + expect(accordionToggle).toBeInTheDocument(); + const accordionContent = selectors.accordionContent(); + expect(accordionContent).toBeInTheDocument(); + }); + + it('should toggle the content when clicking on the accordion header', () => { + renderAccordion(); + const accordionToggle = selectors.accordionToggle(); + const accordionContent = selectors.accordionContent(); + expect(accordionContent).not.toBeVisible(); + userEvent.click(accordionToggle); + expect(accordionContent).toBeVisible(); + }); + + it('should toggle the content when pressing the enter key or space key on the accordion header', () => { + renderAccordion(); + const accordionToggle = selectors.accordionToggle(); + const accordionContent = selectors.accordionContent(); + expect(accordionContent).not.toBeVisible(); + accordionToggle.focus(); + userEvent.keyboard('{enter}'); + expect(accordionContent).toBeVisible(); + userEvent.keyboard('{space}'); + expect(accordionContent).not.toBeVisible(); + }); +}); diff --git a/stories/Accordion/accordion.guideline.mdx b/stories/Accordion/accordion.guideline.mdx index 2ec0522513..878dcf239c 100644 --- a/stories/Accordion/accordion.guideline.mdx +++ b/stories/Accordion/accordion.guideline.mdx @@ -16,5 +16,11 @@ import * as Stories from './accordion.stories'; # Accordion -Accordions are used to toggle the visibility of content. +Accordions are used to toggle the visibility of content. It is used to hide non essential information or to reduce the amount of information displayed on the screen. + +## Playground + + + + diff --git a/stories/Accordion/accordion.stories.tsx b/stories/Accordion/accordion.stories.tsx index 966511ce69..e577ae4539 100644 --- a/stories/Accordion/accordion.stories.tsx +++ b/stories/Accordion/accordion.stories.tsx @@ -16,13 +16,49 @@ const meta: Meta = { title: 'Accordion title', children: ( -
This the subtitle explaining the content of the accordion.
This is the content of the accordion.
+
), }, + argTypes: { + children: { + control: { disable: true }, + description: 'Content of the accordion', + table: { + type: { summary: 'React.ReactNode' }, + }, + }, + title: { + control: { type: 'text' }, + description: 'Title of the accordion', + table: { + type: { summary: 'string' }, + }, + }, + style: { + control: { disable: true }, + description: 'Use this to style the accordion content container', + table: { type: { summary: 'CSSProperties' } }, + }, + id: { + control: { disable: true }, + table: { type: { summary: 'string' } }, + description: 'Unique id for the accordion content container', + }, + }, }; export default meta; export const Playground: AccordionStory = {}; + +export const Stacked: AccordionStory = { + render: (args) => ( + + + + + + ), +};