Debian Codegraph @

addons/web/static/tests/views/fields/many2one_barcode_field.test.js

raw
1 import { beforeEach, expect, test } from "@odoo/hoot";
2 import { mockUserAgent, mockVibrate, runAllTimers } from "@odoo/hoot-mock";
3
4 import {
5 clickSave,
6 contains,
7 defineModels,
8 fields,
9 getKwArgs,
10 models,
11 mountView,
12 onRpc,
13 patchWithCleanup,
14 } from "@web/../tests/web_test_helpers";
15
16 import * as BarcodeScanner from "@web/core/barcode/barcode_dialog";
17
18 class Product extends models.Model {
19 _name = "product.product";
20 name = fields.Char();
21 barcode = fields.Char();
22
23 _records = [
24 {
25 id: 111,
26 name: "product_cable_management_box",
27 barcode: "601647855631",
28 },
29 {
30 id: 112,
31 name: "product_n95_mask",
32 barcode: "601647855632",
33 },
34 {
35 id: 113,
36 name: "product_surgical_mask",
37 barcode: "601647855633",
38 },
39 ];
40 // to allow the search in barcode too
41 name_search() {
42 const result = super.name_search(...arguments);
43 const kwargs = getKwArgs(arguments, "name", "domain", "operator", "limit");
44 for (const record of this) {
45 if (record.barcode === kwargs.name) {
46 result.push([record.id, record.name]);
47 }
48 }
49 return result;
50 }
51 _views = {
52 kanban: `
53 <kanban>
54 <templates>
55 <t t-name="card">
56 <field name="id"/>
57 <field name="name"/>
58 <field name="barcode"/>
59 </t>
60 </templates>
61 </kanban>`,
62 search: "<search/>",
63 };
64 }
65
66 class SaleOrderLine extends models.Model {
67 id = fields.Integer();
68 product_id = fields.Many2one({
69 relation: "product.product",
70 });
71 }
72
73 class User extends models.Model {
74 _name = "res.users";
75 has_group() {
76 return true;
77 }
78 }
79
80 defineModels([Product, SaleOrderLine, User]);
81
82 beforeEach(() => {
83 mockUserAgent("android");
84 mockVibrate((pattern) => expect.step(`vibrate:${pattern}`));
85 });
86
87 test("Many2OneBarcode component should display the barcode icon", async () => {
88 await mountView({
89 type: "form",
90 resModel: "sale.order.line",
91 arch: `
92 <form>
93 <field name="product_id" widget="many2one_barcode"/>
94 </form>
95 `,
96 });
97 expect(".o_barcode").toHaveCount(1);
98 });
99
100 test("barcode button with single results", async () => {
101 expect.assertions(3);
102
103 // The product selected (mock) for the barcode scanner
104 const selectedRecordTest = Product._records[0];
105
106 patchWithCleanup(BarcodeScanner, {
107 scanBarcode: async () => selectedRecordTest.barcode,
108 });
109
110 onRpc("sale.order.line", "web_save", (args) => {
111 const selectedId = args.args[1]["product_id"];
112 expect(selectedId).toBe(selectedRecordTest.id, {
113 message: `product id selected ${selectedId}, should be ${selectedRecordTest.id} (${selectedRecordTest.barcode})`,
114 });
115 return args.parent();
116 });
117
118 await mountView({
119 type: "form",
120 resModel: "sale.order.line",
121 arch: `
122 <form>
123 <field name="product_id" options="{'can_scan_barcode': True}"/>
124 </form>
125 `,
126 });
127
128 expect(".o_barcode").toHaveCount(1);
129
130 await contains(".o_barcode").click();
131 await clickSave();
132
133 expect.verifySteps(["vibrate:100"]);
134 });
135
136 test.tags("desktop");
137 test("barcode button with multiple results on desktop", async () => {
138 expect.assertions(5);
139
140 // The product selected (mock) for the barcode scanner
141 const selectedRecordTest = Product._records[1];
142
143 patchWithCleanup(BarcodeScanner, {
144 scanBarcode: async () => "mask",
145 });
146
147 onRpc("sale.order.line", "web_save", (args) => {
148 const selectedId = args.args[1]["product_id"];
149 expect(selectedId).toBe(selectedRecordTest.id, {
150 message: `product id selected ${selectedId}, should be ${selectedRecordTest.id} (${selectedRecordTest.barcode})`,
151 });
152 return args.parent();
153 });
154 await mountView({
155 type: "form",
156 resModel: "sale.order.line",
157 arch: `
158 <form>
159 <field name="product_id" options="{'can_scan_barcode': True}"/>
160 </form>`,
161 });
162
163 expect(".o_barcode").toHaveCount(1);
164
165 await contains(".o_barcode").click();
166 await runAllTimers();
167 expect(".o-autocomplete--dropdown-menu").toHaveCount(1);
168
169 expect(
170 ".o-autocomplete--dropdown-menu .o-autocomplete--dropdown-item.ui-menu-item:not(.o_m2o_dropdown_option)"
171 ).toHaveCount(2);
172
173 await contains(
174 ".o-autocomplete--dropdown-menu .o-autocomplete--dropdown-item:nth-child(1)"
175 ).click();
176 await clickSave();
177 expect.verifySteps(["vibrate:100"]);
178 });
179
180 test.tags("mobile");
181 test("barcode button with multiple results on mobile", async () => {
182 expect.assertions(5);
183
184 // The product selected (mock) for the barcode scanner
185 const selectedRecordTest = Product._records[1];
186
187 patchWithCleanup(BarcodeScanner, {
188 scanBarcode: async () => "mask",
189 });
190
191 onRpc("sale.order.line", "web_save", (args) => {
192 const selectedId = args.args[1]["product_id"];
193 expect(selectedId).toBe(selectedRecordTest.id, {
194 message: `product id selected ${selectedId}, should be ${selectedRecordTest.id} (${selectedRecordTest.barcode})`,
195 });
196 return args.parent();
197 });
198
199 await mountView({
200 type: "form",
201 resModel: "sale.order.line",
202 arch: `<form><field name="product_id" options="{'can_scan_barcode': True}"/></form>`,
203 });
204
205 expect(".o_barcode").toHaveCount(1, { message: "has scanner barcode button" });
206
207 await contains(".o_barcode").click();
208
209 expect(".modal-dialog.modal-lg").toHaveCount(1, {
210 message: "there should be one modal opened in full screen",
211 });
212 expect(".modal-dialog.modal-lg .o_kanban_record:not(.o_kanban_ghost)").toHaveCount(2, {
213 message: "there should be 2 records displayed",
214 });
215
216 await contains(".o_kanban_record:nth-child(1)").click();
217 await clickSave();
218 expect.verifySteps(["vibrate:100"]);
219 });
220
221 test.tags("mobile");
222 test("many2one with barcode show all records", async () => {
223 // The product selected (mock) for the barcode scanner
224 const selectedRecordTest = Product._records[1];
225
226 patchWithCleanup(BarcodeScanner, {
227 scanBarcode: async () => selectedRecordTest.barcode,
228 });
229
230 await mountView({
231 type: "form",
232 resModel: "sale.order.line",
233 arch: `<form><field name="product_id" options="{'can_scan_barcode': True}"/></form>`,
234 });
235
236 // Select one product
237 await contains(".o_barcode").click();
238
239 // Click on the input to show all records
240 await contains(".o_input_dropdown > input").click();
241
242 expect(".modal-dialog.modal-lg").toHaveCount(1, {
243 message: "there should be one modal opened in full screen",
244 });
245 expect(".modal-dialog.modal-lg .o_kanban_record:not(.o_kanban_ghost)").toHaveCount(3, {
246 message: "there should be 3 records displayed",
247 });
248 expect.verifySteps(["vibrate:100"]);
249 });