-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
type-in-test.js
154 lines (123 loc) · 4.05 KB
/
type-in-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
import { module, test } from 'qunit';
import { typeIn, setupContext, teardownContext } from '@ember/test-helpers';
import { buildInstrumentedElement } from '../../helpers/events';
import { isIE11 } from '../../helpers/browser-detect';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
/*
* Event order based on https://jsbin.com/zitazuxabe/edit?html,js,console,output
*/
let expectedEvents = [
'focus',
'focusin',
'keydown',
'keypress',
'input',
'keyup',
'keydown',
'keypress',
'input',
'keyup',
'keydown',
'keypress',
'input',
'keyup',
'change',
];
if (isIE11) {
expectedEvents = [
'focusin',
'keydown',
'keypress',
'keyup',
'keydown',
'keypress',
'keyup',
'keydown',
'keypress',
'keyup',
'input',
'change',
'focus',
];
}
module('DOM Helper: typeIn', function(hooks) {
if (!hasEmberVersion(2, 4)) {
return;
}
let context, element;
hooks.beforeEach(function() {
context = {};
});
hooks.afterEach(async function() {
element.setAttribute('data-skip-steps', true);
if (element) {
element.parentNode.removeChild(element);
}
if (context.owner) {
await teardownContext(context);
}
document.getElementById('ember-testing').innerHTML = '';
});
test('filling in an input', async function(assert) {
element = buildInstrumentedElement('input');
await typeIn(element, 'foo');
assert.verifySteps(expectedEvents);
assert.strictEqual(document.activeElement, element, 'activeElement updated');
assert.equal(element.value, 'foo');
});
test('it triggers key events with correct arguments', async function(assert) {
element = buildInstrumentedElement('input', ['key', 'charCode']);
await typeIn(element, 'foo');
let chars = ['f', 'o', 'o'];
let expectedEventsWithArguments = expectedEvents.map(eventName => {
// Only key events get the key arguments
if (!['keydown', 'keypress', 'keyup'].includes(eventName)) {
return `${eventName} undefined undefined`;
}
// After each keyup, the next character comes up
let char = eventName === 'keyup' ? chars.shift() : chars[0];
return `${eventName} ${char} ${char.charCodeAt()}`;
});
assert.verifySteps(expectedEventsWithArguments);
});
test('filling in an input with a delay', async function(assert) {
element = buildInstrumentedElement('input');
await typeIn(element, 'foo', { delay: 150 });
assert.verifySteps(expectedEvents);
assert.strictEqual(document.activeElement, element, 'activeElement updated');
assert.equal(element.value, 'foo');
});
test('filling in a textarea', async function(assert) {
element = buildInstrumentedElement('textarea');
await typeIn(element, 'foo');
assert.verifySteps(expectedEvents);
assert.strictEqual(document.activeElement, element, 'activeElement updated');
assert.equal(element.value, 'foo');
});
test('filling in a non-fillable element', async function(assert) {
element = buildInstrumentedElement('div');
await setupContext(context);
assert.rejects(typeIn(`#${element.id}`, 'foo'), /`typeIn` is only usable on form controls/);
});
test('rejects if selector is not found', async function(assert) {
element = buildInstrumentedElement('div');
await setupContext(context);
assert.rejects(
typeIn(`#foo-bar-baz-not-here-ever-bye-bye`, 'foo'),
/Element not found when calling `typeIn\('#foo-bar-baz-not-here-ever-bye-bye'\)`/
);
});
test('rejects if text to fill in is not provided', async function(assert) {
element = buildInstrumentedElement('input');
assert.rejects(typeIn(element), /Must provide `text` when calling `typeIn`/);
});
test('does not run sync', async function(assert) {
element = buildInstrumentedElement('input');
let promise = typeIn(element, 'foo');
assert.verifySteps([]);
await promise;
assert.verifySteps(expectedEvents);
assert.strictEqual(document.activeElement, element, 'activeElement updated');
assert.equal(element.value, 'foo');
});
});