Skip to content

Commit

Permalink
test: add missing tests on Accordion
Browse files Browse the repository at this point in the history
Signed-off-by: Théo Mesnil <[email protected]>
  • Loading branch information
theo-mesnil committed Jun 21, 2023
1 parent 4a6bc78 commit eebac49
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"pre-build": "yarn icons:collect",
"release": "yarn lerna version --conventional-commits --no-private",
"start": "yarn docs:dev",
"test": "node --expose-gc ./node_modules/.bin/jest ./packages --logHeapUsage",
"test": "yarn jest packages/* --logHeapUsage",
"watch": "onchange 'packages/**/*.ts*' -e '**/dist/**' -- node -r esm scripts/watch.js {{changed}}",
"webfont:build": "node -r esm scripts/webfont-build.js --force && yarn build:packages --scope @welcome-ui/icons.font",
"webfont:deploy": "./scripts/webfont-deploy.sh",
Expand Down
12 changes: 8 additions & 4 deletions packages/Accordion/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ export interface AccordionOptions {
export type AccordionProps = CreateWuiProps<'div', AccordionOptions>

export const Accordion = forwardRef<'div', AccordionProps>(
({ children, icon = <RightIcon />, title, store, ...rest }, ref) => {
({ children, icon = <RightIcon />, title, store, dataTestId, ...rest }, ref) => {
const isOpen = store?.useState('open')

return (
<S.Accordion ref={ref} {...rest}>
<S.Disclosure store={store}>
<S.Accordion data-testid={dataTestId} ref={ref} {...rest}>
<S.Disclosure data-testid={dataTestId ? `${dataTestId}-title` : undefined} store={store}>
{title}
<S.Icon isOpen={isOpen}>{cloneElement(icon, { size: 'sm' })}</S.Icon>
</S.Disclosure>
<S.Content isOpen={isOpen} store={store}>
<S.Content
data-testid={dataTestId ? `${dataTestId}-content` : undefined}
isOpen={isOpen}
store={store}
>
<AnimateHeight animateOpacity duration={200} height={isOpen ? 'auto' : 0}>
{children}
</AnimateHeight>
Expand Down
38 changes: 33 additions & 5 deletions packages/Accordion/tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'

import { render, renderHook } from '../../../utils/tests'
import { fireEvent, render, renderHook, screen } from '../../../utils/tests'
import { Accordion, useAccordionStore } from '../src'

const content = 'content'
Expand All @@ -12,13 +12,41 @@ describe('<Accordion>', () => {
result: { current: store },
} = renderHook(() => useAccordionStore())

const { container } = render(
<Accordion store={store} title={title}>
render(
<Accordion dataTestId="accordion" store={store} title={title}>
{content}
</Accordion>
)

expect(container).toHaveTextContent(content)
expect(container).toHaveTextContent(title)
const button = screen.getByTestId('accordion-title')
const children = screen.getByTestId('accordion-content')

expect(button).toHaveTextContent(title)
expect(button).toHaveAttribute('aria-expanded', 'false')
expect(children).toHaveTextContent(content)
expect(children).toHaveAttribute('hidden')

fireEvent.click(button)

expect(button).toHaveAttribute('aria-expanded', 'true')
expect(children).not.toHaveAttribute('hidden')
})

it('should render correctly on open at start', () => {
const {
result: { current: store },
} = renderHook(() => useAccordionStore({ defaultOpen: true }))

render(
<Accordion dataTestId="accordion" store={store} title={title}>
{content}
</Accordion>
)

const button = screen.getByTestId('accordion-title')
const children = screen.getByTestId('accordion-content')

expect(button).toHaveAttribute('aria-expanded', 'true')
expect(children).not.toHaveAttribute('hidden')
})
})

0 comments on commit eebac49

Please sign in to comment.