-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathhelpers.js
101 lines (81 loc) · 2.88 KB
/
helpers.js
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
import { showToast, StdToastElement } from 'std:elements/toast';
// helper functions to keep tests from bleeding into each other
const runTest = (testFn, name, toast, action) => {
try {
test(() => {
testFn(toast, action);
}, name);
} finally {
toast.remove();
}
};
const runTestAsync = (testFn, name, toast) => {
async_test(t => {
testFn(t, toast);
t.add_cleanup(() => {
toast.remove();
});
}, name);
};
export const testToastElement = (testFn, name) => {
const toast = new StdToastElement('Message', {});
document.querySelector('main').appendChild(toast);
runTest(testFn, name, toast);
};
export const testToastElementAsync = (testFn, name) => {
const toast = new StdToastElement('Message', {});
document.querySelector('main').appendChild(toast);
runTestAsync(testFn, name, toast);
};
export const testShowToast = (testFn, name) => {
const toast = showToast("message");
runTest(testFn, name, toast);
};
export const testActionToast = (testFn, name) => {
const toast = new StdToastElement('Message', {});
const action = document.createElement('button');
action.setAttribute('slot', 'action');
action.textContent = 'action';
toast.appendChild(action);
document.querySelector('main').appendChild(toast);
runTest(testFn, name, toast, action);
};
export const assertToastShown = (toast) => {
assert_not_equals(window.getComputedStyle(toast).display, 'none');
assert_true(toast.hasAttribute('open'));
assert_true(toast.open);
};
export const assertToastNotShown = (toast) => {
assert_equals(window.getComputedStyle(toast).display, 'none');
assert_false(toast.hasAttribute('open'));
assert_false(toast.open);
};
export const assertActionButtonOnToast = (action, toast) => {
assert_equals(toast.action, action);
assert_equals(action.getAttribute('slot'), 'action');
assert_equals(action, toast.querySelector('button'));
};
export const assertComputedStyleMapsEqual = (element1, element2) => {
assert_greater_than(element1.computedStyleMap().size, 0);
for (const [styleProperty, baseStyleValues] of element1.computedStyleMap()) {
const refStyleValues = element2.computedStyleMap().getAll(styleProperty);
assert_equals(baseStyleValues.length, refStyleValues.length, `${styleProperty} length`);
for (let i = 0; i < baseStyleValues.length; ++i) {
const baseStyleValue = baseStyleValues[i];
const refStyleValue = refStyleValues[i];
assert_equals(baseStyleValue.toString(), refStyleValue.toString(), `diff at value ${styleProperty}`);
}
}
}
export class EventCollector {
events = [];
getCallback() {
return (e) => {this.events.push(e)};
}
getCount() {
return this.events.length;
}
getEvents() {
return this.events;
}
}