Skip to content

Commit 71eb0c5

Browse files
committed
test: add test for setting readonly props in ElementInternals
1 parent b6d2452 commit 71eb0c5

File tree

1 file changed

+38
-0
lines changed
  • packages/@lwc/integration-not-karma/test/component/LightningElement.attachInternals/elementInternals/formAssociated

1 file changed

+38
-0
lines changed

packages/@lwc/integration-not-karma/test/component/LightningElement.attachInternals/elementInternals/formAssociated/index.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import FormAssociatedNoAttachInternals from 'x/formAssociatedNoAttachInternals';
88
import FormAssociatedFalseNoAttachInternals from 'x/formAssociatedFalseNoAttachInternals';
99
import { ENABLE_ELEMENT_INTERNALS_AND_FACE } from '../../../../../helpers/constants.js';
1010

11+
const readOnlyProperties = [
12+
'shadowRoot',
13+
'states',
14+
'form',
15+
'willValidate',
16+
'validity',
17+
'validationMessage',
18+
'labels',
19+
];
20+
1121
const formAssociatedFalsyTest = (tagName, ctor) => {
1222
const form = document.createElement('form');
1323
document.body.appendChild(form);
@@ -116,6 +126,34 @@ describe.runIf(ENABLE_ELEMENT_INTERNALS_AND_FACE && typeof ElementInternals !==
116126
expect(internals.labels.length).toBe(1);
117127
expect(internals.labels[0]).toBe(label);
118128
});
129+
130+
it('should throw error when trying to set readonly properties on form associated component', () => {
131+
const elm = createElement('x-form-associated', { is: FormAssociated });
132+
document.body.appendChild(elm);
133+
const { internals } = elm;
134+
135+
const readOnlyProperties = [
136+
'shadowRoot',
137+
'states',
138+
'form',
139+
'willValidate',
140+
'validity',
141+
'validationMessage',
142+
'labels',
143+
];
144+
readOnlyProperties.forEach((property) => {
145+
expect(() => (internals[property] = 'test')).toThrow();
146+
});
147+
});
148+
149+
for (const prop of readOnlyProperties) {
150+
it(`should throw error when trying to set ${prop} on form associated component`, () => {
151+
const elm = createElement('x-form-associated', { is: FormAssociated });
152+
document.body.appendChild(elm);
153+
const { internals } = elm;
154+
expect(() => (internals[prop] = 'test')).toThrow();
155+
});
156+
}
119157
}
120158
);
121159

0 commit comments

Comments
 (0)