From 39874b0e9cb4e2c754777fb0f06205eb9dae1b05 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 27 Nov 2023 14:12:25 +0100 Subject: [PATCH] [FIX] web: tests: make mock fetch almost sync (micro-tick) Before this commit, a studio qunit test ("edit/delete menus") sometimes failed on runbot, because it tried to click on a button in a modal footer, but it found 3 of them, because another dialog which was expected to be closed at that moment, was still opened. There was no guarantee for the dialog to be closed because of the way browser.fetch was mocked. The fetching part was ok, but the function returned a Response object, whose `json` method is async (setTimeout-like async, i.e. "real" ticks). This commit mocks some functions of the returned Response instance to ensure that, by calling nextTick after a click or whatever, the fetch operation has been done, the body has been decoded, potential renderings are finished and the DOM has been patched. Runbot error 23054 closes odoo/odoo#143980 X-original-commit: 66ef3ae730a233dc510425e0a5baca2973de493e Signed-off-by: Samuel Degueldre (sad) Signed-off-by: Aaron Bohy (aab) --- addons/web/static/tests/helpers/mock_services.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/addons/web/static/tests/helpers/mock_services.js b/addons/web/static/tests/helpers/mock_services.js index 904a9fd2798..1877a50d5ac 100644 --- a/addons/web/static/tests/helpers/mock_services.js +++ b/addons/web/static/tests/helpers/mock_services.js @@ -167,7 +167,15 @@ export function makeMockFetch(mockRPC) { status = 500; } const blob = new Blob([JSON.stringify(res || {})], { type: "application/json" }); - return new Response(blob, { status }); + const response = new Response(blob, { status }); + // Mock some functions of the Response API to make them almost synchronous (micro-tick level) + // as their native implementation is async (tick level), which can lead to undeterministic + // errors as it breaks the hypothesis that calling nextTick after fetching data is enough + // to see the result rendered in the DOM. + response.json = () => Promise.resolve(JSON.parse(JSON.stringify(res || {}))); + response.text = () => Promise.resolve(String(res || {})); + response.blob = () => Promise.resolve(blob); + return response; }; }