| 1 |
|
| 2 |
|
| 3 |
import { effectService } from "@web/core/effects/effect_service"; |
| 4 |
import { localization } from "@web/core/l10n/localization"; |
| 5 |
import { ConnectionAbortedError, rpcBus, rpc } from "@web/core/network/rpc"; |
| 6 |
import { ormService } from "@web/core/orm_service"; |
| 7 |
import { overlayService } from "@web/core/overlay/overlay_service"; |
| 8 |
import { uiService } from "@web/core/ui/ui_service"; |
| 9 |
import { user } from "@web/core/user"; |
| 10 |
import { patchWithCleanup } from "./utils"; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
export const defaultLocalization = { |
| 17 |
dateFormat: "MM/dd/yyyy", |
| 18 |
timeFormat: "HH:mm:ss", |
| 19 |
dateTimeFormat: "MM/dd/yyyy HH:mm:ss", |
| 20 |
decimalPoint: ".", |
| 21 |
direction: "ltr", |
| 22 |
grouping: [], |
| 23 |
multiLang: false, |
| 24 |
thousandsSep: ",", |
| 25 |
weekStart: 7, |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
export function makeFakeLocalizationService(config = {}) { |
| 32 |
patchWithCleanup(localization, { ...defaultLocalization, ...config }); |
| 33 |
patchWithCleanup(luxon.Settings, { defaultNumberingSystem: "latn" }); |
| 34 |
|
| 35 |
return { |
| 36 |
name: "localization", |
| 37 |
start: async (env) => { |
| 38 |
return localization; |
| 39 |
}, |
| 40 |
}; |
| 41 |
} |
| 42 |
|
| 43 |
export function patchRPCWithCleanup(mockRPC = () => {}) { |
| 44 |
let nextId = 1; |
| 45 |
patchWithCleanup(rpc, { |
| 46 |
_rpc: function (route, params = {}, settings = {}) { |
| 47 |
let rejectFn; |
| 48 |
const data = { |
| 49 |
id: nextId++, |
| 50 |
jsonrpc: "2.0", |
| 51 |
method: "call", |
| 52 |
params: params, |
| 53 |
}; |
| 54 |
rpcBus.trigger("RPC:REQUEST", { data, url: route, settings }); |
| 55 |
const rpcProm = new Promise((resolve, reject) => { |
| 56 |
rejectFn = reject; |
| 57 |
Promise.resolve(mockRPC(...arguments)) |
| 58 |
.then((result) => { |
| 59 |
rpcBus.trigger("RPC:RESPONSE", { data, settings, result }); |
| 60 |
resolve(result); |
| 61 |
}) |
| 62 |
.catch((error) => { |
| 63 |
rpcBus.trigger("RPC:RESPONSE", { |
| 64 |
data, |
| 65 |
settings, |
| 66 |
error, |
| 67 |
}); |
| 68 |
reject(error); |
| 69 |
}); |
| 70 |
}); |
| 71 |
rpcProm.abort = (rejectError = true) => { |
| 72 |
if (rejectError) { |
| 73 |
rejectFn(new ConnectionAbortedError("XmlHttpRequestError abort")); |
| 74 |
} |
| 75 |
}; |
| 76 |
return rpcProm; |
| 77 |
}, |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
export function makeMockXHR(response, sendCb, def) { |
| 82 |
const MockXHR = function () { |
| 83 |
return { |
| 84 |
_loadListener: null, |
| 85 |
url: "", |
| 86 |
addEventListener(type, listener) { |
| 87 |
if (type === "load") { |
| 88 |
this._loadListener = listener; |
| 89 |
} else if (type === "error") { |
| 90 |
this._errorListener = listener; |
| 91 |
} |
| 92 |
}, |
| 93 |
set onload(listener) { |
| 94 |
this._loadListener = listener; |
| 95 |
}, |
| 96 |
set onerror(listener) { |
| 97 |
this._errorListener = listener; |
| 98 |
}, |
| 99 |
open(method, url) { |
| 100 |
this.url = url; |
| 101 |
}, |
| 102 |
getResponseHeader() {}, |
| 103 |
setRequestHeader() {}, |
| 104 |
async send(data) { |
| 105 |
let listener = this._loadListener; |
| 106 |
if (sendCb) { |
| 107 |
if (typeof data === "string") { |
| 108 |
try { |
| 109 |
data = JSON.parse(data); |
| 110 |
} catch { |
| 111 |
|
| 112 |
} |
| 113 |
} |
| 114 |
try { |
| 115 |
await sendCb.call(this, data); |
| 116 |
} catch { |
| 117 |
listener = this._errorListener; |
| 118 |
} |
| 119 |
} |
| 120 |
if (def) { |
| 121 |
await def; |
| 122 |
} |
| 123 |
listener.call(this); |
| 124 |
}, |
| 125 |
response: JSON.stringify(response || ""), |
| 126 |
}; |
| 127 |
}; |
| 128 |
return MockXHR; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
export function makeMockFetch(mockRPC) { |
| 136 |
return async (input, params) => { |
| 137 |
let route = typeof input === "string" ? input : input.url; |
| 138 |
if (route.includes("load_menus")) { |
| 139 |
route = route.split("?")[0]; |
| 140 |
} |
| 141 |
let res; |
| 142 |
let status; |
| 143 |
try { |
| 144 |
res = await mockRPC(route, params); |
| 145 |
status = 200; |
| 146 |
} catch { |
| 147 |
status = 500; |
| 148 |
} |
| 149 |
const blob = new Blob([JSON.stringify(res || {})], { type: "application/json" }); |
| 150 |
const response = new Response(blob, { status }); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
response.json = () => Promise.resolve(JSON.parse(JSON.stringify(res || {}))); |
| 156 |
response.text = () => Promise.resolve(String(res || {})); |
| 157 |
response.blob = () => Promise.resolve(blob); |
| 158 |
return response; |
| 159 |
}; |
| 160 |
} |
| 161 |
|
| 162 |
export const fakeCommandService = { |
| 163 |
start() { |
| 164 |
return { |
| 165 |
add() { |
| 166 |
return () => {}; |
| 167 |
}, |
| 168 |
getCommands() { |
| 169 |
return []; |
| 170 |
}, |
| 171 |
openPalette() {}, |
| 172 |
}; |
| 173 |
}, |
| 174 |
}; |
| 175 |
|
| 176 |
export const fakeTitleService = { |
| 177 |
start() { |
| 178 |
let current = {}; |
| 179 |
return { |
| 180 |
get current() { |
| 181 |
return JSON.stringify(current); |
| 182 |
}, |
| 183 |
getParts() { |
| 184 |
return current; |
| 185 |
}, |
| 186 |
setParts(parts) { |
| 187 |
current = Object.assign({}, current, parts); |
| 188 |
}, |
| 189 |
}; |
| 190 |
}, |
| 191 |
}; |
| 192 |
|
| 193 |
export function makeFakeNotificationService(mock) { |
| 194 |
return { |
| 195 |
start() { |
| 196 |
function add() { |
| 197 |
if (mock) { |
| 198 |
return mock(...arguments); |
| 199 |
} |
| 200 |
} |
| 201 |
return { |
| 202 |
add, |
| 203 |
}; |
| 204 |
}, |
| 205 |
}; |
| 206 |
} |
| 207 |
|
| 208 |
export function makeFakeDialogService(addDialog, closeAllDialog) { |
| 209 |
return { |
| 210 |
start() { |
| 211 |
return { |
| 212 |
add: addDialog || (() => () => {}), |
| 213 |
closeAll: closeAllDialog || (() => () => {}), |
| 214 |
}; |
| 215 |
}, |
| 216 |
}; |
| 217 |
} |
| 218 |
|
| 219 |
export function makeFakePwaService() { |
| 220 |
return { |
| 221 |
start() { |
| 222 |
return { |
| 223 |
canPromptToInstall: false, |
| 224 |
isAvailable: false, |
| 225 |
isScopedApp: false |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
export function patchUserContextWithCleanup(patch) { |
| 232 |
const context = user.context; |
| 233 |
patchWithCleanup(user, { |
| 234 |
get context() { |
| 235 |
return Object.assign({}, context, patch); |
| 236 |
}, |
| 237 |
}); |
| 238 |
} |
| 239 |
|
| 240 |
export function patchUserWithCleanup(patch) { |
| 241 |
patchWithCleanup(user, patch); |
| 242 |
} |
| 243 |
|
| 244 |
export function makeFakeBarcodeService() { |
| 245 |
return { |
| 246 |
start() { |
| 247 |
return { |
| 248 |
bus: { |
| 249 |
async addEventListener() {}, |
| 250 |
async removeEventListener() {}, |
| 251 |
}, |
| 252 |
}; |
| 253 |
}, |
| 254 |
}; |
| 255 |
} |
| 256 |
|
| 257 |
export function makeFakeHTTPService(getResponse, postResponse) { |
| 258 |
getResponse = |
| 259 |
getResponse || |
| 260 |
((route, readMethod) => { |
| 261 |
return readMethod === "json" ? {} : ""; |
| 262 |
}); |
| 263 |
postResponse = |
| 264 |
postResponse || |
| 265 |
((route, params, readMethod) => { |
| 266 |
return readMethod === "json" ? {} : ""; |
| 267 |
}); |
| 268 |
return { |
| 269 |
start() { |
| 270 |
return { |
| 271 |
async get(...args) { |
| 272 |
return getResponse(...args); |
| 273 |
}, |
| 274 |
async post(...args) { |
| 275 |
return postResponse(...args); |
| 276 |
}, |
| 277 |
}; |
| 278 |
}, |
| 279 |
}; |
| 280 |
} |
| 281 |
|
| 282 |
function makeFakeActionService() { |
| 283 |
return { |
| 284 |
start() { |
| 285 |
return { |
| 286 |
doAction() {}, |
| 287 |
}; |
| 288 |
}, |
| 289 |
}; |
| 290 |
} |
| 291 |
|
| 292 |
export const mocks = { |
| 293 |
command: () => fakeCommandService, |
| 294 |
effect: () => effectService, |
| 295 |
localization: makeFakeLocalizationService, |
| 296 |
notification: makeFakeNotificationService, |
| 297 |
title: () => fakeTitleService, |
| 298 |
ui: () => uiService, |
| 299 |
dialog: makeFakeDialogService, |
| 300 |
orm: () => ormService, |
| 301 |
action: makeFakeActionService, |
| 302 |
overlay: () => overlayService, |
| 303 |
}; |