Debian Codegraph @

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

raw
1 import { beforeEach, expect, test } from "@odoo/hoot";
2 import { click, keyDown, pointerDown, queryAll, queryFirst } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 defineModels,
6 defineParams,
7 fields,
8 models,
9 mountView,
10 mountWithCleanup,
11 } from "@web/../tests/web_test_helpers";
12
13 import { Component, useState, xml } from "@odoo/owl";
14 import { useNumpadDecimal } from "@web/views/fields/numpad_decimal_hook";
15
16 class Partner extends models.Model {
17 int_field = fields.Integer();
18 qux = fields.Float({ digits: [16, 1] });
19 currency_id = fields.Many2one({ relation: "currency" });
20 float_factor_field = fields.Float();
21 percentage = fields.Float();
22 monetary = fields.Monetary({ currency_field: "" });
23 progressbar = fields.Integer();
24
25 _records = [
26 {
27 id: 1,
28 int_field: 10,
29 qux: 0.44444,
30 float_factor_field: 9.99,
31 percentage: 0.99,
32 monetary: 9.99,
33 currency_id: 1,
34 progressbar: 69,
35 },
36 ];
37 }
38
39 class Currency extends models.Model {
40 digits = fields.Float();
41 symbol = fields.Char();
42 position = fields.Char();
43
44 _records = [{ id: 1, display_name: "$", symbol: "$", position: "before" }];
45 }
46
47 defineModels([Partner, Currency]);
48
49 beforeEach(() => {
50 defineParams({ lang_parameters: { decimal_point: ",", thousands_sep: "." } });
51 });
52
53 test("Numeric fields: fields with keydown on numpad decimal key", async () => {
54 defineParams({ lang_parameters: { decimal_point: "🇧🇪" } });
55 await mountView({
56 type: "form",
57 resModel: "partner",
58 arch: /* xml */ `
59 <form>
60 <field name="float_factor_field" options="{'factor': 0.5}" widget="float_factor"/>
61 <field name="qux"/>
62 <field name="int_field"/>
63 <field name="monetary"/>
64 <field name="currency_id" invisible="1"/>
65 <field name="percentage" widget="percentage"/>
66 <field name="progressbar" widget="progressbar" options="{'editable': true, 'max_value': 'qux', 'edit_max_value': true}"/>
67 </form>
68 `,
69 resId: 1,
70 });
71
72 // Dispatch numpad "dot" and numpad "comma" keydown events to all inputs and check
73 // Numpad "comma" is specific to some countries (Brazil...)
74 await click(".o_field_float_factor input");
75 await keyDown("ArrowRight", { code: "ArrowRight" });
76 await keyDown(".", { code: "NumpadDecimal" });
77 await keyDown(",", { code: "NumpadDecimal" });
78 await animationFrame();
79 expect(".o_field_float_factor input").toHaveValue("5🇧🇪00🇧🇪🇧🇪");
80
81 await click(".o_field_float input");
82 await keyDown("ArrowRight", { code: "ArrowRight" });
83 await keyDown(".", { code: "NumpadDecimal" });
84 await keyDown(",", { code: "NumpadDecimal" });
85 await animationFrame();
86 expect(".o_field_float input").toHaveValue("0🇧🇪4🇧🇪🇧🇪");
87
88 await click(".o_field_integer input");
89 await keyDown("ArrowRight", { code: "ArrowRight" });
90 await keyDown(".", { code: "NumpadDecimal" });
91 await keyDown(",", { code: "NumpadDecimal" });
92 await animationFrame();
93 expect(".o_field_integer input").toHaveValue("10🇧🇪🇧🇪");
94
95 await click(".o_field_monetary input");
96 await keyDown("ArrowRight", { code: "ArrowRight" });
97 await keyDown(".", { code: "NumpadDecimal" });
98 await keyDown(",", { code: "NumpadDecimal" });
99 await animationFrame();
100 expect(".o_field_monetary input").toHaveValue("9🇧🇪99🇧🇪🇧🇪");
101
102 await click(".o_field_percentage input");
103 await keyDown("ArrowRight", { code: "ArrowRight" });
104 await keyDown(".", { code: "NumpadDecimal" });
105 await keyDown(",", { code: "NumpadDecimal" });
106 await animationFrame();
107 expect(".o_field_percentage input").toHaveValue("99🇧🇪🇧🇪");
108
109 await click(".o_field_progressbar input");
110 await animationFrame();
111
112 await keyDown("ArrowRight", { code: "ArrowRight" });
113 await keyDown(".", { code: "NumpadDecimal" });
114 await keyDown(",", { code: "NumpadDecimal" });
115 await animationFrame();
116 expect(".o_field_progressbar input").toHaveValue("0🇧🇪44🇧🇪🇧🇪");
117 });
118
119 test("Numeric fields: NumpadDecimal key is different from the decimalPoint", async () => {
120 await mountView({
121 type: "form",
122 resModel: "partner",
123 arch: /* xml */ `
124 <form>
125 <field name="float_factor_field" options="{'factor': 0.5}" widget="float_factor"/>
126 <field name="qux"/>
127 <field name="int_field"/>
128 <field name="monetary"/>
129 <field name="currency_id" invisible="1"/>
130 <field name="percentage" widget="percentage"/>
131 <field name="progressbar" widget="progressbar" options="{'editable': true, 'max_value': 'qux', 'edit_max_value': true}"/>
132 </form>
133 `,
134 resId: 1,
135 });
136
137 // Get all inputs
138 const floatFactorField = queryFirst(".o_field_float_factor input");
139 const floatInput = queryFirst(".o_field_float input");
140 const integerInput = queryFirst(".o_field_integer input");
141 const monetaryInput = queryFirst(".o_field_monetary input");
142 const percentageInput = queryFirst(".o_field_percentage input");
143 const progressbarInput = queryFirst(".o_field_progressbar input");
144
145 /**
146 * Common assertion steps are extracted in this procedure.
147 *
148 * @param {object} params
149 * @param {HTMLInputElement} params.el
150 * @param {[number, number]} params.selectionRange
151 * @param {string} params.expectedValue
152 * @param {string} params.msg
153 */
154 async function testInputElementOnNumpadDecimal(params) {
155 const { el, selectionRange, expectedValue, msg } = params;
156
157 await pointerDown(el);
158 await animationFrame();
159 el.setSelectionRange(...selectionRange);
160 const [event] = await keyDown(".", { code: "NumpadDecimal" });
161 if (event.defaultPrevented) {
162 expect.step("preventDefault");
163 }
164 await animationFrame();
165
166 // dispatch an extra keydown event and expect that it's not default prevented
167 const [extraEvent] = await keyDown("1", { code: "Digit1" });
168 if (extraEvent.defaultPrevented) {
169 throw new Error("should not be default prevented");
170 }
171 await animationFrame();
172
173 // Selection range should be at +2 from the specified selection start (separator + character).
174 expect(el.selectionStart).toBe(selectionRange[0] + 2);
175 expect(el.selectionEnd).toBe(selectionRange[0] + 2);
176 await animationFrame();
177 // NumpadDecimal event should be default prevented
178 expect.verifySteps(["preventDefault"]);
179 expect(el).toHaveValue(expectedValue, { message: msg });
180 }
181
182 await testInputElementOnNumpadDecimal({
183 el: floatFactorField,
184 selectionRange: [1, 3],
185 expectedValue: "5,10",
186 msg: "Float factor field from 5,00 to 5,10",
187 });
188
189 await testInputElementOnNumpadDecimal({
190 el: floatInput,
191 selectionRange: [0, 2],
192 expectedValue: ",14",
193 msg: "Float field from 0,4 to ,14",
194 });
195
196 await testInputElementOnNumpadDecimal({
197 el: integerInput,
198 selectionRange: [1, 2],
199 expectedValue: "1,1",
200 msg: "Integer field from 10 to 1,1",
201 });
202
203 await testInputElementOnNumpadDecimal({
204 el: monetaryInput,
205 selectionRange: [0, 3],
206 expectedValue: ",19",
207 msg: "Monetary field from 9,99 to ,19",
208 });
209
210 await testInputElementOnNumpadDecimal({
211 el: percentageInput,
212 selectionRange: [1, 1],
213 expectedValue: "9,19",
214 msg: "Percentage field from 99 to 9,19",
215 });
216
217 await testInputElementOnNumpadDecimal({
218 el: progressbarInput,
219 selectionRange: [1, 3],
220 expectedValue: "0,14",
221 msg: "Progressbar field 2 from 0,44 to 0,14",
222 });
223 });
224
225 test("useNumpadDecimal should synchronize handlers on input elements", async () => {
226 /**
227 * Takes an array of input elements and asserts that each has the correct event listener.
228 * @param {HTMLInputElement[]} inputEls
229 */
230 async function testInputElements(inputEls) {
231 for (const inputEl of inputEls) {
232 await pointerDown(inputEl);
233 await animationFrame();
234 const [event] = await keyDown(".", { code: "NumpadDecimal" });
235 if (event.defaultPrevented) {
236 expect.step("preventDefault");
237 }
238 await animationFrame();
239
240 // dispatch an extra keydown event and expect that it's not default prevented
241 const [extraEvent] = await keyDown("1", { code: "Digit1" });
242 if (extraEvent.defaultPrevented) {
243 throw new Error("should not be default prevented");
244 }
245 await animationFrame();
246
247 expect.verifySteps(["preventDefault"]);
248 }
249 }
250
251 class MyComponent extends Component {
252 static template = xml`
253 <main t-ref="numpadDecimal">
254 <input type="text" placeholder="input 1" />
255 <input t-if="state.showOtherInput" type="text" placeholder="input 2" />
256 </main>
257 `;
258 static props = ["*"];
259 setup() {
260 useNumpadDecimal();
261 this.state = useState({ showOtherInput: false });
262 }
263 }
264
265 const comp = await mountWithCleanup(MyComponent);
266 await animationFrame();
267
268 // Initially, only one input should be rendered.
269 expect("main > input").toHaveCount(1);
270 await testInputElements(queryAll("main > input"));
271
272 // We show the second input by manually updating the state.
273 comp.state.showOtherInput = true;
274 await animationFrame();
275
276 // The second input should also be able to handle numpad decimal.
277 expect("main > input").toHaveCount(2);
278 await testInputElements(queryAll("main > input"));
279 });
280
281 test("select all content on focus", async () => {
282 await mountView({
283 type: "form",
284 resModel: "partner",
285 arch: /* xml */ `<form><field name="monetary"/></form>`,
286 });
287
288 const input = queryFirst(".o_field_widget[name='monetary'] input");
289 await pointerDown(input);
290 await animationFrame();
291 expect(input.selectionStart).toBe(0);
292 expect(input.selectionEnd).toBe(4);
293 });