Skip to content

Commit 71416b3

Browse files
committed
test(ui-svg-images): migrate old InlineSVG and SVGIcon tests
Closes: INSTUI-4372
1 parent b366ddd commit 71416b3

File tree

12 files changed

+353
-380
lines changed

12 files changed

+353
-380
lines changed

cypress/component/InlineSVG.cy.tsx

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
import React from 'react'
25+
import '../support/component'
26+
import 'cypress-real-events'
27+
import { InlineSVG } from '@instructure/ui'
28+
import { expect } from 'chai'
29+
30+
const WIDTH = '100px'
31+
const HEIGHT = '200px'
32+
const SVG_SRC = `<svg><circle cx="50" cy="50" r="40" /></svg>`
33+
34+
describe('<InlineSVG />', () => {
35+
it('should set custom width and height properly', async () => {
36+
cy.mount(<InlineSVG src={SVG_SRC} width={WIDTH} height={HEIGHT} />)
37+
38+
cy.get('svg')
39+
.should('have.css', 'height', HEIGHT)
40+
.and('have.css', 'width', WIDTH)
41+
42+
cy.get('svg').then(($svg) => {
43+
const attributes = $svg[0].attributes
44+
const attributeNames = Array.from(attributes).map((attr) => attr.name)
45+
46+
expect(attributeNames).to.include('width')
47+
expect(attributeNames).to.include('height')
48+
})
49+
})
50+
51+
it('should not set width/height attributes and styles when value is auto', async () => {
52+
cy.mount(<InlineSVG src={SVG_SRC} width="auto" height="auto" />)
53+
54+
cy.get('svg').then(($svg) => {
55+
const attributes = $svg[0].attributes
56+
const attributeNames = Array.from(attributes).map((attr) => attr.name)
57+
58+
expect(attributeNames).not.to.include('width')
59+
expect(attributeNames).not.to.include('height')
60+
})
61+
})
62+
63+
it('should display block when inline is false', async () => {
64+
cy.mount(<InlineSVG src={SVG_SRC} inline={false} />)
65+
66+
cy.get('svg').should('have.css', 'display', 'block')
67+
})
68+
69+
it('should change the SVG color property', async () => {
70+
cy.mount(<InlineSVG src={SVG_SRC} color="success" />)
71+
72+
cy.get('svg').then(($successSvg) => {
73+
const colorSuccess = getComputedStyle($successSvg[0]).color
74+
75+
// Set prop: color
76+
cy.mount(<InlineSVG src={SVG_SRC} color="error" />)
77+
78+
cy.get('svg').then(($errorSvg) => {
79+
const colorError = getComputedStyle($errorSvg[0]).color
80+
81+
expect(colorError).to.not.equal(colorSuccess)
82+
})
83+
})
84+
})
85+
86+
it('should allow passing in the svg src as a string', async () => {
87+
cy.mount(<InlineSVG src={`<svg><circle cx="50" cy="50" r="40" /></svg>`} />)
88+
89+
cy.get('svg').should('exist')
90+
cy.get('g').should('exist')
91+
92+
cy.get('g').then(($g) => {
93+
const innerHTML = $g[0].innerHTML.trim()
94+
expect(innerHTML).to.equal('<circle cx="50" cy="50" r="40"></circle>')
95+
})
96+
})
97+
})

package-lock.json

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ui-svg-images/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
},
2424
"license": "MIT",
2525
"devDependencies": {
26+
"@instructure/ui-axe-check": "10.6.0",
2627
"@instructure/ui-babel-preset": "10.6.0",
27-
"@instructure/ui-test-locator": "10.6.0",
2828
"@instructure/ui-test-utils": "10.6.0",
29-
"@instructure/ui-themes": "10.6.0"
29+
"@instructure/ui-themes": "10.6.0",
30+
"@testing-library/jest-dom": "^6.4.6",
31+
"@testing-library/react": "^16.0.1",
32+
"@testing-library/user-event": "^14.5.2",
33+
"vitest": "^2.1.1"
3034
},
3135
"dependencies": {
3236
"@babel/runtime": "^7.25.6",

packages/ui-svg-images/src/InlineSVG/InlineSVGLocator.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)