-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemba-textinput.test.ts
200 lines (165 loc) · 6.48 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
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',
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');
});
});