Debian Codegraph @

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

raw
1 /* global posmodel */
2
3 import { simulateBarCode } from "@barcodes/../tests/legacy/helpers";
4
5 export function negate(selector, parent = "body") {
6 return `${parent}:not(:has(${selector}))`;
7 }
8 export function run(run, content = "run function", expectUnloadPage = false) {
9 return { content, trigger: "body", run, expectUnloadPage };
10 }
11 export function scan_barcode(barcode) {
12 return [
13 {
14 content: `PoS model scan barcode '${barcode}'`,
15 trigger: "body", // The element here does not really matter as long as it is present
16 run: () => {
17 simulateBarCode([...barcode, "Enter"]);
18 },
19 },
20 ];
21 }
22 export function negateStep(step) {
23 return {
24 ...step,
25 content: `Check that: ---${step.content}--- is not true`,
26 trigger: negate(step.trigger),
27 };
28 }
29 export function refresh() {
30 return run(
31 async () => {
32 await new Promise((resolve) => {
33 const checkTransaction = () => {
34 const activeTransactions = posmodel.data.indexedDB.activeTransactions;
35 if (activeTransactions.size === 0) {
36 window.location.reload();
37 resolve();
38 } else {
39 setTimeout(checkTransaction, 100);
40 }
41 };
42
43 setTimeout(() => {
44 checkTransaction();
45 }, 305);
46 setTimeout(() => {
47 throw new Error("Timeout waiting indexedDB for transactions to finish");
48 }, 2000);
49 });
50 },
51 "refresh page",
52 true
53 );
54 }
55 export function elementDoesNotExist(selector) {
56 return {
57 content: `Check that element "${selector}" don't exist.`,
58 trigger: negate(selector),
59 };
60 }
61
62 export function assertCurrentOrderDirty(dirty = true) {
63 return {
64 trigger: "body",
65 run() {
66 if (posmodel.getOrder().isDirty() !== dirty) {
67 throw new Error("Order should be " + (dirty ? "dirty" : "not dirty"));
68 }
69 },
70 };
71 }