-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathElementAssertion.ts
More file actions
334 lines (287 loc) · 9.57 KB
/
ElementAssertion.ts
File metadata and controls
334 lines (287 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { Assertion, AssertionError } from "@assertive-ts/core";
import equal from "fast-deep-equal";
import { getExpectedAndReceivedStyles, isElementEmpty } from "./helpers/helpers";
export class ElementAssertion<T extends Element> extends Assertion<T> {
public constructor(actual: T) {
super(actual);
}
/**
* Check if the element is in the document.
*
* @returns the assertion instance.
*/
public toBeInTheDocument(): this {
const error = new AssertionError({
actual: this.actual,
message: "Expected the element to be in the document",
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the element to NOT be in the document",
});
return this.execute({
assertWhen: (
this.actual.ownerDocument.defaultView !== null
&& this.actual.ownerDocument === this.actual.getRootNode({ composed: true })
),
error,
invertedError,
});
}
/**
* Check if a given container element contains a specified child element.
*
* @param element the child expected to be contained.
* @returns the assertion instance.
*/
public toContainElement(element: Element): this {
const error = new AssertionError({
actual: this.actual,
message: "Expected the container to contain the element",
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the container to NOT contain the element",
});
return this.execute({
assertWhen: this.actual.contains(element),
error,
invertedError,
});
}
/**
* Check if the element has a specific attribute.
*
* @param name - The attribute name.
* @param expectedValue - The expected attribute value (Optional).
* @returns the assertion instance.
*/
public toHaveAttribute(name: string, expectedValue?: string): this {
const hasAttribute = this.actual.hasAttribute(name);
const receivedValue = this.actual.getAttribute(name);
const isExpectedValuePresent = expectedValue !== undefined;
const error = new AssertionError({
actual: receivedValue,
expected: expectedValue,
message: isExpectedValuePresent
? `Expected to have attribute "${name}" with value "${expectedValue}", but received "${receivedValue}"`
: `Expected to have attribute "${name}"`,
});
const invertedError = new AssertionError({
actual: receivedValue,
expected: expectedValue,
message: isExpectedValuePresent
? `Expected to NOT have attribute "${name}" with value "${expectedValue}", but received "${receivedValue}"`
: `Expected to NOT have attribute "${name}"`,
});
return this.execute({
assertWhen: (isExpectedValuePresent
? hasAttribute && receivedValue === expectedValue
: hasAttribute),
error,
invertedError,
});
}
/**
* Asserts that the element has the specified class.
*
* @param className The class name to check.
* @returns the assertion instance.
*/
public toHaveClass(className: string): this {
const actualClassList = this.getClassList();
return this.assertClassPresence(
actualClassList.includes(className),
[className],
`Expected the element to have class: "${className}"`,
`Expected the element to NOT have class: "${className}"`,
);
}
/**
* Asserts that the element has at least one of the specified classes.
*
* @param classNames - A variadic list of class names to check.
* @returns the assertion instance.
*/
public toHaveAnyClass(...classNames: string[]): this {
const actualClassList = this.getClassList();
return this.assertClassPresence(
classNames.some(cls => actualClassList.includes(cls)),
classNames,
`Expected the element to have at least one of these classes: "${classNames.join(" ")}"`,
`Expected the element to NOT have any of these classes: "${classNames.join(" ")}"`,
);
}
/**
* Asserts that the element has all of the specified classes.
*
* @param classNames - A variadic list of class names to check.
* @returns the assertion instance.
*/
public toHaveAllClasses(...classNames: string[]): this {
const actualClassList = this.getClassList();
return this.assertClassPresence(
classNames.every(cls => actualClassList.includes(cls)),
classNames,
`Expected the element to have all of these classes: "${classNames.join(" ")}"`,
`Expected the element to NOT have all of these classes: "${classNames.join(" ")}"`,
);
}
/**
* Check if the provided element is currently focused in the document.
*
* @example
* const userNameInput = document.querySelector('#username');
* userNameInput.focus();
* expect(userNameInput).toHaveFocus(); // passes
* expect(userNameInput).not.toHaveFocus(); // fails
*
* @returns The assertion instance.
*/
public toHaveFocus(): this {
const hasFocus = this.actual === document.activeElement;
const error = new AssertionError({
actual: this.actual,
expected: document.activeElement,
message: "Expected the element to be focused",
});
const invertedError = new AssertionError({
actual: this.actual,
expected: document.activeElement,
message: "Expected the element NOT to be focused",
});
return this.execute({
assertWhen: hasFocus,
error,
invertedError,
});
}
/**
* Asserts that the element has the specified CSS styles.
*
* @example
* ```
* expect(component).toHaveStyle({ color: 'green', display: 'block' });
* ```
*
* @param expected the expected CSS styles.
* @returns the assertion instance.
*/
public toHaveStyle(expected: Partial<CSSStyleDeclaration>): this {
const [expectedStyle, receivedStyle] = getExpectedAndReceivedStyles(this.actual, expected);
if (!expectedStyle || !receivedStyle) {
throw new Error("Currently there are no available styles.");
}
const error = new AssertionError({
actual: this.actual,
expected: expectedStyle,
message: `Expected the element to match the following style:\n${JSON.stringify(expectedStyle, null, 2)}`,
});
const invertedError = new AssertionError({
actual: this.actual,
message: `Expected the element to NOT match the following style:\n${JSON.stringify(expectedStyle, null, 2)}`,
});
return this.execute({
assertWhen: equal(expectedStyle, receivedStyle),
error,
invertedError,
});
}
/**
* Asserts that the element has one or more of the specified CSS styles.
*
* @example
* ```
* expect(component).toHaveSomeStyle({ color: 'green', display: 'block' });
* ```
*
* @param expected the expected CSS style/s.
* @returns the assertion instance.
*/
public toHaveSomeStyle(expected: Partial<CSSStyleDeclaration>): this {
const [expectedStyle, elementProcessedStyle] = getExpectedAndReceivedStyles(this.actual, expected);
if (!expectedStyle || !elementProcessedStyle) {
throw new Error("No available styles.");
}
const hasSomeStyle = Object.entries(expectedStyle).some(([expectedProp, expectedValue]) => {
return Object.entries(elementProcessedStyle).some(([receivedProp, receivedValue]) => {
return equal(expectedProp, receivedProp) && equal(expectedValue, receivedValue);
});
});
const error = new AssertionError({
actual: this.actual,
message: `Expected the element to match some of the following styles:\n${JSON.stringify(expectedStyle, null, 2)}`,
});
const invertedError = new AssertionError({
actual: this.actual,
// eslint-disable-next-line max-len
message: `Expected the element NOT to match some of the following styles:\n${JSON.stringify(expectedStyle, null, 2)}`,
});
return this.execute({
assertWhen: hasSomeStyle,
error,
invertedError,
});
}
/**
* Asserts that the element does not contain child nodes, excluding comments.
*
* @example
* ```
* expect(component).toBeEmpty();
* ```
*
* @returns the assertion instance.
*/
public toBeEmpty(): this {
const isEmpty = isElementEmpty(this.actual);
const error = new AssertionError({
actual: this.actual,
message: "Expected the element to be empty.",
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the element NOT to be empty.",
});
return this.execute({
assertWhen: isEmpty,
error,
invertedError,
});
}
/**
* Helper method to assert the presence or absence of class names.
*
* @param assertCondition - Boolean to determine assertion pass or fail.
* @param classNames - Array of class names involved in the assertion.
* @param message - Assertion error message.
* @param invertedMessage - Inverted assertion error message.
* @returns the assertion instance.
*/
private assertClassPresence(
assertCondition: boolean,
classNames: string[],
message: string,
invertedMessage: string,
): this {
const actualClassList = this.getClassList();
const error = new AssertionError({
actual: actualClassList,
expected: classNames,
message,
});
const invertedError = new AssertionError({
actual: actualClassList,
expected: classNames,
message: invertedMessage,
});
return this.execute({
assertWhen: assertCondition,
error,
invertedError,
});
}
private getClassList(): string[] {
return this.actual.className.split(/\s+/).filter(Boolean);
}
}