-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
assertions.ts
191 lines (156 loc) · 5.84 KB
/
assertions.ts
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
import { Locator, YupValidator } from '../types';
import { isCallable, isObject } from '../../../shared';
import { IS_ABSENT } from '../symbols';
export function isLocator(value: unknown): value is Locator {
return isCallable(value) && !!(value as Locator).__locatorRef;
}
/**
* Checks if an tag name is a native HTML tag and not a Vue component
*/
export function isHTMLTag(tag: string) {
return ['input', 'textarea', 'select'].includes(tag);
}
/**
* Checks if an input is of type file
*/
export function isFileInputNode(tag: string, attrs: Record<string, unknown>) {
return isHTMLTag(tag) && attrs.type === 'file';
}
export function isYupValidator(value: unknown): value is YupValidator {
return !!value && isCallable((value as { validate?: () => unknown }).validate);
}
export function hasCheckedAttr(type: unknown) {
return type === 'checkbox' || type === 'radio';
}
export function isContainerValue(value: unknown): value is Record<string, unknown> {
return isObject(value) || Array.isArray(value);
}
/**
* True if the value is an empty object or array
*/
export function isEmptyContainer(value: unknown): boolean {
if (Array.isArray(value)) {
return value.length === 0;
}
return isObject(value) && Object.keys(value).length === 0;
}
/**
* Checks if the path opted out of nested fields using `[fieldName]` syntax
*/
export function isNotNestedPath(path: string) {
return /^\[.+\]$/i.test(path);
}
/**
* Checks if an element is a native HTML5 multi-select input element
*/
export function isNativeMultiSelect(el: HTMLElement): el is HTMLSelectElement {
return isNativeSelect(el) && el.multiple;
}
/**
* Checks if an element is a native HTML5 select input element
*/
export function isNativeSelect(el: HTMLElement): el is HTMLSelectElement {
return el.tagName === 'SELECT';
}
/**
* Checks if a tag name with attrs object will render a native multi-select element
*/
export function isNativeMultiSelectNode(tag: string, attrs: Record<string, unknown>) {
// The falsy value array is the values that Vue won't add the `multiple` prop if it has one of these values
const hasTruthyBindingValue =
![false, null, undefined, 0].includes(attrs.multiple as boolean) && !Number.isNaN(attrs.multiple);
return tag === 'select' && 'multiple' in attrs && hasTruthyBindingValue;
}
/**
* Checks if a node should have a `:value` binding or not
*
* These nodes should not have a value binding
* For files, because they are not reactive
* For multi-selects because the value binding will reset the value
*/
export function shouldHaveValueBinding(tag: string, attrs: Record<string, unknown>) {
return !isNativeMultiSelectNode(tag, attrs) && attrs.type !== 'file' && !hasCheckedAttr(attrs.type);
}
export function isFormSubmitEvent(evt: unknown): evt is Event & { target: HTMLFormElement } {
return isEvent(evt) && (evt as any).target && 'submit' in (evt as any).target;
}
export function isEvent(evt: unknown): evt is Event {
if (!evt) {
return false;
}
if (typeof Event !== 'undefined' && isCallable(Event) && evt instanceof Event) {
return true;
}
// this is for IE and Cypress #3161
/* istanbul ignore next */
if (evt && (evt as Event).srcElement) {
return true;
}
return false;
}
export function isPropPresent(obj: Record<string, unknown>, prop: string) {
return prop in obj && obj[prop] !== IS_ABSENT;
}
/**
* Compares if two values are the same borrowed from:
* https://github.com/epoberezkin/fast-deep-equal
* We added a case for file matching since `Object.keys` doesn't work with Files.
* */
export function isEqual(a: any, b: any) {
if (a === b) return true;
if (a && b && typeof a === 'object' && typeof b === 'object') {
if (a.constructor !== b.constructor) return false;
// eslint-disable-next-line no-var
var length, i, keys;
if (Array.isArray(a)) {
length = a.length;
// eslint-disable-next-line eqeqeq
if (length != b.length) return false;
for (i = length; i-- !== 0; ) if (!isEqual(a[i], b[i])) return false;
return true;
}
if (a instanceof Map && b instanceof Map) {
if (a.size !== b.size) return false;
for (i of a.entries()) if (!b.has(i[0])) return false;
for (i of a.entries()) if (!isEqual(i[1], b.get(i[0]))) return false;
return true;
}
// We added this part for file comparison, arguably a little naive but should work for most cases.
// #3911
if (a instanceof File && b instanceof File) {
if (a.size !== b.size) return false;
if (a.name !== b.name) return false;
if (a.lastModified !== b.lastModified) return false;
if (a.type !== b.type) return false;
return true;
}
if (a instanceof Set && b instanceof Set) {
if (a.size !== b.size) return false;
for (i of a.entries()) if (!b.has(i[0])) return false;
return true;
}
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
length = (a as any).length;
// eslint-disable-next-line eqeqeq
if (length != (b as any).length) return false;
for (i = length; i-- !== 0; ) if ((a as any)[i] !== (b as any)[i]) return false;
return true;
}
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
keys = Object.keys(a);
length = keys.length;
if (length !== Object.keys(b).length) return false;
for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
for (i = length; i-- !== 0; ) {
// eslint-disable-next-line no-var
var key = keys[i];
if (!isEqual(a[key], b[key])) return false;
}
return true;
}
// true if both NaN, false otherwise
// eslint-disable-next-line no-self-compare
return a !== a && b !== b;
}