Skip to content

Commit cce0791

Browse files
committed
removing old test + changing position of check
If this happens only after the request, then the first field that is modified, which then triggers a request will think that it is out-of-sync.
1 parent b7f6874 commit cce0791

File tree

2 files changed

+9
-56
lines changed

2 files changed

+9
-56
lines changed

src/LiveComponent/assets/src/live_controller.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,15 @@ export default class extends Controller implements LiveController {
410410
// we're making a request NOW, so no need to make another one after debouncing
411411
this.#clearRequestDebounceTimeout();
412412

413+
// check if any unsynced inputs are now "in sync": their value matches what's in the store
414+
// if they ARE, then they are on longer "unsynced", which means that any
415+
// potential new values from the server *should* now be respected and used
416+
this.unsyncedInputs.allMappedFields().forEach((element, modelName) => {
417+
if (getValueFromInput(element, this.valueStore) === this.valueStore.get(modelName)) {
418+
this.unsyncedInputs.remove(modelName);
419+
}
420+
});
421+
413422
const fetchOptions: RequestInit = {};
414423
fetchOptions.headers = {
415424
'Accept': 'application/vnd.live-component+html',
@@ -521,13 +530,6 @@ export default class extends Controller implements LiveController {
521530
Object.keys(modifiedModelValues).forEach((modelName) => {
522531
this.valueStore.set(modelName, modifiedModelValues[modelName]);
523532
});
524-
525-
// check if any unsynced inputs are now "in sync": their value matches what's in the store
526-
this.unsyncedInputs.allMappedFields().forEach((element, modelName) => {
527-
if (getValueFromInput(element, this.valueStore) === this.valueStore.get(modelName)) {
528-
this.unsyncedInputs.remove(modelName);
529-
}
530-
});
531533
}
532534

533535
_onLoadingStart() {

src/LiveComponent/assets/test/controller/model.test.ts

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -127,55 +127,6 @@ describe('LiveController data-model Tests', () => {
127127
expect(test.controller.dataValue).toEqual({name: 'Jan'});
128128
});
129129

130-
131-
it('only uses the most recent render call result', async () => {
132-
const test = await createTest({ name: 'Ryan' }, (data: any) => `
133-
<div ${initComponent(data)}>
134-
<input
135-
data-model="name"
136-
value="${data.name}"
137-
>
138-
139-
Name is: ${data.name}
140-
</div>
141-
`);
142-
143-
let renderCount = 0;
144-
test.element.addEventListener('live:render', () => {
145-
renderCount++;
146-
})
147-
148-
const requests: Array<{letters: string, delay: number}> = [
149-
{ letters: 'g', delay: 650 },
150-
{ letters: 'gu', delay: 250 },
151-
{ letters: 'guy', delay: 150 },
152-
];
153-
requests.forEach((request) => {
154-
test.expectsAjaxCall('get')
155-
.expectSentData({ name: `Ryan${request.letters}` })
156-
.delayResponse(request.delay)
157-
.init();
158-
});
159-
160-
await userEvent.type(test.queryByDataModel('name'), 'guy', {
161-
// This will result in this sequence:
162-
// A) "g" starts 200ms
163-
// B) "gu" starts 400ms
164-
// C) "guy" starts 600ms
165-
// D) "gu" finishes 650ms (is ignored)
166-
// E) "guy" finishes 750ms (is used)
167-
// F) "g" finishes 850ms (is ignored)
168-
delay: 200
169-
});
170-
171-
await waitFor(() => expect(test.element).toHaveTextContent('Name is: Ryanguy'));
172-
expect(test.queryByDataModel('name')).toHaveValue('Ryanguy');
173-
expect(test.controller.dataValue).toEqual({name: 'Ryanguy'});
174-
175-
// only 1 render should have ultimately occurred
176-
expect(renderCount).toEqual(1);
177-
});
178-
179130
it('falls back to using the name attribute when no data-model is present and <form data-model> is ancestor', async () => {
180131
const test = await createTest({ color: '' }, (data: any) => `
181132
<div ${initComponent(data)}>

0 commit comments

Comments
 (0)