|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import React from 'react' |
| 26 | +import { render, screen, waitFor } from '@testing-library/react' |
| 27 | +import { runAxeCheck } from '@instructure/ui-axe-check' |
| 28 | +import userEvent from '@testing-library/user-event' |
| 29 | +import { vi } from 'vitest' |
| 30 | +import type { MockInstance } from 'vitest' |
| 31 | + |
| 32 | +import '@testing-library/jest-dom' |
| 33 | +import { ToggleDetails } from '../index' |
| 34 | + |
| 35 | +describe('<ToggleDetails />', () => { |
| 36 | + let consoleWarningMock: ReturnType<typeof vi.spyOn> |
| 37 | + let consoleErrorMock: ReturnType<typeof vi.spyOn> |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + // Mocking console to prevent test output pollution |
| 41 | + consoleWarningMock = vi |
| 42 | + .spyOn(console, 'warn') |
| 43 | + .mockImplementation(() => {}) as MockInstance |
| 44 | + consoleErrorMock = vi |
| 45 | + .spyOn(console, 'error') |
| 46 | + .mockImplementation(() => {}) as MockInstance |
| 47 | + }) |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + consoleWarningMock.mockRestore() |
| 51 | + consoleErrorMock.mockRestore() |
| 52 | + }) |
| 53 | + |
| 54 | + it('should hide its content', async () => { |
| 55 | + render(<ToggleDetails summary="Click me">Content</ToggleDetails>) |
| 56 | + const content = screen.queryByText('Content') |
| 57 | + |
| 58 | + expect(content).not.toBeInTheDocument() |
| 59 | + }) |
| 60 | + |
| 61 | + it('should place the icon after the summary when prop is set', async () => { |
| 62 | + const { container } = render( |
| 63 | + <ToggleDetails iconPosition="end" summary="Click me"> |
| 64 | + Content |
| 65 | + </ToggleDetails> |
| 66 | + ) |
| 67 | + const summary = container.querySelector( |
| 68 | + '[class$="-toggleDetails__summaryText"]' |
| 69 | + ) |
| 70 | + const icon = container.querySelector('[class$="-toggleDetails__icon"]') |
| 71 | + |
| 72 | + expect(icon?.previousSibling).toBe(summary) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should have an aria-controls attribute', async () => { |
| 76 | + const { container } = render( |
| 77 | + <ToggleDetails summary="Click me">Details</ToggleDetails> |
| 78 | + ) |
| 79 | + |
| 80 | + const toggle = container.querySelector('[class$="-toggleDetails__toggle"]') |
| 81 | + const content = container.querySelector( |
| 82 | + '[class$="-toggleDetails__details"]' |
| 83 | + ) |
| 84 | + |
| 85 | + expect(toggle).toHaveAttribute('aria-controls', content?.id) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should have an aria-expanded attribute', async () => { |
| 89 | + const { container } = render( |
| 90 | + <ToggleDetails summary="Click me">Details</ToggleDetails> |
| 91 | + ) |
| 92 | + const toggle = container.querySelector('[class$="-toggleDetails__toggle"]') |
| 93 | + |
| 94 | + expect(toggle).toHaveAttribute('aria-expanded', 'false') |
| 95 | + }) |
| 96 | + |
| 97 | + it('should not have aria attributes when it has no children', async () => { |
| 98 | + render( |
| 99 | + <ToggleDetails summary="Click me" data-testid="td__0"></ToggleDetails> |
| 100 | + ) |
| 101 | + const toggle = screen.getByTestId('td__0') |
| 102 | + |
| 103 | + expect(toggle).not.toHaveAttribute('aria-controls') |
| 104 | + expect(toggle).not.toHaveAttribute('aria-expanded') |
| 105 | + }) |
| 106 | + |
| 107 | + it('should toggle on click events', async () => { |
| 108 | + const { container } = render( |
| 109 | + <ToggleDetails summary="Click me">Details</ToggleDetails> |
| 110 | + ) |
| 111 | + const toggle = container.querySelector('[class$="-toggleDetails__toggle"]') |
| 112 | + |
| 113 | + expect(toggle).toHaveAttribute('aria-expanded', 'false') |
| 114 | + |
| 115 | + await userEvent.click(screen.getByText('Click me')) |
| 116 | + |
| 117 | + await waitFor(() => { |
| 118 | + expect(toggle).toHaveAttribute('aria-expanded', 'true') |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + it('should call onToggle on click events', async () => { |
| 123 | + const onToggle = vi.fn() |
| 124 | + |
| 125 | + render( |
| 126 | + <ToggleDetails summary="Click me" expanded={false} onToggle={onToggle}> |
| 127 | + Details |
| 128 | + </ToggleDetails> |
| 129 | + ) |
| 130 | + |
| 131 | + await userEvent.click(screen.getByText('Click me')) |
| 132 | + |
| 133 | + await waitFor(() => { |
| 134 | + const args = onToggle.mock.calls[0] |
| 135 | + |
| 136 | + expect(onToggle).toHaveBeenCalledTimes(1) |
| 137 | + expect(args[0].type).toBe('click') |
| 138 | + expect(args[1]).toBe(true) |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('should call onToggle on click events when it has no children', async () => { |
| 143 | + const onToggle = vi.fn() |
| 144 | + |
| 145 | + render( |
| 146 | + <ToggleDetails |
| 147 | + summary="Click me" |
| 148 | + expanded={false} |
| 149 | + onToggle={onToggle} |
| 150 | + data-testid="td__1" |
| 151 | + ></ToggleDetails> |
| 152 | + ) |
| 153 | + const toggle = screen.getByTestId('td__1') |
| 154 | + await userEvent.click(toggle) |
| 155 | + |
| 156 | + await waitFor(() => { |
| 157 | + const args = onToggle.mock.calls[0] |
| 158 | + |
| 159 | + expect(onToggle).toHaveBeenCalledTimes(1) |
| 160 | + expect(args[0].type).toBe('click') |
| 161 | + expect(args[1]).toBe(true) |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + it('should be initialized by defaultExpanded prop', async () => { |
| 166 | + const { container } = render( |
| 167 | + <ToggleDetails summary="Click me" defaultExpanded> |
| 168 | + Content |
| 169 | + </ToggleDetails> |
| 170 | + ) |
| 171 | + const toggle = container.querySelector('[class$="-toggleDetails__toggle"]') |
| 172 | + const content = container.querySelector( |
| 173 | + '[class$="-toggleDetails__details"]' |
| 174 | + ) |
| 175 | + |
| 176 | + expect(toggle).toHaveAttribute('aria-expanded', 'true') |
| 177 | + expect(content).toHaveTextContent('Content') |
| 178 | + }) |
| 179 | + |
| 180 | + it('should meet a11y standards', async () => { |
| 181 | + const { container } = render( |
| 182 | + <ToggleDetails summary="Click me">Content</ToggleDetails> |
| 183 | + ) |
| 184 | + const axeCheck = await runAxeCheck(container) |
| 185 | + |
| 186 | + expect(axeCheck).toBe(true) |
| 187 | + }) |
| 188 | + |
| 189 | + it('focuses with the focus helper', async () => { |
| 190 | + let toggleRef: any |
| 191 | + |
| 192 | + const { container } = render( |
| 193 | + <ToggleDetails |
| 194 | + summary="Click me" |
| 195 | + ref={(el) => { |
| 196 | + toggleRef = el |
| 197 | + }} |
| 198 | + > |
| 199 | + Content |
| 200 | + </ToggleDetails> |
| 201 | + ) |
| 202 | + const toggle = container.querySelector('[class$="-toggleDetails__toggle"]') |
| 203 | + |
| 204 | + expect(document.activeElement).not.toBe(toggle) |
| 205 | + |
| 206 | + toggleRef?.focus() |
| 207 | + |
| 208 | + expect(document.activeElement).toBe(toggle) |
| 209 | + }) |
| 210 | +}) |
0 commit comments