Skip to content

Commit c7ed565

Browse files
committed
test(textarea): Added defaultValue tests
1 parent a1158f7 commit c7ed565

File tree

1 file changed

+120
-47
lines changed

1 file changed

+120
-47
lines changed

src/components/textarea/textarea.spec.ts

Lines changed: 120 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import type { TemplateResult } from 'lit';
1111
import { configureTheme } from '../../theming/config.js';
1212
import { defineComponents } from '../common/definitions/defineComponents.js';
1313
import {
14-
FormAssociatedTestBed,
1514
type ValidationContainerTestsParams,
15+
createFormAssociatedTestBed,
1616
isFocused,
1717
runValidationContainerTests,
1818
simulateInput,
@@ -224,99 +224,172 @@ describe('Textarea component', () => {
224224
});
225225

226226
describe('Form integration', () => {
227-
const spec = new FormAssociatedTestBed<IgcTextareaComponent>(
227+
const spec = createFormAssociatedTestBed<IgcTextareaComponent>(
228228
html`<igc-textarea name="textarea"></igc-textarea>`
229229
);
230230

231231
beforeEach(async () => {
232232
await spec.setup(IgcTextareaComponent.tagName);
233233
});
234234

235-
it('is form associated', async () => {
235+
it('is form associated', () => {
236236
expect(spec.element.form).to.equal(spec.form);
237237
});
238238

239-
it('is not associated on submit if no value', async () => {
240-
expect(spec.submit()?.get(spec.element.name)).to.be.null;
239+
it('is not associated on submit if no value', () => {
240+
spec.assertSubmitHasValue(null);
241241
});
242242

243-
it('is associated on submit', async () => {
244-
spec.element.value = 'abc';
245-
await elementUpdated(spec.element);
246-
247-
expect(spec.submit()?.get(spec.element.name)).to.equal(
248-
spec.element.value
249-
);
243+
it('is associated on submit', () => {
244+
spec.setProperties({ value: 'abc' });
245+
spec.assertSubmitHasValue(spec.element.value);
250246
});
251247

252-
it('is correctly reset on form reset', async () => {
253-
spec.element.value = 'abc';
254-
await elementUpdated(spec.element);
248+
it('is correctly reset on form reset', () => {
249+
spec.setProperties({ value: 'abc' });
255250

256251
spec.reset();
257252
expect(spec.element.value).to.be.empty;
258253
});
259254

260255
it('is correctly reset on form reset after setAttribute() call', () => {
261-
spec.element.setAttribute('value', 'Some initial value');
262-
spec.element.value = '123';
256+
spec.setAttributes({ value: 'Some initial value' });
257+
spec.setProperties({ value: '123' });
263258

264259
spec.reset();
265260

266261
expect(spec.element.value).to.equal('Some initial value');
267-
expect(spec.submit()?.get(spec.element.name)).to.equal(
268-
spec.element.value
269-
);
262+
spec.assertSubmitHasValue('Some initial value');
270263
});
271264

272-
it('reflects disabled ancestor state', async () => {
265+
it('reflects disabled ancestor state', () => {
273266
spec.setAncestorDisabledState(true);
274267
expect(spec.element.disabled).to.be.true;
275268

276269
spec.setAncestorDisabledState(false);
277270
expect(spec.element.disabled).to.be.false;
278271
});
279272

280-
it('fulfils required constraint', async () => {
281-
spec.element.required = true;
282-
await elementUpdated(spec.element);
283-
spec.submitFails();
273+
it('fulfils required constraint', () => {
274+
spec.setProperties({ required: true });
275+
spec.assertSubmitFails();
284276

285-
spec.element.value = 'abc';
286-
await elementUpdated(spec.element);
287-
spec.submitValidates();
277+
spec.setProperties({ value: 'abc' });
278+
spec.assertSubmitPasses();
288279
});
289280

290-
it('fulfils min length constraint', async () => {
291-
spec.element.minLength = 4;
292-
spec.element.value = 'abc';
293-
await elementUpdated(spec.element);
281+
it('fulfils min length constraint', () => {
282+
spec.setProperties({
283+
minLength: 4,
284+
value: 'abc',
285+
});
294286

295-
spec.submitFails();
287+
spec.assertSubmitFails();
296288

297-
spec.element.value = 'abcd';
298-
await elementUpdated(spec.element);
299-
spec.submitValidates();
289+
spec.setProperties({ value: 'abcd' });
290+
spec.assertSubmitPasses();
300291
});
301292

302-
it('fulfils max length constraint', async () => {
303-
spec.element.maxLength = 3;
304-
spec.element.value = 'abcd';
305-
await elementUpdated(spec.element);
293+
it('fulfils max length constraint', () => {
294+
spec.setProperties({
295+
maxLength: 3,
296+
value: 'abcd',
297+
});
306298

307-
spec.submitFails();
299+
spec.assertSubmitFails();
308300

309-
spec.element.value = 'abc';
310-
await elementUpdated(spec.element);
311-
spec.submitValidates();
301+
spec.setProperties({ value: 'abc' });
302+
spec.assertSubmitPasses();
312303
});
313304

314-
it('fulfils custom constraint', async () => {
305+
it('fulfils custom constraint', () => {
315306
spec.element.setCustomValidity('invalid');
316-
spec.submitFails();
307+
spec.assertSubmitFails();
317308

318309
spec.element.setCustomValidity('');
319-
spec.submitValidates();
310+
spec.assertSubmitPasses();
311+
});
312+
});
313+
314+
describe('defaultValue', () => {
315+
describe('Form integration', () => {
316+
const spec = createFormAssociatedTestBed<IgcTextareaComponent>(html`
317+
<igc-textarea name="textarea" .defaultValue=${'Hello'}></igc-textarea>
318+
`);
319+
320+
beforeEach(async () => {
321+
await spec.setup(IgcTextareaComponent.tagName);
322+
});
323+
324+
it('correct initial state', () => {
325+
spec.assertIsPristine();
326+
expect(spec.element.value).to.equal('Hello');
327+
});
328+
329+
it('is correctly submitted', () => {
330+
spec.assertSubmitHasValue(spec.element.value);
331+
});
332+
333+
it('is correctly reset', () => {
334+
spec.setProperties({ value: 'World' });
335+
spec.reset();
336+
337+
expect(spec.element.value).to.equal('Hello');
338+
});
339+
});
340+
341+
describe('Validation', () => {
342+
const spec = createFormAssociatedTestBed<IgcTextareaComponent>(html`
343+
<igc-textarea
344+
name="textarea"
345+
required
346+
.defaultValue=${undefined}
347+
></igc-textarea>
348+
`);
349+
350+
beforeEach(async () => {
351+
await spec.setup(IgcTextareaComponent.tagName);
352+
});
353+
354+
it('fails required validation', () => {
355+
spec.assertIsPristine();
356+
spec.assertSubmitFails();
357+
});
358+
359+
it('passes required validation when updating defaultValue', () => {
360+
spec.setProperties({ defaultValue: 'Hello' });
361+
362+
spec.assertIsPristine();
363+
spec.assertSubmitPasses();
364+
});
365+
366+
it('fails minlength validation', () => {
367+
spec.setProperties({ minLength: 3, defaultValue: 'ab' });
368+
369+
spec.assertIsPristine();
370+
spec.assertSubmitFails();
371+
});
372+
373+
it('passes minlength validation', () => {
374+
spec.setProperties({ minLength: 3, defaultValue: 'abc' });
375+
376+
spec.assertIsPristine();
377+
spec.assertSubmitPasses();
378+
});
379+
380+
it('fails maxlength validation', () => {
381+
spec.setProperties({ maxLength: 3, defaultValue: 'abcd' });
382+
383+
spec.assertIsPristine();
384+
spec.assertSubmitFails();
385+
});
386+
387+
it('passes maxlength validation', () => {
388+
spec.setProperties({ maxLength: 3, defaultValue: 'abc' });
389+
390+
spec.assertIsPristine();
391+
spec.assertSubmitPasses();
392+
});
320393
});
321394
});
322395

0 commit comments

Comments
 (0)