Skip to content

Commit 86800e4

Browse files
authored
test: replace some unit tests with snapshot tests (#4118)
1 parent e1e5b9e commit 86800e4

3 files changed

Lines changed: 188 additions & 90 deletions

File tree

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

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { aTimeout, fixtureSync, focusin, focusout } from '@vaadin/testing-helpers';
2+
import { fixtureSync, focusin, focusout } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import '../src/vaadin-custom-field.js';
55
import { dispatchChange } from './common.js';
@@ -69,33 +69,7 @@ describe('custom field', () => {
6969
});
7070
});
7171

72-
describe('label', () => {
73-
let labelElement;
74-
75-
beforeEach(() => {
76-
labelElement = customField.querySelector('[slot="label"]');
77-
});
78-
79-
it('should be undefined by default', () => {
80-
expect(customField.label).to.be.undefined;
81-
});
82-
83-
it('should be displayed when assigned', () => {
84-
customField.label = 'Foo';
85-
expect(labelElement.textContent).to.equal('Foo');
86-
});
87-
88-
it('should set has-label attribute on host element when displayed', () => {
89-
customField.label = 'Foo';
90-
expect(customField.hasAttribute('has-label')).to.be.true;
91-
});
92-
});
93-
9472
describe('aria-required', () => {
95-
it('should not have aria-required attribute by default', () => {
96-
expect(customField.hasAttribute('aria-required')).to.be.false;
97-
});
98-
9973
it('should toggle aria-required attribute on required property change', () => {
10074
customField.required = true;
10175
expect(customField.getAttribute('aria-required')).to.equal('true');
@@ -123,35 +97,6 @@ describe('custom field', () => {
12397
});
12498
});
12599

126-
describe('errorMessage', () => {
127-
let error;
128-
129-
beforeEach(() => {
130-
customField.invalid = true;
131-
error = customField.querySelector('[slot="error-message"]');
132-
});
133-
134-
it('should set error message text content when errorMessage is provided', () => {
135-
customField.errorMessage = 'foo';
136-
expect(error.textContent).to.equal('foo');
137-
});
138-
139-
it('should set has-error-message attribute when errorMessage is provided', () => {
140-
customField.errorMessage = 'foo';
141-
expect(customField.hasAttribute('has-error-message')).to.be.true;
142-
});
143-
144-
it('should not set has-error-message when errorMessage is set to empty string', () => {
145-
customField.errorMessage = '';
146-
expect(customField.hasAttribute('has-error-message')).to.be.false;
147-
});
148-
149-
it('should not set has-error-message when errorMessage is set to null', () => {
150-
customField.errorMessage = null;
151-
expect(customField.hasAttribute('has-error-message')).to.be.false;
152-
});
153-
});
154-
155100
describe('required', () => {
156101
beforeEach(() => {
157102
customField.required = true;
@@ -181,40 +126,6 @@ describe('custom field', () => {
181126
});
182127
});
183128

184-
describe('ARIA', () => {
185-
let label, helper, error;
186-
187-
beforeEach(() => {
188-
customField.helperText = 'Helper';
189-
label = customField.querySelector('[slot="label"]');
190-
helper = customField.querySelector('[slot="helper"]');
191-
error = customField.querySelector('[slot="error-message"]');
192-
customField.label = 'label';
193-
customField.helperText = 'helper';
194-
customField.errorMessage = 'error';
195-
});
196-
197-
it('should set correct role attribute on the custom field', () => {
198-
expect(customField.getAttribute('role')).to.equal('group');
199-
});
200-
201-
it('should add label and helper text to aria-labelledby when valid', () => {
202-
const aria = customField.getAttribute('aria-labelledby');
203-
expect(aria).to.include(helper.id);
204-
expect(aria).to.not.include(error.id);
205-
expect(aria).to.include(label.id);
206-
});
207-
208-
it('should add error message to aria-labelledby when field is invalid', async () => {
209-
customField.invalid = true;
210-
await aTimeout(0);
211-
const aria = customField.getAttribute('aria-labelledby');
212-
expect(aria).to.include(helper.id);
213-
expect(aria).to.include(error.id);
214-
expect(aria).to.include(label.id);
215-
});
216-
});
217-
218129
describe('validation', () => {
219130
it('should check validity on validate', () => {
220131
const spy = sinon.spy(customField, 'checkValidity');
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* @web/test-runner snapshot v1 */
2+
export const snapshots = {};
3+
4+
snapshots["vaadin-custom-field host default"] =
5+
`<vaadin-custom-field role="group">
6+
<label
7+
id="label-vaadin-custom-field-0"
8+
slot="label"
9+
>
10+
</label>
11+
<div
12+
hidden=""
13+
id="error-message-vaadin-custom-field-2"
14+
slot="error-message"
15+
>
16+
</div>
17+
</vaadin-custom-field>
18+
`;
19+
/* end snapshot vaadin-custom-field host default */
20+
21+
snapshots["vaadin-custom-field host label"] =
22+
`<vaadin-custom-field
23+
aria-labelledby="label-vaadin-custom-field-0"
24+
has-label=""
25+
role="group"
26+
>
27+
<label
28+
id="label-vaadin-custom-field-0"
29+
slot="label"
30+
>
31+
Label
32+
</label>
33+
<div
34+
hidden=""
35+
id="error-message-vaadin-custom-field-2"
36+
slot="error-message"
37+
>
38+
</div>
39+
</vaadin-custom-field>
40+
`;
41+
/* end snapshot vaadin-custom-field host label */
42+
43+
snapshots["vaadin-custom-field host required"] =
44+
`<vaadin-custom-field
45+
aria-required="true"
46+
required=""
47+
role="group"
48+
>
49+
<label
50+
id="label-vaadin-custom-field-0"
51+
slot="label"
52+
>
53+
</label>
54+
<div
55+
hidden=""
56+
id="error-message-vaadin-custom-field-2"
57+
slot="error-message"
58+
>
59+
</div>
60+
</vaadin-custom-field>
61+
`;
62+
/* end snapshot vaadin-custom-field host required */
63+
64+
snapshots["vaadin-custom-field host helper"] =
65+
`<vaadin-custom-field
66+
aria-labelledby="helper-vaadin-custom-field-1"
67+
has-helper=""
68+
role="group"
69+
>
70+
<label
71+
id="label-vaadin-custom-field-0"
72+
slot="label"
73+
>
74+
</label>
75+
<div
76+
hidden=""
77+
id="error-message-vaadin-custom-field-2"
78+
slot="error-message"
79+
>
80+
</div>
81+
<div
82+
id="helper-vaadin-custom-field-1"
83+
slot="helper"
84+
>
85+
Helper
86+
</div>
87+
</vaadin-custom-field>
88+
`;
89+
/* end snapshot vaadin-custom-field host helper */
90+
91+
snapshots["vaadin-custom-field host error"] =
92+
`<vaadin-custom-field
93+
aria-labelledby="error-message-vaadin-custom-field-2"
94+
has-error-message=""
95+
invalid=""
96+
role="group"
97+
>
98+
<label
99+
id="label-vaadin-custom-field-0"
100+
slot="label"
101+
>
102+
</label>
103+
<div
104+
id="error-message-vaadin-custom-field-2"
105+
role="alert"
106+
slot="error-message"
107+
>
108+
Error
109+
</div>
110+
</vaadin-custom-field>
111+
`;
112+
/* end snapshot vaadin-custom-field host error */
113+
114+
snapshots["vaadin-custom-field shadow default"] =
115+
`<div class="vaadin-custom-field-container">
116+
<div part="label">
117+
<slot name="label">
118+
</slot>
119+
<span
120+
aria-hidden="true"
121+
part="required-indicator"
122+
>
123+
</span>
124+
</div>
125+
<div class="inputs-wrapper">
126+
<slot id="slot">
127+
</slot>
128+
</div>
129+
<div part="helper-text">
130+
<slot name="helper">
131+
</slot>
132+
</div>
133+
<div part="error-message">
134+
<slot name="error-message">
135+
</slot>
136+
</div>
137+
</div>
138+
`;
139+
/* end snapshot vaadin-custom-field shadow default */
140+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { aTimeout, fixtureSync } from '@vaadin/testing-helpers';
3+
import '../../src/vaadin-custom-field.js';
4+
import { resetUniqueId } from '@vaadin/component-base/src/unique-id-utils.js';
5+
6+
describe('vaadin-custom-field', () => {
7+
let field;
8+
9+
beforeEach(() => {
10+
resetUniqueId();
11+
field = fixtureSync('<vaadin-custom-field></vaadin-custom-field>');
12+
});
13+
14+
describe('host', () => {
15+
it('default', async () => {
16+
await expect(field).dom.to.equalSnapshot();
17+
});
18+
19+
it('label', async () => {
20+
field.label = 'Label';
21+
await expect(field).dom.to.equalSnapshot();
22+
});
23+
24+
it('required', async () => {
25+
field.required = true;
26+
await expect(field).dom.to.equalSnapshot();
27+
});
28+
29+
it('helper', async () => {
30+
field.helperText = 'Helper';
31+
await expect(field).dom.to.equalSnapshot();
32+
});
33+
34+
it('error', async () => {
35+
field.errorMessage = 'Error';
36+
field.invalid = true;
37+
await aTimeout(0);
38+
await expect(field).dom.to.equalSnapshot();
39+
});
40+
});
41+
42+
describe('shadow', () => {
43+
it('default', async () => {
44+
await expect(field).shadowDom.to.equalSnapshot();
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)