forked from nyaruka/temba-components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
temba-textinput.test.ts
274 lines (227 loc) · 8.45 KB
/
temba-textinput.test.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
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
import { fixture, expect, assert } from '@open-wc/testing';
import { TextInput } from '../src/textinput/TextInput';
import { assertScreenshot, getAttributes, getClip } from './utils.test';
export const getInputHTML = (attrs: any = { value: 'hello world' }) => {
return `<temba-textinput ${getAttributes(attrs)}></temba-textinput>`;
};
export const createInput = async (def: string) => {
const parentNode = document.createElement('div');
parentNode.setAttribute('style', 'width: 250px;');
const input: TextInput = await fixture(def, { parentNode });
return input;
};
describe('temba-textinput', () => {
it('can be created', async () => {
const input: TextInput = await createInput(getInputHTML());
assert.instanceOf(input, TextInput);
await assertScreenshot('textinput/input', getClip(input));
});
it('shows placeholder', async () => {
const input: TextInput = await createInput(
getInputHTML({ placeholder: 'Enter some text' })
);
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.placeholder).to.equal('Enter some text');
await assertScreenshot('textinput/input-placeholder', getClip(input));
});
it('should focus inputs on click', async () => {
const input: TextInput = await createInput(getInputHTML());
await click('temba-textinput');
await assertScreenshot('textinput/input-focused', getClip(input));
});
it('should render textarea', async () => {
const input: TextInput = await createInput(
getInputHTML({ value: 'hello world', textarea: true })
);
assert.instanceOf(input, TextInput);
await assertScreenshot('textinput/textarea', getClip(input));
});
it('should focus textarea on click', async () => {
const input: TextInput = await createInput(
getInputHTML({ value: 'hello world', textarea: true })
);
await click('temba-textinput');
await assertScreenshot('textinput/textarea-focused', getClip(input));
});
it('takes internal input changes', async () => {
const input: TextInput = await createInput(
getInputHTML({ value: 'hello world' })
);
// trigger a change on our internal widget
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.tagName).to.equal('INPUT');
expect(widget.disabled).to.equal(false);
// focus our widget, move back a few spots and insert some text
await click('temba-textinput');
await pressKey('ArrowLeft', 5);
await type('to the ');
// should be reflected on our main input
expect(input.value).to.equal('hello to the world');
});
it('does not take internal input changes for disabled', async () => {
const input: TextInput = await createInput(
getInputHTML({ value: 'hello world', disabled: true })
);
// trigger a change on our internal widget
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.tagName).to.equal('INPUT');
expect(widget.disabled).to.equal(true);
// focus our widget, move back a few spots and insert some text
await click('temba-textinput');
await pressKey('ArrowLeft', 5);
await type('to the ');
// should be reflected on our main input
expect(input.value).to.equal('hello world');
await assertScreenshot('textinput/input-disabled', getClip(input));
});
it('takes internal textarea changes', async () => {
const input: TextInput = await createInput(
getInputHTML({ value: 'hello world', textarea: true })
);
// trigger a change on our internal widget
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.tagName).to.equal('TEXTAREA');
expect(widget.disabled).to.equal(false);
// focus our widget, move back a few spots and insert some text
await click('temba-textinput');
await pressKey('ArrowLeft', 5);
await type('to the ');
// should be reflected on our main input
expect(input.value).to.equal('hello to the world');
});
it('does not take internal textarea changes for disabled', async () => {
const input: TextInput = await fixture(
getInputHTML({ value: 'hello world', textarea: true, disabled: true })
);
// trigger a change on our internal widget
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.tagName).to.equal('TEXTAREA');
expect(widget.disabled).to.equal(true);
// focus our widget, move back a few spots and insert some text
await click('temba-textinput');
await pressKey('ArrowLeft', 5);
await type('to the ');
// should be reflected on our main input
expect(input.value).to.equal('hello world');
});
it("doesn't advance cursor on GSM character replacement", async () => {
const input: TextInput = await createInput(
getInputHTML({ value: 'hello world', textarea: true, gsm: true })
);
input.value = 'Let’s try some text with a funny tick.';
// focus our widget, move back a few spots and insert some text
await click('temba-textinput');
await pressKey('ArrowLeft', 5);
await type('replaced ');
expect(input.value).to.equal(
"Let's try some text with a funny replaced tick."
);
});
it("doesn't move cursor to the end on insert in input", async () => {
const input: TextInput = await createInput(
getInputHTML({ value: 'hello world' })
);
// focus our widget, move back a few spots and insert some text
await click('temba-textinput');
await pressKey('ArrowLeft', 5);
await type('sad, sad ');
expect(input.value).to.equal('hello sad, sad world');
await assertScreenshot('textinput/input-inserted', getClip(input));
});
it('shows form attributes', async () => {
const input: TextInput = await createInput(
getInputHTML({
name: 'message',
value: 'hello world',
label: 'Your Message',
// eslint-disable-next-line @typescript-eslint/camelcase
help_text: 'Enter your message here',
})
);
await assertScreenshot('textinput/input-form', getClip(input));
});
it('updates input value', async () => {
const input: TextInput = await createInput(
getInputHTML({
value: 'hello world',
})
);
input.value = 'Updated by attribute change';
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
await assertScreenshot('textinput/input-updated', getClip(input));
expect(widget.value).to.equal('Updated by attribute change');
});
it('shows datepicker placeholder in a form', async () => {
const input: TextInput = await createInput(
getInputHTML({
name: 'Date',
label: 'Your Date',
datepicker: true,
placeholder: 'Select a date',
// eslint-disable-next-line @typescript-eslint/camelcase
help_text: 'Dates can be helpful',
})
);
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.placeholder).to.equal('Select a date');
await assertScreenshot('textinput/date-form', getClip(input));
});
it('shows initialized date', async () => {
const input: TextInput = await createInput(
getInputHTML({
datepicker: true,
placeholder: 'Select a date',
value: '2020-04-20',
})
);
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.value).to.equal('2020-04-20');
await waitFor(500);
await click('temba-textinput');
await waitFor(500);
const clip = getClip(input);
// account for the portaled date picker
clip.height += 325;
clip.width += 55;
await assertScreenshot('textinput/date-initialized', clip);
});
it('updates on date selection', async () => {
const input: TextInput = await createInput(
getInputHTML({
datepicker: true,
placeholder: 'Select a date',
value: '2020-04-20',
})
);
// open our picker
await waitFor(500);
await click('temba-textinput');
await waitFor(500);
// click on a new date
const newDate = document.querySelectorAll(
".flatpickr-day[aria-label='April 21, 2020']"
)[1] as HTMLSpanElement;
newDate.click();
// make sure it updated
const widget = input.shadowRoot.querySelector(
'.textinput'
) as HTMLInputElement;
expect(widget.value).to.equal('April 21, 2020');
});
});