Debian Codegraph @

addons/point_of_sale/static/tests/generic_helpers/offline_util.js

raw
1 import { run } from "@point_of_sale/../tests/generic_helpers/utils";
2 import { ConnectionLostError } from "@web/core/network/rpc";
3
4 const originalFetch = window.fetch;
5 const originalSend = XMLHttpRequest.prototype.send;
6 const originalConsoleError = console.error;
7
8 export function setOfflineMode() {
9 return run(() => {
10 window.fetch = () => {
11 throw new ConnectionLostError();
12 };
13 XMLHttpRequest.prototype.send = () => {
14 throw new ConnectionLostError();
15 };
16 console.error = (...args) => {
17 const message = args[0] instanceof Error ? args[0].message : args[0];
18 if (typeof message === "string" && message.includes("ConnectionLostError")) {
19 console.info("Connection lost error handled in offline mode:", ...args);
20 } else {
21 originalConsoleError.apply(console, args);
22 }
23 };
24 }, "Offline mode is now enabled");
25 }
26
27 export function setOnlineMode() {
28 return run(() => {
29 window.fetch = originalFetch;
30 XMLHttpRequest.prototype.send = originalSend;
31 console.error = originalConsoleError;
32 }, "Offline mode is now disabled");
33 }