-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
183 lines (157 loc) · 7.16 KB
/
index.spec.js
File metadata and controls
183 lines (157 loc) · 7.16 KB
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
import { createElement } from 'lwc';
import NoPropDeclared from 'x/noPropDeclared';
import PropDeclared from 'x/propDeclared';
import ApiPropDeclared from 'x/apiPropDeclared';
import TrackPropDeclared from 'x/trackPropDeclared';
import NoPropDeclaredNoSuper from 'x/noPropDeclaredNoSuper';
import PropDeclaredNoSuper from 'x/propDeclaredNoSuper';
import ApiPropDeclaredNoSuper from 'x/apiPropDeclaredNoSuper';
import TrackPropDeclaredNoSuper from 'x/trackPropDeclaredNoSuper';
import { ariaPropertiesMapping, extractDataIds } from '../../../helpers/utils.js';
describe('aria reflection', () => {
// Test with and without a custom superclass, since we may set the property accessor differently in each case
const variants = [
{
name: 'has custom superclass',
components: {
NoPropDeclared: {
tagName: 'x-no-prop-declared',
Ctor: NoPropDeclared,
},
PropDeclared: {
tagName: 'x-prop-declared',
Ctor: PropDeclared,
},
ApiPropDeclared: {
tagName: 'x-api-prop-declared',
Ctor: ApiPropDeclared,
},
TrackPropDeclared: {
tagName: 'x-track-prop-declared',
Ctor: TrackPropDeclared,
},
},
},
{
name: 'no custom superclass',
components: {
NoPropDeclared: {
tagName: 'x-no-prop-declared-no-super',
Ctor: NoPropDeclaredNoSuper,
},
PropDeclared: {
tagName: 'x-prop-declared-no-super',
Ctor: PropDeclaredNoSuper,
},
ApiPropDeclared: {
tagName: 'x-api-prop-declared-no-super',
Ctor: ApiPropDeclaredNoSuper,
},
TrackPropDeclared: {
tagName: 'x-track-prop-declared-no-super',
Ctor: TrackPropDeclaredNoSuper,
},
},
},
];
const scenarios = [
{
name: 'No prop declared',
componentKey: 'NoPropDeclared',
expectAttrReflection: true,
},
{
name: 'Prop declared',
componentKey: 'PropDeclared',
// declaring a prop in the component results in no attribute reflection
expectAttrReflection: false,
},
{
name: '@api prop declared',
componentKey: 'ApiPropDeclared',
// declaring a prop in the component results in no attribute reflection
expectAttrReflection: false,
},
{
name: '@track prop declared',
componentKey: 'TrackPropDeclared',
// declaring a prop in the component results in no attribute reflection
expectAttrReflection: false,
},
];
scenarios.forEach(({ name: scenarioName, componentKey, expectAttrReflection }) => {
describe(scenarioName, () => {
variants.forEach(({ name: variantName, components }) => {
describe(variantName, () => {
const { tagName, Ctor } = components[componentKey];
Object.entries(ariaPropertiesMapping).forEach(([propName, attrName]) => {
function validateAria(elm, expected) {
const dataIds = extractDataIds(elm);
// rendering the prop works
expect(dataIds[propName].textContent).toBe(
expected === null ? '' : expected
);
// the property is correct
expect(elm[propName]).toBe(expected);
expect(elm.getPropInternal(propName)).toBe(expected);
// the attr is reflected (if we expect that to work)
expect(elm.getAttribute(attrName)).toBe(
expectAttrReflection ? expected : null
);
expect(elm.getAttrInternal(attrName)).toBe(
expectAttrReflection ? expected : null
);
}
describe(propName, () => {
it('no initial value', async () => {
const elm = createElement(tagName, { is: Ctor });
document.body.appendChild(elm);
await Promise.resolve();
validateAria(elm, null);
expect(elm.renderCount).toBe(1);
});
it('set externally', async () => {
const elm = createElement(tagName, { is: Ctor });
// set initial prop before rendering
elm[propName] = 'foo';
document.body.appendChild(elm);
await Promise.resolve();
validateAria(elm, 'foo');
expect(elm.renderCount).toBe(1);
// mutate prop
elm[propName] = 'bar';
await Promise.resolve();
validateAria(elm, 'bar');
expect(elm.renderCount).toBe(2);
// remove
elm[propName] = null;
await Promise.resolve();
validateAria(elm, null);
expect(elm.renderCount).toBe(3);
});
it('set internally', async () => {
const elm = createElement(tagName, { is: Ctor });
// set initial prop before rendering
elm.setPropInternal(propName, 'foo');
document.body.appendChild(elm);
await Promise.resolve();
validateAria(elm, 'foo');
expect(elm.renderCount).toBe(1);
// mutate prop
elm.setPropInternal(propName, 'bar');
await Promise.resolve();
validateAria(elm, 'bar');
expect(elm.renderCount).toBe(2);
// remove
elm.setPropInternal(propName, null);
await Promise.resolve();
validateAria(elm, null);
expect(elm.renderCount).toBe(3);
});
});
});
});
});
});
});
});