Skip to content

Commit 7026a7f

Browse files
Test to show how form values are handled
1 parent d77eb77 commit 7026a7f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/Components/Web.JS/test/DomSync.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,43 @@ describe('DomSync', () => {
257257
expect(newNodes[2]).toBe(origRoot2);
258258
expect(newNodes[2].childNodes[0]).toBe(anotherChildWillRetain);
259259
});
260+
261+
test('should update input element value when not modified by user', () => {
262+
// Arrange
263+
const destination = makeExistingContent(
264+
`<input value='original'>`);
265+
const newContent = makeNewContent(
266+
`<input value='changed'>`);
267+
const destinationNode = toNodeArray(destination)[0] as HTMLInputElement;
268+
269+
// Act
270+
synchronizeDomContent(destination, newContent);
271+
272+
// Assert
273+
expect(destinationNode.value).toEqual('changed');
274+
expect(destinationNode.getAttribute('value')).toEqual('changed');
275+
});
276+
277+
test('should not update input element value when modified by user', () => {
278+
// Arrange
279+
const destination = makeExistingContent(
280+
`<input value='original'>`);
281+
const newContent = makeNewContent(
282+
`<input value='changed'>`);
283+
const destinationNode = toNodeArray(destination)[0] as HTMLInputElement;
284+
destinationNode.value = 'edited by user';
285+
286+
// Act
287+
synchronizeDomContent(destination, newContent);
288+
289+
// Assert
290+
// While we do write a new attribute value, we do *not* overwrite the 'value'
291+
// property, so any user-performed edits will not be overwritten. This happens automatically
292+
// without any explicit implementation logic, but since it's the behavior we want, this test
293+
// is here to detect if we ever regress this behavior.
294+
expect(destinationNode.value).toEqual('edited by user');
295+
expect(destinationNode.getAttribute('value')).toEqual('changed');
296+
});
260297
});
261298

262299
function makeExistingContent(html: string): CommentBoundedRange {

0 commit comments

Comments
 (0)