Skip to content

Commit 43ed994

Browse files
authored
test: update text-area tests to allow async property updates (#5528)
1 parent 054024a commit 43ed994

2 files changed

Lines changed: 87 additions & 52 deletions

File tree

‎packages/text-area/test/text-area.test.js‎

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ describe('text-area', () => {
2424
});
2525

2626
describe('native', () => {
27-
function assertAttrCanBeSet(prop, value) {
27+
async function assertAttrCanBeSet(prop, value) {
2828
textArea[prop] = value;
29+
await nextFrame();
30+
2931
const attrValue = native.getAttribute(prop);
3032

3133
if (value === true) {
@@ -37,54 +39,53 @@ describe('text-area', () => {
3739
}
3840
}
3941

40-
function assertPropCanBeSet(prop, value) {
41-
for (let i = 0; i < 3; i++) {
42-
// Check different values
43-
const newValue = typeof value === 'boolean' ? i % 2 === 0 : value + i;
44-
textArea[prop] = newValue;
45-
expect(native[prop]).to.be.equal(newValue);
46-
}
47-
}
48-
4942
['placeholder', 'value'].forEach((prop) => {
50-
it(`should set string property ${prop}`, () => {
51-
assertPropCanBeSet(prop, 'foo');
43+
it(`should set string property ${prop}`, async () => {
44+
textArea[prop] = 'foo';
45+
await nextFrame();
46+
expect(native[prop]).to.be.equal('foo');
5247
});
5348
});
5449

5550
['disabled'].forEach((prop) => {
56-
it(`should set boolean property ${prop}`, () => {
57-
assertPropCanBeSet(prop, true);
51+
it(`should set boolean property ${prop}`, async () => {
52+
textArea[prop] = true;
53+
await nextFrame();
54+
expect(native[prop]).to.be.true;
55+
56+
textArea[prop] = false;
57+
await nextFrame();
58+
expect(native[prop]).to.be.false;
5859
});
5960
});
6061

6162
['maxlength', 'minlength'].forEach((prop) => {
62-
it(`should set numeric attribute ${prop}`, () => {
63-
assertAttrCanBeSet(prop, 2);
63+
it(`should set numeric attribute ${prop}`, async () => {
64+
await assertAttrCanBeSet(prop, 2);
6465
});
6566
});
6667

6768
['autocomplete'].forEach((prop) => {
68-
it(`should set boolean attribute ${prop}`, () => {
69-
assertAttrCanBeSet(prop, 'on');
69+
it(`should set boolean attribute ${prop}`, async () => {
70+
await assertAttrCanBeSet(prop, 'on');
7071
});
7172
});
7273

7374
['autocapitalize'].forEach((prop) => {
74-
it(`should set boolean attribute ${prop}`, () => {
75-
assertAttrCanBeSet(prop, 'none');
75+
it(`should set boolean attribute ${prop}`, async () => {
76+
await assertAttrCanBeSet(prop, 'none');
7677
});
7778
});
7879

7980
['autocomplete', 'autocorrect', 'readonly', 'required'].forEach((prop) => {
80-
it(`should set boolean attribute ${prop}`, () => {
81-
assertAttrCanBeSet(prop, true);
82-
assertAttrCanBeSet(prop, false);
81+
it(`should set boolean attribute ${prop}`, async () => {
82+
await assertAttrCanBeSet(prop, true);
83+
await assertAttrCanBeSet(prop, false);
8384
});
8485
});
8586
});
8687

87-
describe('binding', () => {
88+
describe('value', () => {
8889
it('should set default value to empty string', () => {
8990
expect(textArea.value).to.be.equal('');
9091
});
@@ -94,29 +95,34 @@ describe('text-area', () => {
9495
expect(textArea.value).to.be.equal('foo');
9596
});
9697

97-
it('should update has-value attribute when value is set', () => {
98+
it('should update has-value attribute when value is set', async () => {
9899
textArea.value = 'foo';
100+
await nextFrame();
99101
expect(textArea.hasAttribute('has-value')).to.be.true;
100102
});
101103

102-
it('should not update has-value attribute when value is set to undefined', () => {
104+
it('should not update has-value attribute when value is set to undefined', async () => {
103105
textArea.value = undefined;
106+
await nextFrame();
104107
expect(textArea.hasAttribute('has-value')).to.be.false;
105108
});
106109

107-
it('should not update has-value attribute when value is set to empty string', () => {
110+
it('should not update has-value attribute when value is set to empty string', async () => {
108111
textArea.value = '';
112+
await nextFrame();
109113
expect(textArea.hasAttribute('has-value')).to.be.false;
110114
});
111115

112116
// User could accidentally set a 0 or false value
113-
it('should update has-value attribute when numeric value is set', () => {
117+
it('should update has-value attribute when numeric value is set', async () => {
114118
textArea.value = 0;
119+
await nextFrame();
115120
expect(textArea.hasAttribute('has-value')).to.be.true;
116121
});
117122

118-
it('should update has-value attribute when boolean value is set', () => {
123+
it('should update has-value attribute when boolean value is set', async () => {
119124
textArea.value = false;
125+
await nextFrame();
120126
expect(textArea.hasAttribute('has-value')).to.be.true;
121127
});
122128
});
@@ -151,17 +157,18 @@ describe('text-area', () => {
151157
container = textArea.shadowRoot.querySelector('.vaadin-text-area-container');
152158
});
153159

154-
it('should grow height with unwrapped text', () => {
160+
it('should grow height with unwrapped text', async () => {
155161
const originalHeight = parseInt(window.getComputedStyle(inputField).height);
156162

157163
// Make sure there are enough characters to grow the textarea
158164
textArea.value = Array(400).join('400');
165+
await nextFrame();
159166

160167
const newHeight = parseInt(window.getComputedStyle(inputField).height);
161168
expect(newHeight).to.be.at.least(originalHeight + 10);
162169
});
163170

164-
it('should not grow over max-height', () => {
171+
it('should not grow over max-height', async () => {
165172
inputField.style.padding = '0';
166173
inputField.style.border = 'none';
167174
textArea.style.maxHeight = '100px';
@@ -174,13 +181,14 @@ describe('text-area', () => {
174181
lot
175182
of
176183
rows`;
184+
await nextFrame();
177185

178186
expect(parseFloat(window.getComputedStyle(textArea).height)).to.be.lte(100);
179187
expect(parseFloat(window.getComputedStyle(container).height)).to.be.lte(100);
180188
expect(parseFloat(window.getComputedStyle(inputField).height)).to.be.lte(100);
181189
});
182190

183-
it('should not shrink less than min-height', () => {
191+
it('should not shrink less than min-height', async () => {
184192
textArea.style.minHeight = '125px';
185193

186194
expect(window.getComputedStyle(textArea).height).to.be.equal('125px');
@@ -189,13 +197,14 @@ describe('text-area', () => {
189197

190198
// Check that value modification doesn't break min-height rule
191199
textArea.value = '1 row';
200+
await nextFrame();
192201

193202
expect(window.getComputedStyle(textArea).height).to.be.equal('125px');
194203
expect(window.getComputedStyle(container).height).to.be.equal('125px');
195204
expect(parseFloat(window.getComputedStyle(inputField).height)).to.be.above(100);
196205
});
197206

198-
it('should stay between min and max height', () => {
207+
it('should stay between min and max height', async () => {
199208
textArea.style.minHeight = '100px';
200209
textArea.style.maxHeight = '175px';
201210

@@ -216,30 +225,40 @@ describe('text-area', () => {
216225
and
217226
even
218227
more`;
228+
await nextFrame();
219229

220230
expect(window.getComputedStyle(textArea).height).to.be.equal('175px');
221231
expect(window.getComputedStyle(container).height).to.be.equal('175px');
222232
expect(parseFloat(window.getComputedStyle(inputField).height)).to.be.above(150);
223233
});
224234

225-
it('should increase inputField height', () => {
235+
it('should increase input container height', async () => {
226236
textArea.style.height = '200px';
227237
textArea.value = 'foo';
238+
await nextFrame();
228239
expect(inputField.clientHeight).to.be.closeTo(200, 10);
229240
});
230241

231-
it('should maintain scroll top', () => {
242+
it('should maintain scroll top', async () => {
232243
textArea.style.maxHeight = '100px';
233244
textArea.value = Array(400).join('400');
245+
await nextFrame();
246+
234247
inputField.scrollTop = 200;
235248
textArea.value += 'foo';
249+
await nextFrame();
250+
236251
expect(inputField.scrollTop).to.equal(200);
237252
});
238253

239-
it('should decrease height automatically', () => {
254+
it('should decrease height automatically', async () => {
240255
textArea.value = Array(400).join('400');
256+
await nextFrame();
257+
241258
const height = textArea.clientHeight;
242259
textArea.value = '';
260+
await nextFrame();
261+
243262
expect(textArea.clientHeight).to.be.below(height);
244263
});
245264

@@ -271,21 +290,26 @@ describe('text-area', () => {
271290
);
272291
});
273292

274-
it('should have matching height', () => {
293+
it('should have matching height', async () => {
275294
inputField.style.padding = '0';
276295
textArea.style.maxHeight = '100px';
277296

278297
textArea.value = Array(400).join('400');
298+
await nextFrame();
299+
279300
textArea.value = textArea.value.slice(0, -1);
301+
await nextFrame();
280302
expect(native.clientHeight).to.equal(inputField.scrollHeight);
281303
});
282304

283-
it('should cover native field', () => {
305+
it('should cover native textarea', async () => {
284306
inputField.style.padding = '0';
285307
inputField.style.border = 'none';
286308
textArea.style.minHeight = '300px';
287309
textArea.style.padding = '0';
310+
288311
textArea.value = 'foo';
312+
await nextFrame();
289313

290314
expect(native.clientHeight).to.equal(
291315
Math.round(
@@ -315,9 +339,10 @@ describe('text-area', () => {
315339
.style.getPropertyValue('--_text-area-vertical-scroll-position');
316340
}
317341

318-
beforeEach(() => {
342+
beforeEach(async () => {
319343
textArea.style.height = '100px';
320344
textArea.value = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz';
345+
await nextFrame();
321346
});
322347

323348
it('should be 0 initially', () => {

‎packages/text-area/test/validation.test.js‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
2+
import { fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import '../src/vaadin-text-area.js';
55

@@ -57,24 +57,26 @@ describe('validation', () => {
5757
});
5858

5959
describe('basic', () => {
60-
beforeEach(() => {
60+
let validatedSpy;
61+
62+
beforeEach(async () => {
6163
textArea = fixtureSync('<vaadin-text-area></vaadin-text-area>');
64+
await nextRender();
65+
validatedSpy = sinon.spy();
66+
textArea.addEventListener('validated', validatedSpy);
6267
});
6368

6469
it('should fire a validated event on validation success', () => {
65-
const validatedSpy = sinon.spy();
66-
textArea.addEventListener('validated', validatedSpy);
6770
textArea.validate();
6871

6972
expect(validatedSpy.calledOnce).to.be.true;
7073
const event = validatedSpy.firstCall.args[0];
7174
expect(event.detail.valid).to.be.true;
7275
});
7376

74-
it('should fire a validated event on validation failure', () => {
75-
const validatedSpy = sinon.spy();
76-
textArea.addEventListener('validated', validatedSpy);
77+
it('should fire a validated event on validation failure', async () => {
7778
textArea.required = true;
79+
await nextFrame();
7880
textArea.validate();
7981

8082
expect(validatedSpy.calledOnce).to.be.true;
@@ -84,37 +86,45 @@ describe('validation', () => {
8486
});
8587

8688
describe('minlength', () => {
87-
beforeEach(() => {
89+
beforeEach(async () => {
8890
textArea = fixtureSync('<vaadin-text-area></vaadin-text-area>');
91+
await nextRender();
8992
});
9093

91-
it('should not validate the field when minlength is set', () => {
94+
it('should not validate the field when minlength is set', async () => {
9295
textArea.minlength = 2;
96+
await nextFrame();
9397
expect(textArea.invalid).to.be.false;
9498
});
9599

96-
it('should validate the field when invalid after minlength is changed', () => {
100+
it('should validate the field when invalid after minlength is changed', async () => {
97101
textArea.invalid = true;
102+
await nextFrame();
98103
const spy = sinon.spy(textArea, 'validate');
99104
textArea.minlength = 2;
105+
await nextFrame();
100106
expect(spy.calledOnce).to.be.true;
101107
});
102108
});
103109

104110
describe('maxlength', () => {
105-
beforeEach(() => {
111+
beforeEach(async () => {
106112
textArea = fixtureSync('<vaadin-text-area></vaadin-text-area>');
113+
await nextRender();
107114
});
108115

109-
it('should not validate the field when maxlength is set', () => {
116+
it('should not validate the field when maxlength is set', async () => {
110117
textArea.maxlength = 6;
118+
await nextFrame();
111119
expect(textArea.invalid).to.be.false;
112120
});
113121

114-
it('should validate the field when invalid after maxlength is changed', () => {
122+
it('should validate the field when invalid after maxlength is changed', async () => {
115123
textArea.invalid = true;
124+
await nextFrame();
116125
const spy = sinon.spy(textArea, 'validate');
117126
textArea.maxlength = 6;
127+
await nextFrame();
118128
expect(spy.calledOnce).to.be.true;
119129
});
120130
});

0 commit comments

Comments
 (0)