Skip to content

Commit 21e3eb0

Browse files
committed
test(ui-img): migrate old Img tests
Closes: INSTUI-4365
1 parent bd4b170 commit 21e3eb0

File tree

8 files changed

+244
-233
lines changed

8 files changed

+244
-233
lines changed

cypress/component/Img.cy.tsx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 { Img } from '@instructure/ui'
28+
29+
const HEIGHT = 32
30+
const WIDTH = 24
31+
const ALT = 'test-alt'
32+
const IMAGE =
33+
'data:image/gif;base64,R0lGODlhFAAUAJEAAP/9/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw=='
34+
35+
describe('<Img />', () => {
36+
it('has a container with matching height and width when constrain="cover" is used with overlay', async () => {
37+
cy.mount(
38+
<div style={{ width: WIDTH, height: HEIGHT }}>
39+
<Img
40+
src={IMAGE}
41+
alt={ALT}
42+
constrain="cover"
43+
overlay={{ color: '#ff0000', opacity: 7 }}
44+
/>
45+
</div>
46+
)
47+
48+
cy.get('[class*="-img__container"]')
49+
.should('have.css', 'height', `${HEIGHT}px`)
50+
.and('have.css', 'width', `${WIDTH}px`)
51+
52+
cy.get('img').should('have.attr', 'alt', ALT)
53+
})
54+
55+
it('has a container with matching height and not matching width when constrain="contain" is used with overlay', async () => {
56+
cy.mount(
57+
<div style={{ width: WIDTH, height: HEIGHT }}>
58+
<Img
59+
src={IMAGE}
60+
alt={ALT}
61+
constrain="contain"
62+
overlay={{ color: '#ff0000', opacity: 7 }}
63+
/>
64+
</div>
65+
)
66+
67+
cy.get('[class*="-img__container"]')
68+
// height is set to inherit, it should match
69+
.should('have.css', 'height', `${HEIGHT}px`)
70+
// width isn't set, it shouldn't match
71+
.and('not.have.css', 'width', `${WIDTH}px`)
72+
73+
cy.get('img').should('have.attr', 'alt', ALT)
74+
})
75+
})

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-img/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
"prop-types": "^15.8.1"
3535
},
3636
"devDependencies": {
37+
"@instructure/ui-axe-check": "10.6.0",
3738
"@instructure/ui-babel-preset": "10.6.0",
38-
"@instructure/ui-test-locator": "10.6.0",
39-
"@instructure/ui-test-utils": "10.6.0"
39+
"@instructure/ui-test-utils": "10.6.0",
40+
"@testing-library/jest-dom": "^6.4.6",
41+
"@testing-library/react": "^16.0.1",
42+
"@testing-library/user-event": "^14.5.2",
43+
"vitest": "^2.1.1"
4044
},
4145
"peerDependencies": {
4246
"react": ">=16.8 <=18"

packages/ui-img/src/Img/ImgLocator.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 { render } from '@testing-library/react'
26+
import type { MockInstance } from 'vitest'
27+
import { vi } from 'vitest'
28+
29+
import '@testing-library/jest-dom'
30+
import { runAxeCheck } from '@instructure/ui-axe-check'
31+
import { Img } from '../index'
32+
33+
describe('<Img />', () => {
34+
const image =
35+
'data:image/gif;base64,R0lGODlhFAAUAJEAAP/9/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw=='
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 and expect for messages
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+
describe('for a11y', () => {
55+
it('should meet a11y standards', async () => {
56+
const { container } = render(<Img src={image} />)
57+
const axeCheck = await runAxeCheck(container)
58+
59+
expect(axeCheck).toBe(true)
60+
})
61+
62+
it('should render an empty alt attribute by default', async () => {
63+
const { container } = render(<Img src={image} />)
64+
const img = container.querySelector('img')
65+
66+
expect(img).toHaveAttribute('alt', '')
67+
})
68+
69+
it('should render the provided alt attribute', async () => {
70+
const altValue = 'Foo'
71+
const { container } = render(<Img src={image} alt={altValue} />)
72+
const img = container.querySelector('img')
73+
74+
expect(img).toHaveAttribute('alt', altValue)
75+
})
76+
})
77+
78+
it('should render an overlay color', async () => {
79+
const { container } = render(
80+
<Img src={image} overlay={{ color: '#ff0000', opacity: 7 }} />
81+
)
82+
const overlay = container.querySelector('[class*="-img__overlay"]')
83+
84+
expect(overlay).toBeInTheDocument()
85+
})
86+
87+
it('should render a blur filter', async () => {
88+
const { container } = render(<Img src={image} withBlur={true} />)
89+
90+
const img = container.querySelector('img')!
91+
const style = getComputedStyle(img)
92+
93+
expect(style.filter).toContain('blur')
94+
})
95+
96+
it('should render a grayscale filter', async () => {
97+
const { container } = render(<Img src={image} withGrayscale={true} />)
98+
99+
const img = container.querySelector('img')!
100+
const style = getComputedStyle(img)
101+
102+
expect(style.filter).toContain('grayscale')
103+
})
104+
105+
// // If component renders as simple image
106+
it('should display block-level when display="block"', async () => {
107+
const { container } = render(<Img src={image} display="block" />)
108+
const img = container.querySelector('img')!
109+
const style = getComputedStyle(img)
110+
111+
expect(style.display).toBe('block')
112+
})
113+
114+
// // If component has an overlay and renders as image inside containing element
115+
it('should display block-level with overlay when display="block"', async () => {
116+
const { container } = render(
117+
<Img
118+
src={image}
119+
display="block"
120+
overlay={{ color: 'tomato', opacity: 7 }}
121+
/>
122+
)
123+
const img = container.querySelector('img')!
124+
const imgContainer = container.querySelector('[class*="-img__container"]')!
125+
const imgStyle = getComputedStyle(img)
126+
const imgContainerStyle = getComputedStyle(imgContainer)
127+
128+
expect(imgStyle.display).toBe('block')
129+
expect(imgContainerStyle.display).toBe('block')
130+
})
131+
132+
it('should apply CSS object-fit: cover when constrain="cover"', async () => {
133+
const { container } = render(
134+
<div style={{ width: 16, height: 16 }}>
135+
<Img src={image} constrain="cover" />
136+
</div>
137+
)
138+
const img = container.querySelector('img')!
139+
const imgStyle = getComputedStyle(img)
140+
141+
expect(imgStyle.objectFit).toBe('cover')
142+
})
143+
144+
it('should apply CSS object-fit: contain when constrain="contain"', async () => {
145+
const { container } = render(
146+
<div style={{ width: 16, height: 16 }}>
147+
<Img src={image} constrain="contain" />
148+
</div>
149+
)
150+
const img = container.querySelector('img')!
151+
const imgStyle = getComputedStyle(img)
152+
153+
expect(imgStyle.objectFit).toBe('contain')
154+
})
155+
})

0 commit comments

Comments
 (0)