UNPKG

4.29 kBJavaScriptView Raw
1import React from 'react';
2import { render, fireEvent, waitFor, screen } from '@testing-library/react';
3import '@testing-library/jest-dom';
4import { Accordion, AccordionItem, AccordionHeader, AccordionBody } from '..';
5import { testForCustomClass, testForCustomTag } from '../testUtils';
6
7beforeEach(() => {
8 jest.useFakeTimers();
9});
10
11afterEach(() => {
12 jest.runOnlyPendingTimers();
13 jest.useRealTimers();
14});
15
16describe('Accordion', () => {
17 const accordionItems = [
18 <AccordionItem data-testid="item-1" key="1">
19 <AccordionHeader targetId="1" data-testid="head-1">
20 Accordion Item 1
21 </AccordionHeader>
22 <AccordionBody accordionId="1" data-testid="body-1">
23 This is the body of first item.{' '}
24 </AccordionBody>
25 </AccordionItem>,
26 <AccordionItem data-testid="item-2" key="2">
27 <AccordionHeader targetId="2" data-testid="head-2">
28 Accordion Item 2
29 </AccordionHeader>
30 <AccordionBody accordionId="2" data-testid="body-2">
31 This is the body of second item.{' '}
32 </AccordionBody>
33 </AccordionItem>,
34 <AccordionItem data-testid="item-3" key="3">
35 <AccordionHeader targetId="3" data-testid="head-3">
36 Accordion Item 3
37 </AccordionHeader>
38 <AccordionBody accordionId="3" data-testid="body-3">
39 This is the body of third item.{' '}
40 </AccordionBody>
41 </AccordionItem>,
42 ];
43
44 it('should render a div with "accordion" class', () => {
45 render(
46 <Accordion open="this accordion" toggle={() => {}} data-testid="test" />,
47 );
48 const node = screen.getByTestId('test');
49 expect(node.tagName).toMatch(/div/i);
50 expect(node).toHaveClass('accordion');
51 });
52
53 it('should render flush prop', () => {
54 render(
55 <Accordion
56 open="this accordion"
57 flush
58 toggle={() => {}}
59 data-testid="test"
60 />,
61 );
62 const node = screen.getByTestId('test');
63 expect(node).toHaveClass('accordion');
64 expect(node).toHaveClass('accordion-flush');
65 });
66
67 it('should render custom tag', () => {
68 testForCustomTag(Accordion, {
69 open: 'this accordion',
70 flush: true,
71 toggle: () => {},
72 });
73 });
74
75 it('should render custom classes', () => {
76 testForCustomClass(Accordion, {
77 open: 'this accordion',
78 toggle: () => {},
79 });
80 });
81
82 it('should have second item showing and others collapsed', () => {
83 render(
84 <Accordion open="2" toggle={() => {}} data-testid="accordion">
85 {accordionItems}
86 </Accordion>,
87 );
88
89 expect(screen.getByTestId('body-1')).not.toHaveClass('show');
90 expect(screen.getByTestId('body-2')).toHaveClass('show');
91 expect(screen.getByTestId('body-3')).not.toHaveClass('show');
92 });
93
94 it('should call toggle with clicked item id', () => {
95 const mockFn = jest.fn(() => {});
96 render(
97 <Accordion open="1" toggle={mockFn} data-testid="accordion">
98 {accordionItems}
99 </Accordion>,
100 );
101
102 fireEvent.click(screen.getByText(/accordion item 2/i));
103
104 expect(mockFn.mock.calls[0][0]).toBe('2');
105 });
106
107 it('should collapse current item and open new item on prop change', async () => {
108 const { rerender } = render(
109 <Accordion open="1" toggle={() => {}} data-testid="accordion">
110 {accordionItems}
111 </Accordion>,
112 );
113
114 expect(screen.getByTestId('body-1')).toHaveClass('show');
115
116 rerender(
117 <Accordion open="2" toggle={() => {}} data-testid="accordion">
118 {accordionItems}
119 </Accordion>,
120 );
121
122 expect(
123 screen.getByTestId('item-1').querySelector('.accordion-collapse'),
124 ).toHaveClass('collapsing');
125
126 await waitFor(() => {
127 expect(
128 screen.getByTestId('item-1').querySelector('.accordion-collapse'),
129 ).not.toHaveClass('show');
130 expect(
131 screen.getByTestId('item-2').querySelector('.accordion-collapse'),
132 ).toHaveClass('show');
133 });
134 });
135
136 it('should allow multiple items to open', async () => {
137 render(
138 <Accordion open={['1', '2']} toggle={() => {}} data-testid="accordion">
139 {accordionItems}
140 </Accordion>,
141 );
142
143 await waitFor(() => {
144 expect(screen.getByTestId('body-1')).toHaveClass('show');
145 expect(screen.getByTestId('body-2')).toHaveClass('show');
146 });
147 });
148});