-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-input-test.js
172 lines (143 loc) · 5.92 KB
/
search-input-test.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
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
(function() {
"use strict";
const names = [];
const logListeners = [];
// redefine custom elements define
const defineElement = window.customElements.define;
window.customElements.define = function(name, cls) {
names.push(name);
defineElement.call(window.customElements, name, cls);
};
// define log event to capture input
const log = console.log;
console.log = function() {
const args = Array.from(arguments).join('');
logListeners.forEach(listener => listener(args));
log.apply(console, arguments);
};
if (window.addEventListener) {
window.addEventListener('load', ready, false);
} else if (window.attachEvent) {
window.attachEvent('onload', ready);
}
function captureLogs() {
let data = '';
const listener = function(output) {
data += output;
};
logListeners.push(listener);
return function() {
const index = logListeners.indexOf(listener);
if (index !== -1) logListeners.splice(index, 1);
return data;
}
}
function getButton(component) {
const shadowRoot = document.querySelector('search-input').shadowRoot;
if (!shadowRoot) throw Error('You must use the shadow DOM on your component.');
return shadowRoot.querySelector('button');
}
function getInputField(component) {
const shadowRoot = document.querySelector('search-input').shadowRoot;
if (!shadowRoot) throw Error('You must use the shadow DOM on your component.');
return shadowRoot.querySelector('input[type="text"]') || shadowRoot.querySelector('input');
}
function randomValue() {
return '' + Math.random();
}
function readInputValue(component) {
const input = getInputField(component);
return input.value;
}
function runTests(component) {
const score = {
pass: 0,
fail: 0
};
let value = '';
console.log('Testing component: ', component);
test(score, 'Component has a text input field', () => {
const input = getInputField(component);
if (!input) throw Error('Input field does not exist');
});
test(score, 'Component has a button', () => {
const btn = getButton(component);
if (!btn) throw Error('Button does not exist');
});
value = randomValue();
test(score, 'Can set value via property', () => {
component.value = value;
if (readInputValue(component) !== value) throw Error('Not set to value: ' + value);
});
value = randomValue();
test(score, 'HTML attribute set to value', () => {
component.value = value;
if (component.getAttribute('value') !== value) throw Error('Not set to value: ' + value);
});
value = randomValue();
test(score, 'Can get value from property', () => {
component.value = value;
if (component.value !== value) throw Error('Could not get from value');
});
value = randomValue();
test(score, 'Can get value from attribute', () => {
component.value = value;
if (component.getAttribute('value') !== value) throw Error('Could not get from value');
});
value = randomValue();
test(score, 'Pressing enter while in input logs search term to console.log', () => {
const input = getInputField();
input.value = value;
const unlisten = captureLogs();
const kd = new KeyboardEvent('keydown', { key: 13, code: 13, charCode: 13, keyCode: 13, which: 13 });
input.dispatchEvent(kd);
const kp = new KeyboardEvent('keypress', { key: 13, code: 13, charCode: 13, keyCode: 13, which: 13 });
input.dispatchEvent(kp);
const ku = new KeyboardEvent('keyup', { key: 13, code: 13, charCode: 13, keyCode: 13, which: 13 });
input.dispatchEvent(ku);
const out = unlisten();
if (out.indexOf(value) === -1) throw Error('Did not capture log output');
});
value = randomValue();
test(score, 'Clicking button logs search term to console.log', () => {
const input = getInputField();
input.value = value;
const unlisten = captureLogs();
const event = new MouseEvent('click', {});
getButton().dispatchEvent(event);
const out = unlisten();
if (out.indexOf(value) === -1) throw Error('Did not capture log output');
});
const percentage = score.pass / (score.pass + score.fail);
console.info('=== FINAL RESULTS ===\nPassed: ' + score.pass + '\nFailed: ' + score.fail +
'\nPercent: ' + Math.round(100 * percentage) + '%' +
'\nScore: ' + Math.round(25 * percentage) + ' / 25');
}
function ready() {
if (names.length === 0) {
console.error('You did not define any custom elements.');
} else if (names.length > 1) {
console.warn('The automated test runner only works if you have only defined one custom element. ' +
'You have defined ' + names.length + ' so you must manually run the tester by calling ' +
'"window.it410Test(el)" where el is your component to test.');
} else {
const component = document.querySelector(names[0]);
if (!component) {
console.error('You must have one instance of your component in your index.html file')
} else {
runTests(component);
}
}
}
function test(score, message, fn) {
try {
fn();
score.pass++;
console.info(message);
} catch (e) {
score.fail++;
console.error(message + '\n', e.stack);
}
}
window.it410Test = runTests;
})();