Debian Codegraph @

addons/point_of_sale/static/tests/pos/tours/utils/chrome_util.js

raw
1 /* global posmodel */
2 import * as Dialog from "@point_of_sale/../tests/generic_helpers/dialog_util";
3 import { negate } from "@point_of_sale/../tests/generic_helpers/utils";
4 import * as Numpad from "@point_of_sale/../tests/generic_helpers/numpad_util";
5 const { DateTime } = luxon;
6
7 export function confirmPopup() {
8 return [Dialog.confirm()];
9 }
10 export function clickMenuButton() {
11 return {
12 content: "Click on the menu button",
13 trigger: ".pos-rightheader button:has(.fa-bars)",
14 run: "click",
15 };
16 }
17 export function clickMenuOption(name, options) {
18 return [
19 waitForMenuButtons(),
20 clickMenuButton(),
21 waitForMenuOptionsToOpen(),
22 clickMenuDropdownOption(name, options),
23 ];
24 }
25 export function waitForMenuButtons() {
26 return {
27 content: "Wait for the menu buttons to be available",
28 trigger: ".pos-rightheader button:has(.fa-bars)",
29 };
30 }
31 export function waitForMenuOptionsToOpen() {
32 return {
33 content: `Wait for the menu options to be available`,
34 trigger: `span.dropdown-item`,
35 };
36 }
37 export function clickMenuDropdownOption(name, { expectUnloadPage = false } = {}) {
38 return {
39 content: `click on something in the burger menu`,
40 trigger: `span.dropdown-item:contains(${name})`,
41 run: "click",
42 expectUnloadPage,
43 };
44 }
45 export function existMenuOption(name) {
46 return [
47 clickMenuButton(),
48 {
49 content: `check that ${name} exists in the burger menu`,
50 trigger: `span.dropdown-item:contains(${name})`,
51 },
52 clickMenuButton(),
53 ];
54 }
55 export function notExistMenuOption(name) {
56 return [
57 clickMenuButton(),
58 {
59 content: `check that ${name} doesn't exist in the burger menu`,
60 trigger: negate(`span.dropdown-item:contains(${name})`),
61 },
62 ];
63 }
64 export function isCashMoveButtonHidden() {
65 return [
66 {
67 trigger: ".pos-topheader:not(:contains(Cash In/Out))",
68 },
69 ];
70 }
71 export function doCashMove(amount, reason) {
72 const numpadWrite = (val) => val.split("").flatMap((key) => Numpad.click(key));
73 return [
74 ...clickMenuOption("Cash In/Out"),
75 fillTextArea(".cash-reason", reason),
76 {
77 isActive: ["desktop"],
78 content: "Enter the amount to cash in/out",
79 trigger: ".modal input.o_input",
80 run: "edit " + amount,
81 },
82 {
83 isActive: ["mobile"],
84 content: "Enter the amount to cash in/out",
85 trigger: ".modal input.o_input",
86 run: "click",
87 },
88 ...numpadWrite(amount).map((step) => ({
89 isActive: ["mobile"],
90 ...step,
91 })),
92 {
93 isActive: ["mobile"],
94 trigger: ".o-overlay-item:nth-child(2) .modal-footer button:contains('Confirm')",
95 run: "click",
96 },
97 Dialog.confirm(),
98 ];
99 }
100 export function endTour() {
101 return {
102 content: "Last tour step that avoids error mentioned in commit 443c209",
103 trigger: "body",
104 };
105 }
106 export function isSyncStatusConnected() {
107 return {
108 trigger: negate(".oe_status", ".pos-rightheader .status-buttons"),
109 };
110 }
111 export function clickPlanButton() {
112 return [
113 {
114 content: "go back to the floor screen",
115 trigger: ".pos-leftheader .table-button",
116 run: "click",
117 },
118 ...waitRequest(),
119 ];
120 }
121 export function startPoS() {
122 return [
123 {
124 content: "Start PoS",
125 trigger: ".screen-login .btn.open-register-btn",
126 run: "click",
127 },
128 ];
129 }
130 export function clickBtn(name, { expectUnloadPage = false } = {}) {
131 return {
132 content: `Click on ${name}`,
133 trigger: `body button:contains(${name})`,
134 run: "click",
135 expectUnloadPage,
136 };
137 }
138 export function hasBtn(name) {
139 return {
140 content: `Check button ${name} exist`,
141 trigger: `body button:contains(${name})`,
142 };
143 }
144 export function fillTextArea(target, value) {
145 return {
146 content: `Fill text area with ${value}`,
147 trigger: `textarea${target}`,
148 run: `edit ${value}`,
149 };
150 }
151 export function createFloatingOrder() {
152 return { trigger: ".pos-leftheader .list-plus-btn", run: "click" };
153 }
154
155 function _hasFloatingOrder(name, yes) {
156 const negateIfNecessary = (trigger) => (yes ? trigger : negate(trigger));
157 return [
158 {
159 isActive: ["desktop"],
160 trigger: negateIfNecessary(
161 `.pos-topheader .floating-order-container:contains('${name}')`
162 ),
163 },
164 {
165 isActive: ["mobile"],
166 trigger: ".pos-leftheader button.fa-caret-down",
167 run: "click",
168 },
169 {
170 isActive: ["mobile"],
171 trigger: negateIfNecessary(
172 `.modal-header:contains(Choose an order) ~ .modal-body .floating-order-container:contains('${name}')`
173 ),
174 },
175 {
176 isActive: ["mobile"],
177 trigger: ".oi-arrow-left",
178 run: "click",
179 },
180 ];
181 }
182
183 export function hasFloatingOrder(name) {
184 return _hasFloatingOrder(name, true);
185 }
186
187 export function noFloatingOrder(name) {
188 return _hasFloatingOrder(name, false);
189 }
190 export function clickOrders() {
191 return { trigger: ".pos-leftheader .orders-button", run: "click" };
192 }
193 export function clickPresetTimingSlot() {
194 return { trigger: ".pos-leftheader .preset-time-btn", run: "click" };
195 }
196 export function selectPresetTimingSlotHour(hour) {
197 return [
198 {
199 content: `Click on the slot hour ${hour} in the modal`,
200 trigger: `.modal:has(.modal-header:contains(select a preset)) button:contains('${hour}')`,
201 run: "click",
202 },
203 {
204 content: `Wait the slot hour ${hour} is set and loading is done (to avoid currency error)`,
205 trigger: `body:not(:has(.modal)):not(:has(.oe_status .fa-spin)) .pos-leftheader .preset-time-btn:contains(${hour})`,
206 },
207 ];
208 }
209 export function presetTimingSlotHourNotExists(hour) {
210 return { trigger: negate(`.modal button:visible:contains('${hour}')`) };
211 }
212 export function presetTimingSlotHourExists(hour) {
213 return { trigger: `.modal button:contains('${hour}')` };
214 }
215 export function selectSlotDays(d) {
216 return {
217 trigger: `.modal .d-flex.w-100.flex-wrap.gap-2.mt-2 button:nth-of-type(${d})`,
218 run: "click",
219 };
220 }
221 export function selectPresetTimingSlotIndex(index) {
222 return {
223 trigger: `.modal .row div:not(.d-none) .d-flex.flex-wrap.gap-1 button:nth-of-type(${index})`,
224 run: "click",
225 };
226 }
227 export function clickRegister() {
228 return { trigger: ".pos-leftheader .register-label", run: "click" };
229 }
230 export function waitRequest() {
231 return [
232 {
233 trigger: "body",
234 content: "Wait loading is finished if it is shown",
235 timeout: 15000,
236 async run({ waitFor }) {
237 let isLoading = false;
238 try {
239 isLoading = await waitFor("body:has(.fa-circle-o-notch)", { timeout: 2000 });
240 } catch {
241 /* fa-circle-o-notch will certainly never appears :'( */
242 }
243 if (isLoading) {
244 await waitFor("body:not(:has(.fa-circle-o-notch))", { timeout: 10000 });
245 }
246 },
247 },
248 ];
249 }
250
251 export function storedOrderCount(expectedCount) {
252 return {
253 content: `Stored order count should be ${expectedCount}`,
254 trigger: "body",
255 run: () => {
256 const actualCount = posmodel.data.models["pos.order"].length;
257 if (actualCount !== expectedCount) {
258 throw new Error(
259 `Expected stored order count to be ${expectedCount}, but got ${actualCount}`
260 );
261 }
262 },
263 };
264 }
265
266 export function isSynced() {
267 return {
268 content: "Check if the request is proceeded",
269 trigger: negate(".fa-spin", ".status-buttons"),
270 };
271 }
272
273 export function clickOnScanButton() {
274 return {
275 content: "Click the Scan button located in the top header.",
276 trigger: ".pos-topheader .status-buttons .fa-barcode",
277 run: "click",
278 };
279 }
280
281 export function ClickOnCustomerDisplayButton() {
282 return {
283 content: "Click on the customer display button inside the burger menu",
284 trigger: "span i.fa-desktop",
285 run: "click",
286 };
287 }
288 export function CustomerDisplayHasThisDeviceButton() {
289 return {
290 isActive: ["desktop"],
291 content: "Check that the customer display popup has a 'This device' button",
292 trigger: ".o_dialog .modal-body .container .btn-primary:contains('This device')",
293 };
294 }
295 export function CustomerDisplayHasQRButton() {
296 return {
297 isActive: ["desktop"],
298 content: "Check that the customer display popup has a 'Display QR' button",
299 trigger: ".o_dialog .modal-body .container .btn-secondary:contains('Display QR')",
300 };
301 }
302 export function ClickCustomerDisplayThisDeviceButton() {
303 return {
304 isActive: ["desktop"],
305 content: "Check that the customer display popup has a 'This device' button",
306 trigger: ".btn-primary:contains('This device')",
307 run: "click",
308 };
309 }
310 export function ClickCustomerDisplayQRButton() {
311 return {
312 isActive: ["desktop"],
313 content: "Check that the customer display popup has a 'Display QR' button",
314 trigger: ".btn-secondary:contains('Display QR')",
315 run: "click",
316 };
317 }
318 export function CustomerDisplayQRIsDisplayed() {
319 return {
320 isActive: ["desktop"],
321 content: "Check that the QR code is displayed on screen",
322 trigger: ".o-overlay-item .modal .modal-body img.square",
323 };
324 }
325 export function freezeDateTime(millis) {
326 return [
327 {
328 trigger: "body",
329 run: () => {
330 DateTime.now = () => DateTime.fromMillis(millis);
331 },
332 },
333 ];
334 }
335
336 const originalNow = DateTime.now;
337
338 export function withTimeFreeze(millis, steps) {
339 return [
340 {
341 content: `Freeze time to ${millis}`,
342 trigger: "body",
343 run: () => {
344 sessionStorage.setItem("pos_test_frozen_time", millis);
345 DateTime.now = () => DateTime.fromMillis(millis);
346 },
347 },
348 ...steps,
349 {
350 content: "Unfreeze time",
351 trigger: "body",
352 run: () => {
353 sessionStorage.removeItem("pos_test_frozen_time");
354 DateTime.now = originalNow;
355 },
356 },
357 ].flat();
358 }
359
360 if (sessionStorage.getItem("pos_test_frozen_time")) {
361 const millis = parseInt(sessionStorage.getItem("pos_test_frozen_time"));
362 DateTime.now = () => DateTime.fromMillis(millis);
363 }
364
365 export function selectPresetDateButton(formattedDate) {
366 return {
367 trigger: `.modal-body button:contains("${formattedDate}")`,
368 run: "click",
369 };
370 }