@afixt/test-utils 1.2.0 → 1.2.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.
- package/eslint.config.mjs +1 -1
- package/package.json +2 -1
- package/src/constants.js +231 -0
- package/src/cssUtils.js +77 -0
- package/src/domUtils.js +268 -12
- package/src/formUtils.js +175 -0
- package/src/getAccessibleText.js +60 -39
- package/src/getCSSGeneratedContent.js +39 -17
- package/src/index.js +18 -2
- package/src/stringUtils.js +149 -0
- package/src/tableUtils.js +180 -0
- package/src/testContrast.js +35 -1
- package/src/testLang.js +514 -444
- package/test/cssUtils.test.js +248 -0
- package/test/domUtils.test.js +815 -297
- package/test/formUtils.test.js +389 -0
- package/test/getAccessibleText.test.js +93 -0
- package/test/getCSSGeneratedContent.test.js +187 -232
- package/test/hasCSSGeneratedContent.test.js +37 -147
- package/test/playwright/css-pseudo-elements.spec.js +224 -91
- package/test/playwright/fixtures/css-pseudo-elements.html +6 -0
- package/test/stringUtils.test.js +222 -0
- package/test/tableUtils.test.js +340 -0
- package/vitest.config.js +28 -28
- package/test/getCSSGeneratedContent.browser.test.js +0 -125
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
2
|
+
import cssUtils from '../src/cssUtils.js';
|
|
3
|
+
|
|
4
|
+
describe('cssUtils', () => {
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
document.body.innerHTML = '';
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe('hasTransparentBackground', () => {
|
|
10
|
+
it('should return true for rgba(0, 0, 0, 0)', () => {
|
|
11
|
+
const style = { backgroundColor: 'rgba(0, 0, 0, 0)', backgroundImage: 'none' };
|
|
12
|
+
expect(cssUtils.hasTransparentBackground(style)).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should return true for "transparent"', () => {
|
|
16
|
+
const style = { backgroundColor: 'transparent', backgroundImage: 'none' };
|
|
17
|
+
expect(cssUtils.hasTransparentBackground(style)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should return true for empty string background', () => {
|
|
21
|
+
const style = { backgroundColor: '', backgroundImage: 'none' };
|
|
22
|
+
expect(cssUtils.hasTransparentBackground(style)).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return false when background color is set', () => {
|
|
26
|
+
const style = { backgroundColor: 'rgb(255, 0, 0)', backgroundImage: 'none' };
|
|
27
|
+
expect(cssUtils.hasTransparentBackground(style)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should return false when background image is set', () => {
|
|
31
|
+
const style = { backgroundColor: 'rgba(0, 0, 0, 0)', backgroundImage: 'url(bg.png)' };
|
|
32
|
+
expect(cssUtils.hasTransparentBackground(style)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should return false when both background color and image are set', () => {
|
|
36
|
+
const style = { backgroundColor: 'rgb(255, 255, 255)', backgroundImage: 'url(bg.png)' };
|
|
37
|
+
expect(cssUtils.hasTransparentBackground(style)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('hasNoBorder', () => {
|
|
42
|
+
it('should return true when borderStyle is "none"', () => {
|
|
43
|
+
const style = { borderStyle: 'none', borderWidth: '0px' };
|
|
44
|
+
expect(cssUtils.hasNoBorder(style)).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should return true when borderWidth is 0', () => {
|
|
48
|
+
const style = { borderStyle: 'solid', borderWidth: '0px' };
|
|
49
|
+
expect(cssUtils.hasNoBorder(style)).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should return false when border is visible', () => {
|
|
53
|
+
const style = { borderStyle: 'solid', borderWidth: '1px' };
|
|
54
|
+
expect(cssUtils.hasNoBorder(style)).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should return false for dashed border with width', () => {
|
|
58
|
+
const style = { borderStyle: 'dashed', borderWidth: '2px' };
|
|
59
|
+
expect(cssUtils.hasNoBorder(style)).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('looksLikeText', () => {
|
|
64
|
+
it('should return true for element with no visual indicators', () => {
|
|
65
|
+
const el = document.createElement('span');
|
|
66
|
+
document.body.appendChild(el);
|
|
67
|
+
|
|
68
|
+
const mockStyle = {
|
|
69
|
+
textDecoration: 'none',
|
|
70
|
+
backgroundColor: 'rgba(0, 0, 0, 0)',
|
|
71
|
+
backgroundImage: 'none',
|
|
72
|
+
borderStyle: 'none',
|
|
73
|
+
borderWidth: '0px',
|
|
74
|
+
};
|
|
75
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue(mockStyle);
|
|
76
|
+
|
|
77
|
+
expect(cssUtils.looksLikeText(el)).toBe(true);
|
|
78
|
+
vi.restoreAllMocks();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should return false for element with underline', () => {
|
|
82
|
+
const el = document.createElement('a');
|
|
83
|
+
document.body.appendChild(el);
|
|
84
|
+
|
|
85
|
+
const mockStyle = {
|
|
86
|
+
textDecoration: 'underline',
|
|
87
|
+
backgroundColor: 'rgba(0, 0, 0, 0)',
|
|
88
|
+
backgroundImage: 'none',
|
|
89
|
+
borderStyle: 'none',
|
|
90
|
+
borderWidth: '0px',
|
|
91
|
+
};
|
|
92
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue(mockStyle);
|
|
93
|
+
|
|
94
|
+
expect(cssUtils.looksLikeText(el)).toBe(false);
|
|
95
|
+
vi.restoreAllMocks();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should return false for element with background', () => {
|
|
99
|
+
const el = document.createElement('span');
|
|
100
|
+
document.body.appendChild(el);
|
|
101
|
+
|
|
102
|
+
const mockStyle = {
|
|
103
|
+
textDecoration: 'none',
|
|
104
|
+
backgroundColor: 'rgb(200, 200, 200)',
|
|
105
|
+
backgroundImage: 'none',
|
|
106
|
+
borderStyle: 'none',
|
|
107
|
+
borderWidth: '0px',
|
|
108
|
+
};
|
|
109
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue(mockStyle);
|
|
110
|
+
|
|
111
|
+
expect(cssUtils.looksLikeText(el)).toBe(false);
|
|
112
|
+
vi.restoreAllMocks();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should return false for element with border', () => {
|
|
116
|
+
const el = document.createElement('span');
|
|
117
|
+
document.body.appendChild(el);
|
|
118
|
+
|
|
119
|
+
const mockStyle = {
|
|
120
|
+
textDecoration: 'none',
|
|
121
|
+
backgroundColor: 'rgba(0, 0, 0, 0)',
|
|
122
|
+
backgroundImage: 'none',
|
|
123
|
+
borderStyle: 'solid',
|
|
124
|
+
borderWidth: '1px',
|
|
125
|
+
};
|
|
126
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue(mockStyle);
|
|
127
|
+
|
|
128
|
+
expect(cssUtils.looksLikeText(el)).toBe(false);
|
|
129
|
+
vi.restoreAllMocks();
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('hasLinkStyling', () => {
|
|
134
|
+
it('should return true for element with underline text decoration', () => {
|
|
135
|
+
const el = document.createElement('a');
|
|
136
|
+
document.body.appendChild(el);
|
|
137
|
+
|
|
138
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
|
139
|
+
textDecoration: 'underline',
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(cssUtils.hasLinkStyling(el)).toBe(true);
|
|
143
|
+
vi.restoreAllMocks();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should return true when underline is part of multiple decorations', () => {
|
|
147
|
+
const el = document.createElement('a');
|
|
148
|
+
document.body.appendChild(el);
|
|
149
|
+
|
|
150
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
|
151
|
+
textDecoration: 'underline solid rgb(0, 0, 238)',
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
expect(cssUtils.hasLinkStyling(el)).toBe(true);
|
|
155
|
+
vi.restoreAllMocks();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should return false for element without underline', () => {
|
|
159
|
+
const el = document.createElement('span');
|
|
160
|
+
document.body.appendChild(el);
|
|
161
|
+
|
|
162
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
|
163
|
+
textDecoration: 'none',
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
expect(cssUtils.hasLinkStyling(el)).toBe(false);
|
|
167
|
+
vi.restoreAllMocks();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('hasButtonStyling', () => {
|
|
172
|
+
it('should return true for element with background and padding', () => {
|
|
173
|
+
const el = document.createElement('span');
|
|
174
|
+
document.body.appendChild(el);
|
|
175
|
+
|
|
176
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
|
177
|
+
backgroundColor: 'rgb(0, 123, 255)',
|
|
178
|
+
backgroundImage: 'none',
|
|
179
|
+
borderStyle: 'none',
|
|
180
|
+
borderWidth: '0px',
|
|
181
|
+
paddingTop: '8px',
|
|
182
|
+
paddingBottom: '8px',
|
|
183
|
+
paddingLeft: '16px',
|
|
184
|
+
paddingRight: '16px',
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
expect(cssUtils.hasButtonStyling(el)).toBe(true);
|
|
188
|
+
vi.restoreAllMocks();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should return true for element with border and padding', () => {
|
|
192
|
+
const el = document.createElement('span');
|
|
193
|
+
document.body.appendChild(el);
|
|
194
|
+
|
|
195
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
|
196
|
+
backgroundColor: 'rgba(0, 0, 0, 0)',
|
|
197
|
+
backgroundImage: 'none',
|
|
198
|
+
borderStyle: 'solid',
|
|
199
|
+
borderWidth: '1px',
|
|
200
|
+
paddingTop: '8px',
|
|
201
|
+
paddingBottom: '8px',
|
|
202
|
+
paddingLeft: '16px',
|
|
203
|
+
paddingRight: '16px',
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
expect(cssUtils.hasButtonStyling(el)).toBe(true);
|
|
207
|
+
vi.restoreAllMocks();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('should return false for element without sufficient padding', () => {
|
|
211
|
+
const el = document.createElement('span');
|
|
212
|
+
document.body.appendChild(el);
|
|
213
|
+
|
|
214
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
|
215
|
+
backgroundColor: 'rgb(0, 123, 255)',
|
|
216
|
+
backgroundImage: 'none',
|
|
217
|
+
borderStyle: 'none',
|
|
218
|
+
borderWidth: '0px',
|
|
219
|
+
paddingTop: '0px',
|
|
220
|
+
paddingBottom: '0px',
|
|
221
|
+
paddingLeft: '0px',
|
|
222
|
+
paddingRight: '0px',
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
expect(cssUtils.hasButtonStyling(el)).toBe(false);
|
|
226
|
+
vi.restoreAllMocks();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should return false for transparent element with no border', () => {
|
|
230
|
+
const el = document.createElement('span');
|
|
231
|
+
document.body.appendChild(el);
|
|
232
|
+
|
|
233
|
+
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
|
234
|
+
backgroundColor: 'rgba(0, 0, 0, 0)',
|
|
235
|
+
backgroundImage: 'none',
|
|
236
|
+
borderStyle: 'none',
|
|
237
|
+
borderWidth: '0px',
|
|
238
|
+
paddingTop: '10px',
|
|
239
|
+
paddingBottom: '10px',
|
|
240
|
+
paddingLeft: '20px',
|
|
241
|
+
paddingRight: '20px',
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
expect(cssUtils.hasButtonStyling(el)).toBe(false);
|
|
245
|
+
vi.restoreAllMocks();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
});
|