|
| 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 } from '@testing-library/react' |
| 27 | +import '@testing-library/jest-dom' |
| 28 | + |
| 29 | +// eslint-disable-next-line no-restricted-imports |
| 30 | +import { generateA11yTests } from '@instructure/ui-scripts/lib/test/generateA11yTests' |
| 31 | +import { runAxeCheck } from '@instructure/ui-axe-check' |
| 32 | +import { InlineSVG } from '../index' |
| 33 | +import InlineSVGExamples from '../__examples__/InlineSVG.examples' |
| 34 | + |
| 35 | +const SVG_SRC = `<svg><circle cx="50" cy="50" r="40" /></svg>` |
| 36 | + |
| 37 | +describe('<InlineSVG />', () => { |
| 38 | + it('should render', () => { |
| 39 | + const { container } = render(<InlineSVG src={SVG_SRC} />) |
| 40 | + const svg = container.querySelector('svg') |
| 41 | + |
| 42 | + expect(svg).toBeInTheDocument() |
| 43 | + }) |
| 44 | + |
| 45 | + it('should have role "presentation" when no title is provided', () => { |
| 46 | + const { container } = render(<InlineSVG src={SVG_SRC} />) |
| 47 | + const svg = container.querySelector('svg') |
| 48 | + |
| 49 | + expect(svg).toHaveAttribute('role', 'presentation') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should have role "img" when a title is provided', () => { |
| 53 | + const { container } = render( |
| 54 | + <InlineSVG src={SVG_SRC} title="testIconTitle" /> |
| 55 | + ) |
| 56 | + const svg = container.querySelector('svg') |
| 57 | + |
| 58 | + expect(svg).toHaveAttribute('role', 'img') |
| 59 | + }) |
| 60 | + |
| 61 | + it('should add a group with a role "presentation', () => { |
| 62 | + const { container } = render( |
| 63 | + <InlineSVG src={SVG_SRC} title="testIconTitle" /> |
| 64 | + ) |
| 65 | + const group = container.querySelector('g') |
| 66 | + |
| 67 | + expect(group).toHaveAttribute('role', 'presentation') |
| 68 | + }) |
| 69 | + |
| 70 | + it('should not render title when no title prop is provided', () => { |
| 71 | + const { container } = render(<InlineSVG src={SVG_SRC} />) |
| 72 | + const title = container.querySelector('title') |
| 73 | + |
| 74 | + expect(title).not.toBeInTheDocument() |
| 75 | + }) |
| 76 | + |
| 77 | + it('should render title when title prop is provided', () => { |
| 78 | + const { container } = render(<InlineSVG src={SVG_SRC} title="Test Title" />) |
| 79 | + const title = container.querySelector('title') |
| 80 | + |
| 81 | + expect(title).toBeInTheDocument() |
| 82 | + expect(title).toHaveTextContent('Test Title') |
| 83 | + }) |
| 84 | + |
| 85 | + it('should not render description when no description prop is provided', () => { |
| 86 | + const { container } = render(<InlineSVG src={SVG_SRC} />) |
| 87 | + const description = container.querySelector('desc') |
| 88 | + |
| 89 | + expect(description).not.toBeInTheDocument() |
| 90 | + }) |
| 91 | + |
| 92 | + it('should render description when description prop is provided', () => { |
| 93 | + const { container } = render( |
| 94 | + <InlineSVG src={SVG_SRC} description="testIconDesc" /> |
| 95 | + ) |
| 96 | + const description = container.querySelector('desc') |
| 97 | + |
| 98 | + expect(description).toBeInTheDocument() |
| 99 | + expect(description).toHaveTextContent('testIconDesc') |
| 100 | + }) |
| 101 | + |
| 102 | + it('should produce null for "labelledBy" when no title or desc are provided', () => { |
| 103 | + const { container } = render(<InlineSVG src={SVG_SRC} />) |
| 104 | + const svg = container.querySelector('svg') |
| 105 | + |
| 106 | + expect(svg).toBeInTheDocument() |
| 107 | + expect(svg).not.toHaveAttribute('aria-labelledby') |
| 108 | + }) |
| 109 | + |
| 110 | + it('should properly join ids when both title and desc attributes are provided', () => { |
| 111 | + const { container } = render( |
| 112 | + <InlineSVG |
| 113 | + src={SVG_SRC} |
| 114 | + title="testIconTitle" |
| 115 | + description="testIconDescription" |
| 116 | + /> |
| 117 | + ) |
| 118 | + const svg = container.querySelector('svg')! |
| 119 | + const ids = svg.getAttribute('aria-labelledby')!.split(' ') |
| 120 | + |
| 121 | + expect(ids.length).toBe(2) |
| 122 | + }) |
| 123 | + |
| 124 | + it('should set focusable to false by default', () => { |
| 125 | + const { container } = render(<InlineSVG src={SVG_SRC} />) |
| 126 | + const svg = container.querySelector('svg') |
| 127 | + |
| 128 | + expect(svg).toHaveAttribute('focusable', 'false') |
| 129 | + }) |
| 130 | + |
| 131 | + it('should allow focusable to be overridden', () => { |
| 132 | + const { container } = render(<InlineSVG src={SVG_SRC} focusable={true} />) |
| 133 | + const svg = container.querySelector('svg') |
| 134 | + |
| 135 | + expect(svg).toHaveAttribute('focusable', 'true') |
| 136 | + }) |
| 137 | + |
| 138 | + describe('with generated examples', () => { |
| 139 | + const generatedComponents = generateA11yTests(InlineSVG, InlineSVGExamples) |
| 140 | + |
| 141 | + it.each(generatedComponents)( |
| 142 | + 'should be accessible with example: $description', |
| 143 | + async ({ content }) => { |
| 144 | + const { container } = render(content) |
| 145 | + const axeCheck = await runAxeCheck(container) |
| 146 | + expect(axeCheck).toBe(true) |
| 147 | + } |
| 148 | + ) |
| 149 | + }) |
| 150 | +}) |
0 commit comments