/* jest */ import React from 'react' import {renderHook} from '@testing-library/react-hooks' import {render, fireEvent, act} from '@testing-library/react' import userEvent from '@testing-library/user-event' import {Accordion, Section, Trigger, Panel, Close, useControls} from './index' const silenceErrors = (test: (...args: any[]) => void) => () => { const originalError = console.error console.error = jest.fn() // eslint-disable-next-line jest/expect-expect test() console.error = originalError } describe('', () => { it( 'should throw if it has no child Sections', silenceErrors(() => { expect(() => render(
) ).toThrowErrorMatchingSnapshot() }) ) it( 'should throw if it has no open sections and allowAllClosed is false', silenceErrors(() => { expect(() => render(
) ).toThrowErrorMatchingSnapshot() }) ) it('should not throw if it has no open sections and allowAllClosed is true', () => { expect( render(
).asFragment() ).toMatchSnapshot() }) it( 'should throw on multiple defaultOpen if allowMultipleOpen is false', silenceErrors(() => { expect(() => render(
).asFragment() ).toThrowErrorMatchingSnapshot() }) ) it( 'should throw on multiple open if allowMultipleOpen is false', silenceErrors(() => { expect(() => render(
).asFragment() ).toThrowErrorMatchingSnapshot() }) ) it('should render singular defaultOpen', () => { expect( render(
).asFragment() ).toMatchSnapshot('index=0 closed, index=1 open') }) it('should render singular defaultOpen w/ array', () => { expect( render(
).asFragment() ).toMatchSnapshot('index=0 closed, index=1 open') }) it('should render multiple defaultOpen', () => { expect( render(
).asFragment() ).toMatchSnapshot('index=0 open, index=1 open') }) it('should render singular open', () => { expect( render(
).asFragment() ).toMatchSnapshot('index=0 closed, index=1 open') }) it('should render singular open w/ array', () => { expect( render(
).asFragment() ).toMatchSnapshot('index=0 closed, index=1 open') }) it('should render multiple open', () => { expect( render(
).asFragment() ).toMatchSnapshot('index=0 open, index=1 open') }) it('should allow custom indexes and keys on sections', () => { expect( render(
).asFragment() ).toMatchSnapshot('index=1 open, index=0 closed') }) it('should call onChange handler when open sections change', () => { const called = [] const result = render(
) expect(called.length).toBe(0) userEvent.click(result.getByTestId('btn')) // expect(called[0]).toBe(1) }) it('should call onChange handler when multiple open sections change', () => { // wtf dude // const cb = jest.fn() const called = [] const result = render( { called.push(value) }} >
) expect(called.length).toBe(0) userEvent.click(result.getByTestId('btn')) // expect(called[0]).toEqual([0, 1]) }) }) describe(`
`, () => { it(`should provide context to child function`, () => { let value render(
{(context) => { value = context return
}}
) expect(value).toMatchSnapshot() }) it(`should not update state if disabled`, () => { let close const {getByTestId} = render(
{(context) => { close = context.close return (
) }}
) expect(getByTestId('btn').getAttribute('aria-disabled')).toBe('true') expect(getByTestId('btn').getAttribute('aria-expanded')).toBe('true') act(close) expect(getByTestId('btn').getAttribute('aria-expanded')).toBe('true') }) it('should not open if disabled', () => { let open const result = render(
{(context) => { open = context.open return (
) }}
) expect(result.getByTestId('btn').getAttribute('aria-expanded')).toBe( 'false' ) act(open) expect(result.getByTestId('btn').getAttribute('aria-expanded')).toBe( 'false' ) }) it(`should not close if allowAllClosed is false`, () => { let close const {getByTestId} = render(
{(context) => { close = context.close return (
) }}
) expect(getByTestId('btn').getAttribute('aria-expanded')).toBe('true') act(close) expect(getByTestId('btn').getAttribute('aria-expanded')).toBe('true') }) it(`should close if allowAllClosed is true`, () => { let close const {getByTestId} = render(
{(context) => { close = context.close return (
) }}
) expect(getByTestId('btn').getAttribute('aria-expanded')).toBe('true') act(close) expect(getByTestId('btn').getAttribute('aria-expanded')).toBe('false') }) it(`should receive index from parent Accordion`, () => { const indexes: Set = new Set() render(
{(context) => { indexes.add(context.index) return
}}
{(context) => { indexes.add(context.index) return
}}
) expect([...indexes]).toEqual([0, 1]) }) it(`should override default ID`, () => { expect( render(
).asFragment() ).toMatchSnapshot('aria-controls=custom, id=custom') }) }) describe(``, () => { it(`should apply open/closed classes`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('open') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('closed') }) it(`should apply open/closed styles`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('display: block') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('display: none') }) it(`should apply custom class`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('open + custom') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('closed + custom') }) it(`should apply custom styles`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('display: block, height: 300') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('display: none, height: 300') }) it(`should fire user onClick definition`, () => { const cb = jest.fn() const {getByTestId} = render(
) expect(cb).not.toBeCalled() userEvent.click(getByTestId('btn')) expect(cb).toBeCalledTimes(1) }) it(`should focus next trigger on down arrow`, () => { const focus1 = jest.fn() const focus2 = jest.fn() const {getByTestId} = render(
) fireEvent.keyDown(getByTestId('btn-1'), {which: 40}) expect(focus2).toBeCalledTimes(1) fireEvent.keyDown(getByTestId('btn-2'), {which: 40}) expect(focus1).toBeCalledTimes(1) }) it(`should focus prev trigger on up arrow`, () => { const focus1 = jest.fn() const focus2 = jest.fn() const {getByTestId} = render(
) fireEvent.keyDown(getByTestId('btn-1'), {which: 38}) expect(focus2).toBeCalledTimes(1) fireEvent.keyDown(getByTestId('btn-2'), {which: 38}) expect(focus1).toBeCalledTimes(1) }) it(`should focus first trigger on home key, last on end key`, () => { const focus1 = jest.fn() const focus2 = jest.fn() const {getByTestId} = render(
) fireEvent.keyDown(getByTestId('btn-1'), {which: 36}) expect(focus1).toBeCalledTimes(1) fireEvent.keyDown(getByTestId('btn-2'), {which: 35}) expect(focus2).toBeCalledTimes(1) }) }) describe(``, () => { it(`should apply open/closed classes`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('open') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('closed') }) it(`should apply open/closed styles`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('display: block') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('display: none') }) it(`should apply custom class`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('open + custom') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('closed + custom') }) it(`should apply custom styles`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('display: block, height: 300') fireEvent.mouseDown(getByTestId('btn')) userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('display: none, height: 300') }) it(`should close on escape`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('closed initially') userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('open') fireEvent.keyDown(getByTestId('panel'), { key: 'Escape', which: 27, }) expect(asFragment()).toMatchSnapshot('closed') }) it(`should close on escape if closeOnEscape is false`, () => { const {getByTestId, asFragment} = render(
) expect(asFragment()).toMatchSnapshot('closed initially') userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('open') fireEvent.keyDown(getByTestId('panel'), { key: 'Escape', which: 27, }) expect(asFragment()).toMatchSnapshot('open [despite escape]') }) }) describe(``, () => { it(`should close its parent section if allowAllClosed is true`, () => { const {asFragment, getByTestId} = render(
) expect(asFragment()).toMatchSnapshot('visible') userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('hidden') }) it(`should not close its parent section if allowAllClosed is false`, () => { const {asFragment, getByTestId} = render(
) expect(asFragment()).toMatchSnapshot('visible') userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('visible [despite click]') }) it(`should fire child onClick`, () => { const cb = jest.fn() const {asFragment, getByTestId} = render(
) expect(asFragment()).toMatchSnapshot('visible') userEvent.click(getByTestId('btn')) expect(asFragment()).toMatchSnapshot('hidden') expect(cb).toBeCalledTimes(1) }) }) describe(`useControls()`, () => { it('should have open, close, toggle keys', () => { const {result} = renderHook(() => useControls(), { wrapper: ({children}) => (
), }) expect(Object.keys(result.current)).toStrictEqual([ 'open', 'close', 'toggle', ]) }) })