forked from salesforce/lwc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.spec.js
More file actions
185 lines (163 loc) · 8.15 KB
/
index.spec.js
File metadata and controls
185 lines (163 loc) · 8.15 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
184
185
import { createElement } from 'lwc';
import NotFormAssociated from 'x/notFormAssociated';
import FormAssociated from 'x/formAssociated';
import FormAssociatedFalse from 'x/formAssociatedFalse';
import NotFormAssociatedNoAttachInternals from 'x/notFormAssociatedNoAttachInternals';
import FormAssociatedNoAttachInternals from 'x/formAssociatedNoAttachInternals';
import FormAssociatedFalseNoAttachInternals from 'x/formAssociatedFalseNoAttachInternals';
import { ENABLE_ELEMENT_INTERNALS_AND_FACE } from '../../../../../helpers/constants.js';
describe.runIf(ENABLE_ELEMENT_INTERNALS_AND_FACE && typeof ElementInternals !== 'undefined')(
'should throw an error when duplicate tag name used',
() => {
it('with different formAssociated value', () => {
// Register tag with formAssociated = true
createElement('x-form-associated', { is: FormAssociated });
// Try to register again with formAssociated = false
expect(() =>
createElement('x-form-associated', { is: FormAssociatedFalse })
).toThrowError(
/<x-form-associated> was already registered with formAssociated=true. It cannot be re-registered with formAssociated=false. Please rename your component to have a different name than <x-form-associated>/
);
});
it('should not throw when duplicate tag name used with the same formAssociated value', () => {
// formAssociated = true
createElement('x-form-associated', { is: FormAssociated });
expect(() => createElement('x-form-associated', { is: FormAssociated })).not.toThrow();
// formAssociated = false
createElement('x-form-associated-false', { is: FormAssociatedFalse });
expect(() =>
createElement('x-form-associated-false', { is: FormAssociatedFalse })
).not.toThrow();
// formAssociated = undefined
createElement('x-not-form-associated', { is: NotFormAssociated });
expect(() =>
createElement('x-not-form-associated', { is: NotFormAssociated })
).not.toThrow();
});
it('should throw an error when accessing form related properties on a non-form associated component', () => {
const form = document.createElement('form');
document.body.appendChild(form);
const testElements = {
'x-form-associated-false': FormAssociatedFalse,
'x-not-form-associated': NotFormAssociated,
};
let elm;
Object.entries(testElements).forEach(([tagName, ctor]) => {
elm = createElement(`x-${tagName}`, { is: ctor });
const { internals } = elm;
form.appendChild(elm);
expect(() => internals.form).toThrow();
expect(() => internals.setFormValue('2019-03-15')).toThrow();
expect(() => internals.willValidate).toThrow();
expect(() => internals.validity).toThrow();
expect(() => internals.checkValidity()).toThrow();
expect(() => internals.reportValidity()).toThrow();
expect(() => internals.setValidity('')).toThrow();
expect(() => internals.validationMessage).toThrow();
expect(() => internals.labels).toThrow();
});
document.body.removeChild(form);
});
it('should be able to use internals to validate form associated component', () => {
const elm = createElement('x-form-associated', { is: FormAssociated });
const { internals } = elm;
expect(internals.willValidate).toBe(true);
expect(internals.validity.valid).toBe(true);
expect(internals.checkValidity()).toBe(true);
expect(internals.reportValidity()).toBe(true);
expect(internals.validationMessage).toBe('');
internals.setValidity({ rangeUnderflow: true }, 'pick future date');
expect(internals.validity.valid).toBe(false);
expect(internals.checkValidity()).toBe(false);
expect(internals.reportValidity()).toBe(false);
expect(internals.validationMessage).toBe('pick future date');
});
it('should be able to use setFormValue on a form associated component', () => {
const form = document.createElement('form');
document.body.appendChild(form);
const elm = createElement('x-form-associated', { is: FormAssociated });
const { internals } = elm;
form.appendChild(elm);
expect(internals.form).toBe(form);
elm.setAttribute('name', 'date');
const inputElm = elm.shadowRoot
.querySelector('x-input')
.shadowRoot.querySelector('input');
internals.setFormValue('2019-03-15', '3/15/2019', inputElm);
const formData = new FormData(form);
expect(formData.get('date')).toBe('2019-03-15');
});
it('should be able to associate labels to a form associated component', () => {
const elm = createElement('x-form-associated', { is: FormAssociated });
document.body.appendChild(elm);
const { internals } = elm;
expect(internals.labels.length).toBe(0);
elm.id = 'test-id';
const label = document.createElement('label');
label.htmlFor = elm.id;
document.body.appendChild(label);
expect(internals.labels.length).toBe(1);
expect(internals.labels[0]).toBe(label);
});
}
);
it.runIf(typeof ElementInternals === 'undefined')(
'disallows form association on older API versions',
() => {
const isFormAssociated = (elm) => {
const form = document.createElement('form');
document.body.appendChild(form);
form.appendChild(elm);
const result = elm.formAssociatedCallbackCalled;
document.body.removeChild(form); // cleanup
return result;
};
let elm;
// formAssociated = true
const createFormAssociatedTrue = () => {
elm = createElement('x-form-associated-no-attach-internals', {
is: FormAssociatedNoAttachInternals,
});
};
if (ENABLE_ELEMENT_INTERNALS_AND_FACE) {
createFormAssociatedTrue();
expect(isFormAssociated(elm)).toBe(true);
} else {
expect(createFormAssociatedTrue).toLogWarningDev(
/Component <x-form-associated-no-attach-internals> set static formAssociated to true, but form association is not enabled/
);
expect(isFormAssociated(elm)).toBe(false);
}
// formAssociated = false
elm = createElement('x-form-associated-false-no-attach-internals', {
is: FormAssociatedFalseNoAttachInternals,
});
expect(isFormAssociated(elm)).toBe(false);
// formAssociated = undefined
elm = createElement('x-not-form-associated-no-attach-internals', {
is: NotFormAssociatedNoAttachInternals,
});
expect(isFormAssociated(elm)).toBe(false);
}
);
it.skipIf(ENABLE_ELEMENT_INTERNALS_AND_FACE)(
'warns for attachInternals on older API versions',
() => {
// formAssociated = true
expect(() => {
expect(() => createElement('x-form-associated', { is: FormAssociated })).toThrowError(
/The attachInternals API is only supported in API version 61 and above/
);
}).toLogWarningDev(
/Component <x-form-associated> set static formAssociated to true, but form association is not enabled/
);
// formAssociated = false
expect(() =>
createElement('x-form-associated-false', { is: FormAssociatedFalse })
).toThrowError(/The attachInternals API is only supported in API version 61 and above/);
// formAssociated = undefined
expect(() =>
createElement('x-not-form-associated', { is: NotFormAssociated })
).toThrowError(/The attachInternals API is only supported in API version 61 and above/);
}
);