@instructure/ui-color-picker 11.7.2 → 11.7.3-snapshot-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,263 +0,0 @@
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 { render, screen } from '@testing-library/react'
26
- import { vi } from 'vitest'
27
- import '@testing-library/jest-dom'
28
- import { runAxeCheck } from '@instructure/ui-axe-check'
29
- import { contrast } from '@instructure/ui-color-utils'
30
-
31
- import { ColorContrast } from './'
32
-
33
- const testColors = {
34
- firstColor: '#FF0000',
35
- secondColor: '#FFFF00'
36
- }
37
-
38
- const testLabels = {
39
- label: 'Color Contrast Ratio',
40
- successLabel: 'PASS',
41
- failureLabel: 'FAIL',
42
- normalTextLabel: 'Normal text',
43
- largeTextLabel: 'Large text',
44
- graphicsTextLabel: 'Graphics text',
45
- firstColorLabel: 'Background',
46
- secondColorLabel: 'Foreground'
47
- }
48
-
49
- type ContrastStatus = 'FAIL' | 'PASS'
50
-
51
- describe('<ColorContrast />', () => {
52
- describe('elementRef prop', () => {
53
- it('should provide ref', async () => {
54
- const elementRef = vi.fn()
55
- const { container } = render(
56
- <ColorContrast
57
- {...testColors}
58
- {...testLabels}
59
- elementRef={elementRef}
60
- />
61
- )
62
-
63
- expect(elementRef).toHaveBeenCalledWith(container.firstChild)
64
- })
65
- })
66
-
67
- describe('labels are displayed:', () => {
68
- Object.entries(testLabels).forEach(([label, text]) => {
69
- it(label, async () => {
70
- const { container } = render(
71
- <ColorContrast {...testColors} {...testLabels} />
72
- )
73
-
74
- expect(container).toHaveTextContent(text)
75
- })
76
- })
77
- })
78
-
79
- describe('labelLevel prop', () => {
80
- it('should render label as div when labelLevel is not provided', async () => {
81
- render(<ColorContrast {...testColors} {...testLabels} />)
82
-
83
- const heading = screen.queryByRole('heading', {
84
- name: testLabels.label
85
- })
86
- expect(heading).not.toBeInTheDocument()
87
-
88
- const labelText = screen.getByText(testLabels.label)
89
- expect(labelText).toBeInTheDocument()
90
- expect(labelText.tagName.toLowerCase()).toBe('div')
91
- })
92
-
93
- it('should render label as Heading when labelLevel is provided', async () => {
94
- render(<ColorContrast {...testColors} {...testLabels} labelLevel="h2" />)
95
-
96
- const heading = screen.getByRole('heading', {
97
- name: testLabels.label,
98
- level: 2
99
- })
100
- expect(heading).toBeInTheDocument()
101
- })
102
-
103
- it('should render correct heading level', async () => {
104
- const { rerender } = render(
105
- <ColorContrast {...testColors} {...testLabels} labelLevel="h3" />
106
- )
107
-
108
- let heading = screen.getByRole('heading', {
109
- name: testLabels.label,
110
- level: 3
111
- })
112
- expect(heading).toBeInTheDocument()
113
-
114
- rerender(
115
- <ColorContrast {...testColors} {...testLabels} labelLevel="h1" />
116
- )
117
-
118
- heading = screen.getByRole('heading', {
119
- name: testLabels.label,
120
- level: 1
121
- })
122
- expect(heading).toBeInTheDocument()
123
- })
124
- })
125
-
126
- describe('should calculate contrast correctly', () => {
127
- it('on opaque colors', async () => {
128
- const color1 = '#fff'
129
- const color2 = '#088'
130
-
131
- const { container } = render(
132
- <ColorContrast
133
- {...testLabels}
134
- firstColor={color1}
135
- secondColor={color2}
136
- />
137
- )
138
- const contrastResult = contrast(color1, color2, 2)
139
-
140
- expect(container).toHaveTextContent(contrastResult + ':1')
141
- })
142
-
143
- it('on transparent colors', async () => {
144
- const color1 = '#fff'
145
- const color2 = '#00888880'
146
-
147
- const { container } = render(
148
- <ColorContrast
149
- {...testLabels}
150
- firstColor={color1}
151
- secondColor={color2}
152
- />
153
- )
154
-
155
- // this is the result of a complicated "blended color" calculation
156
- // in the component, not simple `contrast()` check
157
- expect(container).toHaveTextContent('2:1')
158
- })
159
- })
160
-
161
- describe('withoutColorPreview prop', () => {
162
- it('should be false by default, should display preview', async () => {
163
- const { container } = render(
164
- <ColorContrast {...testColors} {...testLabels} />
165
- )
166
-
167
- const preview = container.querySelector(
168
- "[class$='-colorContrast__colorPreview']"
169
- )
170
- expect(preview).toBeInTheDocument()
171
- })
172
-
173
- it('should hide preview', async () => {
174
- const { container } = render(
175
- <ColorContrast {...testColors} {...testLabels} withoutColorPreview />
176
- )
177
-
178
- const preview = container.querySelector(
179
- "[class$='-colorContrast__colorPreview']"
180
- )
181
- expect(preview).not.toBeInTheDocument()
182
- })
183
- })
184
-
185
- describe('contrast check', () => {
186
- const checkContrastPills = (
187
- title: string,
188
- firstColor: string,
189
- secondColor: string,
190
- expectedResult: {
191
- normal: ContrastStatus
192
- large: ContrastStatus
193
- graphics: ContrastStatus
194
- }
195
- ) => {
196
- describe(title, () => {
197
- it(`normal text should ${expectedResult.normal.toLowerCase()}`, async () => {
198
- const { container } = render(
199
- <ColorContrast
200
- {...testLabels}
201
- firstColor={firstColor}
202
- secondColor={secondColor}
203
- />
204
- )
205
-
206
- expect(container).toHaveTextContent(expectedResult.normal)
207
- })
208
-
209
- it(`large text should ${expectedResult.large.toLowerCase()}`, async () => {
210
- const { container } = render(
211
- <ColorContrast
212
- {...testLabels}
213
- firstColor={firstColor}
214
- secondColor={secondColor}
215
- />
216
- )
217
-
218
- expect(container).toHaveTextContent(expectedResult.large)
219
- })
220
-
221
- it(`graphics should ${expectedResult.graphics.toLowerCase()}`, async () => {
222
- const { container } = render(
223
- <ColorContrast
224
- {...testLabels}
225
- firstColor={firstColor}
226
- secondColor={secondColor}
227
- />
228
- )
229
- expect(container).toHaveTextContent(expectedResult.graphics)
230
- })
231
- })
232
- }
233
-
234
- checkContrastPills('on x < 3 contrast', '#fff', '#aaa', {
235
- normal: 'FAIL',
236
- large: 'FAIL',
237
- graphics: 'FAIL'
238
- })
239
-
240
- checkContrastPills('on small 3 < x < 4.5 contrast', '#fff', '#0c89bf', {
241
- normal: 'FAIL',
242
- large: 'PASS',
243
- graphics: 'PASS'
244
- })
245
-
246
- checkContrastPills('on small x > 4.5 contrast', '#fff', '#333', {
247
- normal: 'PASS',
248
- large: 'PASS',
249
- graphics: 'PASS'
250
- })
251
- })
252
-
253
- describe('should be accessible', () => {
254
- it('a11y', async () => {
255
- const { container } = render(
256
- <ColorContrast {...testColors} {...testLabels} />
257
- )
258
- const axeCheck = await runAxeCheck(container)
259
-
260
- expect(axeCheck).toBe(true)
261
- })
262
- })
263
- })