Skip to content

Commit d290048

Browse files
authored
test: replace some text-field unit tests with snapshot tests (#4056)
1 parent 9720d8a commit d290048

4 files changed

Lines changed: 71 additions & 92 deletions

File tree

‎packages/field-base/test/field-mixin.test.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ describe('field-mixin', () => {
107107
expect(element.hasAttribute('has-error-message')).to.be.false;
108108
});
109109

110+
it('should not set has-error-message attribute when the property is an empty string', () => {
111+
element.errorMessage = '';
112+
expect(element.hasAttribute('has-error-message')).to.be.false;
113+
});
114+
115+
it('should not set has-error-message attribute when the property is null', () => {
116+
element.errorMessage = null;
117+
expect(element.hasAttribute('has-error-message')).to.be.false;
118+
});
119+
110120
it('should set has-error-message attribute when attribute is set', () => {
111121
element.setAttribute('error-message', 'This field is required');
112122
expect(element.hasAttribute('has-error-message')).to.be.true;
@@ -325,6 +335,11 @@ describe('field-mixin', () => {
325335
element.helperText = '';
326336
expect(element.hasAttribute('has-helper')).to.be.false;
327337
});
338+
339+
it('should remove has-helper attribute when property is null', () => {
340+
element.helperText = null;
341+
expect(element.hasAttribute('has-helper')).to.be.false;
342+
});
328343
});
329344

330345
describe('attribute', () => {

‎packages/text-field/test/dom/__snapshots__/text-field.test.snap.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,28 @@ snapshots["vaadin-text-field shadow theme"] =
316316
`;
317317
/* end snapshot vaadin-text-field shadow theme */
318318

319+
snapshots["vaadin-text-field host label"] =
320+
`<vaadin-text-field has-label="">
321+
<label
322+
for="input-vaadin-text-field-3"
323+
id="label-vaadin-text-field-0"
324+
slot="label"
325+
>
326+
Label
327+
</label>
328+
<div
329+
hidden=""
330+
id="error-message-vaadin-text-field-2"
331+
slot="error-message"
332+
>
333+
</div>
334+
<input
335+
aria-labelledby="label-vaadin-text-field-0"
336+
id="input-vaadin-text-field-3"
337+
slot="input"
338+
type="text"
339+
>
340+
</vaadin-text-field>
341+
`;
342+
/* end snapshot vaadin-text-field host label */
343+

‎packages/text-field/test/dom/text-field.test.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ describe('vaadin-text-field', () => {
1616
await expect(field).dom.to.equalSnapshot();
1717
});
1818

19+
it('label', async () => {
20+
field.label = 'Label';
21+
await expect(field).dom.to.equalSnapshot();
22+
});
23+
1924
it('helper', async () => {
2025
field.helperText = 'Helper';
2126
await expect(field).dom.to.equalSnapshot();

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

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

@@ -128,94 +128,11 @@ describe('text-field', () => {
128128
expect(textField.value).to.be.equal('foo');
129129
});
130130

131-
it('setting input value updates has-value attribute', () => {
132-
textField.value = 'foo';
133-
expect(textField.hasAttribute('has-value')).to.be.true;
134-
});
135-
136-
it('setting value to undefined should not update has-value attribute', () => {
137-
textField.value = undefined;
138-
expect(textField.hasAttribute('has-value')).to.be.false;
139-
});
140-
141131
it('setting value to undefined should clear the native input value', () => {
142132
textField.value = 'foo';
143133
textField.value = undefined;
144134
expect(input.value).to.equal('');
145135
});
146-
147-
it('setting empty value does not update has-value attribute', () => {
148-
textField.value = '';
149-
expect(textField.hasAttribute('has-value')).to.be.false;
150-
});
151-
152-
// User could accidentally set a 0 or false value
153-
it('setting number value updates has-value attribute', () => {
154-
textField.value = 0;
155-
expect(textField.hasAttribute('has-value')).to.be.true;
156-
});
157-
158-
it('setting boolean value updates has-value attribute', () => {
159-
textField.value = false;
160-
expect(textField.hasAttribute('has-value')).to.be.true;
161-
});
162-
163-
it('setting label updates has-label attribute', () => {
164-
textField.label = 'foo';
165-
expect(textField.hasAttribute('has-label')).to.be.true;
166-
});
167-
168-
it('setting label to empty string does not update has-label attribute', () => {
169-
textField.label = '';
170-
expect(textField.hasAttribute('has-label')).to.be.false;
171-
});
172-
173-
it('setting label to null does not update has-label attribute', () => {
174-
textField.label = null;
175-
expect(textField.hasAttribute('has-label')).to.be.false;
176-
});
177-
178-
it('setting helper updates has-helper attribute', () => {
179-
textField.helperText = 'foo';
180-
expect(textField.hasAttribute('has-helper')).to.be.true;
181-
});
182-
183-
it('setting helper to empty string does not update has-helper attribute', () => {
184-
textField.helperText = '';
185-
expect(textField.hasAttribute('has-helper')).to.be.false;
186-
});
187-
188-
it('setting helper to null does not update has-helper attribute', () => {
189-
textField.helperText = null;
190-
expect(textField.hasAttribute('has-helper')).to.be.false;
191-
});
192-
193-
it('setting helper with slot updates has-helper attribute', async () => {
194-
const helper = document.createElement('div');
195-
helper.setAttribute('slot', 'helper');
196-
helper.textContent = 'foo';
197-
textField.appendChild(helper);
198-
await nextFrame();
199-
expect(textField.hasAttribute('has-helper')).to.be.true;
200-
});
201-
202-
it('setting errorMessage updates has-error-message attribute', () => {
203-
textField.invalid = true;
204-
textField.errorMessage = 'foo';
205-
expect(textField.hasAttribute('has-error-message')).to.be.true;
206-
});
207-
208-
it('setting errorMessage to empty string does not update has-error-message attribute', () => {
209-
textField.invalid = true;
210-
textField.errorMessage = '';
211-
expect(textField.hasAttribute('has-error-message')).to.be.false;
212-
});
213-
214-
it('setting errorMessage to null does not update has-error-message attribute', () => {
215-
textField.invalid = true;
216-
textField.errorMessage = null;
217-
expect(textField.hasAttribute('has-error-message')).to.be.false;
218-
});
219136
});
220137

221138
describe('required', () => {
@@ -348,6 +265,31 @@ describe('text-field', () => {
348265
});
349266
});
350267

268+
describe('has-value attribute', () => {
269+
it('should toggle the attribute on value change', () => {
270+
textField.value = 'foo';
271+
expect(textField.hasAttribute('has-value')).to.be.true;
272+
textField.value = undefined;
273+
expect(textField.hasAttribute('has-value')).to.be.false;
274+
});
275+
276+
it('should not add the attribute when the value is an empty string', () => {
277+
textField.value = '';
278+
expect(textField.hasAttribute('has-value')).to.be.false;
279+
});
280+
281+
// User could accidentally set a 0 or false value
282+
it('should add the attribute when the value is a number', () => {
283+
textField.value = 0;
284+
expect(textField.hasAttribute('has-value')).to.be.true;
285+
});
286+
287+
it('should add the attribute when the value is a boolean', () => {
288+
textField.value = false;
289+
expect(textField.hasAttribute('has-value')).to.be.true;
290+
});
291+
});
292+
351293
describe('value property', () => {
352294
it('should not consider updating the value as user input if the value is not changed', () => {
353295
const event = new Event('input', {
@@ -387,12 +329,4 @@ describe('text-field', () => {
387329
expect(input.value).to.equal('');
388330
});
389331
});
390-
391-
describe('theme attribute', () => {
392-
it('should propagate theme attribute to input container', () => {
393-
const container = textField.shadowRoot.querySelector('[part="input-field"]');
394-
textField.setAttribute('theme', 'align-center');
395-
expect(container.getAttribute('theme')).to.equal('align-center');
396-
});
397-
});
398332
});

0 commit comments

Comments
 (0)