|
| 1 | +/* |
| 2 | +Copyright 2019 Iguazio Systems Ltd. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License") with |
| 5 | +an addition restriction as set forth herein. You may not use this |
| 6 | +file except in compliance with the License. You may obtain a copy of |
| 7 | +the License at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +implied. See the License for the specific language governing |
| 13 | +permissions and limitations under the License. |
| 14 | +
|
| 15 | +In addition, you may not use the software for any purposes that are |
| 16 | +illegal under applicable law, and the grant of the foregoing license |
| 17 | +under the Apache 2.0 license is conditioned upon your compliance with |
| 18 | +such restriction. |
| 19 | +*/ |
| 20 | +import React from 'react' |
| 21 | +import { render, screen, fireEvent } from '@testing-library/react' |
| 22 | +import { vi, describe, it, expect, afterEach } from 'vitest' |
| 23 | +import Accordion from './Accordion' |
| 24 | + |
| 25 | +vi.mock('igz-controls/components', () => ({ |
| 26 | + RoundedIcon: (props) => <button data-testid="accordion-btn" {...props} /> |
| 27 | +})) |
| 28 | + |
| 29 | +describe('Accordion Component', () => { |
| 30 | + const defaultChildren = <div data-testid="content">Accordion Content</div> |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + vi.clearAllMocks() |
| 34 | + }) |
| 35 | + |
| 36 | + it('renders closed by default', () => { |
| 37 | + render(<Accordion>{defaultChildren}</Accordion>) |
| 38 | + |
| 39 | + const accordion = screen.getByTestId('accordion') |
| 40 | + |
| 41 | + expect(accordion).toHaveClass('accordion__container') |
| 42 | + expect(accordion).not.toHaveClass('open') |
| 43 | + expect(screen.getByText('Accordion Content')).toBeInTheDocument() |
| 44 | + }) |
| 45 | + |
| 46 | + it('renders opened when openByDefault is true', () => { |
| 47 | + render(<Accordion openByDefault>{defaultChildren}</Accordion>) |
| 48 | + |
| 49 | + const accordion = screen.getByTestId('accordion') |
| 50 | + expect(accordion).toHaveClass('open') |
| 51 | + }) |
| 52 | + |
| 53 | + it('toggles state when clicking the container if no icon is provided', () => { |
| 54 | + render(<Accordion>{defaultChildren}</Accordion>) |
| 55 | + |
| 56 | + const accordion = screen.getByTestId('accordion') |
| 57 | + |
| 58 | + fireEvent.click(accordion) |
| 59 | + expect(accordion).toHaveClass('open') |
| 60 | + |
| 61 | + fireEvent.click(accordion) |
| 62 | + expect(accordion).not.toHaveClass('open') |
| 63 | + }) |
| 64 | + |
| 65 | + it('does not toggle state when clicking container if icon is provided', () => { |
| 66 | + render(<Accordion icon={<span>Icon</span>}>{defaultChildren}</Accordion>) |
| 67 | + |
| 68 | + const accordion = screen.getByTestId('accordion') |
| 69 | + |
| 70 | + fireEvent.click(accordion) |
| 71 | + expect(accordion).not.toHaveClass('open') |
| 72 | + }) |
| 73 | + |
| 74 | + it('toggles state when clicking the icon', () => { |
| 75 | + render( |
| 76 | + <Accordion icon={<span>MY ICON</span>} iconClassName="custom-icon-class"> |
| 77 | + {defaultChildren} |
| 78 | + </Accordion> |
| 79 | + ) |
| 80 | + |
| 81 | + const accordion = screen.getByTestId('accordion') |
| 82 | + const iconBtn = screen.getByTestId('accordion-btn') |
| 83 | + |
| 84 | + expect(iconBtn).toHaveClass('accordion__icon') |
| 85 | + expect(iconBtn).toHaveClass('custom-icon-class') |
| 86 | + |
| 87 | + fireEvent.click(iconBtn) |
| 88 | + expect(accordion).toHaveClass('open') |
| 89 | + expect(iconBtn).toHaveClass('open') |
| 90 | + |
| 91 | + fireEvent.click(iconBtn) |
| 92 | + expect(accordion).not.toHaveClass('open') |
| 93 | + }) |
| 94 | + |
| 95 | + it('does not close if alwaysOpened prop is true', () => { |
| 96 | + render(<Accordion alwaysOpened openByDefault>{defaultChildren}</Accordion>) |
| 97 | + |
| 98 | + const accordion = screen.getByTestId('accordion') |
| 99 | + expect(accordion).toHaveClass('open') |
| 100 | + |
| 101 | + fireEvent.click(accordion) |
| 102 | + |
| 103 | + expect(accordion).toHaveClass('open') |
| 104 | + }) |
| 105 | + |
| 106 | + it('handles closeOnBlur (clicking outside)', () => { |
| 107 | + const mockCloseOnBlur = vi.fn() |
| 108 | + |
| 109 | + render( |
| 110 | + <Accordion |
| 111 | + openByDefault={true} |
| 112 | + closeOnBlur={mockCloseOnBlur} |
| 113 | + > |
| 114 | + {defaultChildren} |
| 115 | + </Accordion> |
| 116 | + ) |
| 117 | + |
| 118 | + const accordion = screen.getByTestId('accordion') |
| 119 | + expect(accordion).toHaveClass('open') |
| 120 | + |
| 121 | + fireEvent.click(document.body) |
| 122 | + |
| 123 | + expect(accordion).not.toHaveClass('open') |
| 124 | + expect(mockCloseOnBlur).toHaveBeenCalledTimes(1) |
| 125 | + }) |
| 126 | +}) |
0 commit comments