[FIX] spreadsheet: call the correct callback when fetching data

Before this revision, the callback passed to the constructor of
BatchEndpoint which should be called when there is an error when
fetching data was not called.

Part of task-id 2928601

X-original-commit: 6b4d5e4274c7644f9fda40c96f6b08a6bfd9ff37
Part-of: odoo/odoo#108527
This commit is contained in:
Pierre Rousseau
2022-12-22 14:49:29 +01:00
parent ca17c97920
commit 0fa9d984f3
2 changed files with 31 additions and 3 deletions
@@ -10,7 +10,7 @@ function removeDuplicates(array) {
return [...new Set(array.map((el) => JSON.stringify(el)))].map((el) => JSON.parse(el));
}
class Request {
export class Request {
/**
* @param {string} resModel
* @param {string} method
@@ -251,7 +251,11 @@ export default class BatchEndpoint {
*/
_notifyResults(batchResult) {
for (const [request, result] of batchResult) {
this.successCallback(request, result);
if (result instanceof Error) {
this.failureCallback(request, result);
} else {
this.successCallback(request, result);
}
}
}
@@ -1,8 +1,8 @@
/** @odoo-module */
import { nextTick } from "@web/../tests/helpers/utils";
import { ServerData } from "@spreadsheet/data_sources/server_data";
import { LoadingDataError } from "@spreadsheet/o_spreadsheet/errors";
import BatchEndpoint, { Request, ServerData } from "@spreadsheet/data_sources/server_data";
QUnit.module("spreadsheet server data", {}, () => {
QUnit.test("simple synchronous get", async (assert) => {
@@ -279,4 +279,28 @@ QUnit.module("spreadsheet server data", {}, () => {
assert.deepEqual(serverData.batch.get("partner", "get_something", 5), 5);
assert.verifySteps([]);
});
QUnit.test("Call the correct callback after a batch result", async (assert) => {
const orm = {
call: async (model, method, args) => {
if (args[0].includes(5)) {
throw new Error("error while fetching data");
}
return args[0];
},
};
const batchEndpoint = new BatchEndpoint(orm, "partner", "get_something", {
whenDataIsFetched: () => {},
successCallback: () => assert.step("success-callback"),
failureCallback: () => assert.step("failure-callback"),
});
const request = new Request("partner", "get_something", [4]);
const request2 = new Request("partner", "get_something", [5]);
batchEndpoint.call(request);
batchEndpoint.call(request2);
assert.verifySteps([]);
await nextTick();
console.log("Passe");
assert.verifySteps(["success-callback", "failure-callback"]);
});
});