Skip to content

Commit 536b74e

Browse files
committed
Adding tests for the "batch" action
1 parent 42ab648 commit 536b74e

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/LiveComponent/assets/src/live_controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export default class extends Controller implements LiveController {
457457
url += `/${encodeURIComponent(actions[0].name)}`;
458458
} else {
459459
// TODO: support on the server
460-
url += '/batch';
460+
url += '/_batch';
461461
requestData.actions = actions;
462462
}
463463
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,32 @@ describe('LiveController Action Tests', () => {
175175
// but soon the re-render does happen
176176
await waitFor(() => expect(test.element).toHaveTextContent('donut holes'));
177177
});
178+
179+
it('batches multiple actions together', async () => {
180+
const test = await createTest({ isSaved: false }, (data: any) => `
181+
<div ${initComponent(data)}>
182+
${data.isSaved ? 'Component Saved!' : ''}
183+
<button data-action="live#action" data-action-name="debounce(10)|save">Save</button>
184+
<button data-action="live#action" data-action-name="debounce(10)|sync(syncAll=1)">Sync</button>
185+
</div>
186+
`);
187+
188+
// 1 request with all 3 actions
189+
test.expectsAjaxCall('post')
190+
.expectSentData(test.initialData)
191+
// 3 actions called
192+
.expectActionCalled('save')
193+
.expectActionCalled('sync', { syncAll: '1' })
194+
.expectActionCalled('save')
195+
.serverWillChangeData((data: any) => {
196+
data.isSaved = true;
197+
})
198+
.init();
199+
200+
getByText(test.element, 'Save').click();
201+
getByText(test.element, 'Sync').click();
202+
getByText(test.element, 'Save').click();
203+
204+
await waitFor(() => expect(test.element).toHaveTextContent('Component Saved!'));
205+
});
178206
});

src/LiveComponent/assets/test/tools.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ class MockedAjaxCall {
113113
method: string;
114114
test: FunctionalTest;
115115
expectedSentData?: any;
116-
expectedActionName?: string;
117-
expectedActionArgs: any = {};
116+
expectedActions: Array<{ name: string, args: any }> = [];
118117
expectedHeaders: any = {};
119118
changeDataCallback?: (data: any) => void;
120119
template?: (data: any) => string
@@ -157,8 +156,10 @@ class MockedAjaxCall {
157156

158157
expectActionCalled(actionName: string, args: any = {}): MockedAjaxCall {
159158
this.checkInitialization('expectActionName');
160-
this.expectedActionName = actionName;
161-
this.expectedActionArgs = args;
159+
this.expectedActions.push({
160+
name: actionName,
161+
args: args
162+
})
162163

163164
return this;
164165
}
@@ -216,8 +217,8 @@ class MockedAjaxCall {
216217
} else {
217218
requestInfo.push(` DATA: ${JSON.stringify(this.getRequestBody())}`);
218219
}
219-
if (this.expectedActionName) {
220-
requestInfo.push(` Expected URL to contain action /${this.expectedActionName}`)
220+
if (this.expectedActions.length === 1) {
221+
requestInfo.push(` Expected URL to contain action /${this.expectedActions[0]}`)
221222
}
222223

223224
return requestInfo.join("\n");
@@ -242,6 +243,11 @@ class MockedAjaxCall {
242243
matcherObject.url = `end:?${params.toString()}`;
243244
} else {
244245
matcherObject.body = this.getRequestBody();
246+
if (this.expectedActions.length === 1) {
247+
matcherObject.url = `end:/${this.expectedActions[0].name}`;
248+
} else if (this.expectedActions.length > 1) {
249+
matcherObject.url = `end:/_batch`;
250+
}
245251
}
246252

247253
this.routeName = `route-${this.test.mockedAjaxCalls.length}`;
@@ -258,8 +264,11 @@ class MockedAjaxCall {
258264
const body: any = {
259265
data: this.expectedSentData
260266
};
261-
if (this.expectedActionName) {
262-
body.args = this.expectedActionArgs;
267+
268+
if (this.expectedActions.length === 1) {
269+
body.args = this.expectedActions[0].args;
270+
} else if (this.expectedActions.length > 1) {
271+
body.actions = this.expectedActions;
263272
}
264273

265274
return body;

0 commit comments

Comments
 (0)