Debian Codegraph @

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

raw
1 import { uuidv4 } from "@point_of_sale/utils";
2 import { getService, makeDialogMockEnv, mountWithCleanup } from "@web/../tests/web_test_helpers";
3 import { animationFrame, tick, waitFor, waitUntil } from "@odoo/hoot-dom";
4 import { Deferred } from "@odoo/hoot-mock";
5 import { MainComponentsContainer } from "@web/core/main_components_container";
6 import { patch } from "@web/core/utils/patch";
7 import { onMounted } from "@odoo/owl";
8 import { expect } from "@odoo/hoot";
9
10 const { DateTime } = luxon;
11
12 export const setupPosEnv = async () => {
13 // Do not change these variables, they are in accordance with the demo data
14 odoo.pos_session_id = 1;
15 odoo.pos_config_id = 1;
16 odoo.from_backend = 0;
17 odoo.access_token = uuidv4(); // Avoid indexedDB conflicts
18 odoo.info = {
19 db: `pos-${uuidv4()}`, // Avoid indexedDB conflicts
20 isEnterprise: true,
21 };
22
23 await makeDialogMockEnv();
24 const store = getService("pos");
25 store.setCashier(store.user);
26 return store;
27 };
28
29 export const getFilledOrder = async (store, data = {}) => {
30 const order = store.addNewOrder(data);
31 const product1 = store.models["product.template"].get(5);
32 const product2 = store.models["product.template"].get(6);
33 const date = DateTime.now();
34 order.write_date = date;
35 order.create_date = date;
36
37 await store.addLineToOrder(
38 {
39 product_tmpl_id: product1,
40 qty: 3,
41 write_date: date,
42 create_date: date,
43 },
44 order
45 );
46 await store.addLineToOrder(
47 {
48 product_tmpl_id: product2,
49 qty: 2,
50 write_date: date,
51 create_date: date,
52 },
53 order
54 );
55 store.addPendingOrder([order.id]);
56 return order;
57 };
58
59 export async function waitUntilOrdersSynced(store, options) {
60 await waitUntil(() => !store.syncingOrders.size, options);
61 await tick();
62 }
63
64 export const mountPosDialog = async (component, props) => {
65 patchDialogComponent(component);
66 const dialog = getService("dialog");
67 const root = await mountWithCleanup(MainComponentsContainer);
68 const deferred = new Deferred();
69
70 const getComponentInstance = (root) => {
71 const flattenedChildren = (comp, acc = {}) => {
72 const array = Object.values(comp.children);
73 for (const child of array) {
74 acc[child.name] = child;
75 flattenedChildren(child, acc);
76 }
77 return acc;
78 };
79 const components = flattenedChildren(root);
80 return components[component.name];
81 };
82
83 dialog.add(component, {
84 ...props,
85 onMounted() {
86 const dialogComponent = getComponentInstance(root.__owl__);
87 deferred.resolve(dialogComponent.component);
88 },
89 });
90 return await deferred;
91 };
92
93 export const patchDialogComponent = (component) => {
94 component.props = [...component.props, "onMounted?"];
95 patch(component.prototype, {
96 setup() {
97 super.setup();
98
99 onMounted(() => {
100 this.props.onMounted && this.props.onMounted();
101 });
102 },
103 });
104 };
105
106 export const expectFormattedPrice = (value, expected) => {
107 expect(value).toBe(expected.replaceAll(" ", "\u00a0"));
108 };
109
110 export const dialogActions = async (action, steps = []) => {
111 // Launch the action in a promise to be able to await the end of the steps
112 await mountWithCleanup(MainComponentsContainer);
113 const promise = new Promise((resolve) => {
114 const call = async (fn) => {
115 const result = await fn();
116 resolve(result);
117 };
118 call(action);
119 });
120
121 // Wait for the dialog to be mounted
122 await waitFor(".o_dialog");
123
124 // Execute the steps one by one
125 for (const step of steps) {
126 await step();
127 await animationFrame();
128 }
129
130 // Return the result of the action
131 return await promise;
132 };