|
| 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 { vi } from 'vitest' |
| 26 | +import type { MockInstance } from 'vitest' |
| 27 | +import { render, screen, waitFor } from '@testing-library/react' |
| 28 | +import userEvent from '@testing-library/user-event' |
| 29 | +import '@testing-library/jest-dom' |
| 30 | + |
| 31 | +// eslint-disable-next-line no-restricted-imports |
| 32 | +import { generateA11yTests } from '@instructure/ui-scripts/lib/test/generateA11yTests' |
| 33 | +import { runAxeCheck } from '@instructure/ui-axe-check' |
| 34 | +import { SmallViewportModeWrapper } from '../../utils/exampleHelpers' |
| 35 | +import { elevateIcon } from '../../utils/exampleSvgFiles' |
| 36 | +import { TopNavBarBrand } from '../index' |
| 37 | +import TopNavBarBrandExamples from '../__examples__/TopNavBarBrand.examples' |
| 38 | + |
| 39 | +describe('<TopNavBarBrand />', () => { |
| 40 | + let consoleWarningMock: ReturnType<typeof vi.spyOn> |
| 41 | + let consoleErrorMock: ReturnType<typeof vi.spyOn> |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + // Mocking console to prevent test output pollution |
| 45 | + consoleWarningMock = vi |
| 46 | + .spyOn(console, 'warn') |
| 47 | + .mockImplementation(() => {}) as MockInstance |
| 48 | + consoleErrorMock = vi |
| 49 | + .spyOn(console, 'error') |
| 50 | + .mockImplementation(() => {}) as MockInstance |
| 51 | + }) |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + consoleWarningMock.mockRestore() |
| 55 | + consoleErrorMock.mockRestore() |
| 56 | + }) |
| 57 | + |
| 58 | + it('should render', async () => { |
| 59 | + const { container } = render( |
| 60 | + <TopNavBarBrand screenReaderLabel="Brand name" /> |
| 61 | + ) |
| 62 | + const component = container.querySelector('[class$="-topNavBarBrand"]') |
| 63 | + |
| 64 | + expect(component).toBeInTheDocument() |
| 65 | + }) |
| 66 | + |
| 67 | + describe('elementRef prop should return a ref to the root element', () => { |
| 68 | + it('should return root element', async () => { |
| 69 | + const elementRef = vi.fn() |
| 70 | + const { container } = render( |
| 71 | + <TopNavBarBrand |
| 72 | + screenReaderLabel="Brand name" |
| 73 | + elementRef={elementRef} |
| 74 | + /> |
| 75 | + ) |
| 76 | + const component = container.querySelector('[class$="-topNavBarBrand"]') |
| 77 | + |
| 78 | + expect(elementRef).toHaveBeenCalledWith(component) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe('screenReaderLabel prop', () => { |
| 83 | + it('should render label for SR', async () => { |
| 84 | + const { container } = render( |
| 85 | + <TopNavBarBrand |
| 86 | + screenReaderLabel="Test label" |
| 87 | + renderIcon={elevateIcon} |
| 88 | + /> |
| 89 | + ) |
| 90 | + const screenReaderLabel = container.querySelector( |
| 91 | + '[class$="-screenReaderContent"]' |
| 92 | + ) |
| 93 | + |
| 94 | + expect(screenReaderLabel).toHaveTextContent('Test label') |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe('renderIcon prop', () => { |
| 99 | + it('should render name in desktop mode', async () => { |
| 100 | + const { container } = render( |
| 101 | + <TopNavBarBrand |
| 102 | + screenReaderLabel="Test label" |
| 103 | + renderIcon={elevateIcon} |
| 104 | + /> |
| 105 | + ) |
| 106 | + const iconContainer = container.querySelector('[class$="iconContainer"]') |
| 107 | + const icon = container.querySelector('svg') |
| 108 | + |
| 109 | + expect(iconContainer).toBeVisible() |
| 110 | + expect(icon).toBeVisible() |
| 111 | + expect(icon).toHaveAttribute('id', 'elevateIcon') |
| 112 | + }) |
| 113 | + |
| 114 | + it('should not render name in smallViewport mode', async () => { |
| 115 | + const { container } = render( |
| 116 | + <SmallViewportModeWrapper> |
| 117 | + <TopNavBarBrand |
| 118 | + screenReaderLabel="Test label" |
| 119 | + renderIcon={elevateIcon} |
| 120 | + /> |
| 121 | + </SmallViewportModeWrapper> |
| 122 | + ) |
| 123 | + const iconContainer = container.querySelector('[class$="iconContainer"]') |
| 124 | + |
| 125 | + expect(iconContainer).not.toBeInTheDocument() |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + describe('iconBackground prop', () => { |
| 130 | + it('should be visible in desktop mode', async () => { |
| 131 | + const { container } = render( |
| 132 | + <TopNavBarBrand |
| 133 | + screenReaderLabel="Test label" |
| 134 | + renderIcon={elevateIcon} |
| 135 | + iconBackground="blue" |
| 136 | + /> |
| 137 | + ) |
| 138 | + const iconContainer = container.querySelector('[class$="iconContainer"]')! |
| 139 | + |
| 140 | + expect(getComputedStyle(iconContainer).backgroundColor).toBe( |
| 141 | + 'rgb(0, 0, 255)' |
| 142 | + ) |
| 143 | + }) |
| 144 | + |
| 145 | + it('should not be visible in smallViewport mode', async () => { |
| 146 | + const { container } = render( |
| 147 | + <SmallViewportModeWrapper> |
| 148 | + <TopNavBarBrand |
| 149 | + screenReaderLabel="Test label" |
| 150 | + renderIcon={elevateIcon} |
| 151 | + iconBackground="blue" |
| 152 | + /> |
| 153 | + </SmallViewportModeWrapper> |
| 154 | + ) |
| 155 | + const iconContainer = container.querySelector('[class$="iconContainer"]') |
| 156 | + |
| 157 | + expect(iconContainer).not.toBeInTheDocument() |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + describe('href prop', () => { |
| 162 | + it('should render component as link', async () => { |
| 163 | + render( |
| 164 | + <TopNavBarBrand |
| 165 | + screenReaderLabel="Test label" |
| 166 | + renderIcon={elevateIcon} |
| 167 | + href="/#TestHref" |
| 168 | + /> |
| 169 | + ) |
| 170 | + const link = screen.getByRole('link') |
| 171 | + |
| 172 | + expect(link).toBeInTheDocument() |
| 173 | + expect(link.tagName).toBe('A') |
| 174 | + expect(link).toHaveAttribute('href', '/#TestHref') |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + describe('onClick prop', () => { |
| 179 | + it('should render component button', async () => { |
| 180 | + const onClick = vi.fn() |
| 181 | + render( |
| 182 | + <TopNavBarBrand |
| 183 | + screenReaderLabel="Test label" |
| 184 | + renderIcon={elevateIcon} |
| 185 | + href={undefined} |
| 186 | + onClick={onClick} |
| 187 | + /> |
| 188 | + ) |
| 189 | + const button = screen.getByRole('button') |
| 190 | + |
| 191 | + userEvent.click(button) |
| 192 | + |
| 193 | + await waitFor(() => { |
| 194 | + expect(button.tagName).toBe('BUTTON') |
| 195 | + expect(onClick).toHaveBeenCalled() |
| 196 | + }) |
| 197 | + }) |
| 198 | + }) |
| 199 | + |
| 200 | + describe('as prop', () => { |
| 201 | + it('should render component as passed element', async () => { |
| 202 | + const onClick = vi.fn() |
| 203 | + render( |
| 204 | + <TopNavBarBrand |
| 205 | + screenReaderLabel="Test label" |
| 206 | + renderIcon={elevateIcon} |
| 207 | + href="/#TestHref" |
| 208 | + onClick={onClick} |
| 209 | + as="button" |
| 210 | + /> |
| 211 | + ) |
| 212 | + const button = screen.getByRole('button') |
| 213 | + |
| 214 | + userEvent.click(button) |
| 215 | + |
| 216 | + await waitFor(() => { |
| 217 | + expect(button.tagName).toBe('BUTTON') |
| 218 | + expect(onClick).toHaveBeenCalled() |
| 219 | + }) |
| 220 | + }) |
| 221 | + }) |
| 222 | + |
| 223 | + it('should be accessible', async () => { |
| 224 | + const { container } = render( |
| 225 | + <TopNavBarBrand screenReaderLabel="Brand name" /> |
| 226 | + ) |
| 227 | + const axeCheck = await runAxeCheck(container) |
| 228 | + |
| 229 | + expect(axeCheck).toBe(true) |
| 230 | + }) |
| 231 | + |
| 232 | + describe('with generated examples', () => { |
| 233 | + const generatedComponents = generateA11yTests( |
| 234 | + TopNavBarBrand, |
| 235 | + TopNavBarBrandExamples |
| 236 | + ) |
| 237 | + |
| 238 | + it.each(generatedComponents)( |
| 239 | + 'should be accessible with example: $description', |
| 240 | + async ({ content }) => { |
| 241 | + const { container } = render(content) |
| 242 | + const axeCheck = await runAxeCheck(container) |
| 243 | + expect(axeCheck).toBe(true) |
| 244 | + } |
| 245 | + ) |
| 246 | + }) |
| 247 | +}) |
0 commit comments