Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import {
3 contains,
4 defineModels,
5 editSelectMenu,
6 fields,
7 models,
8 mountView,
9 patchWithCleanup,
10 onRpc,
11 } from "@web/../tests/web_test_helpers";
12 import { TimezoneMismatchField } from "@web/views/fields/timezone_mismatch/timezone_mismatch_field";
13
14 class Localization extends models.Model {
15 country = fields.Selection({
16 selection: [
17 ["belgium", "Belgium"],
18 ["usa", "United States"],
19 ],
20 onChange: (record) => {
21 record.tz_offset = "+4800";
22 },
23 });
24 tz_offset = fields.Char();
25 _records = [{ id: 1, country: "belgium" }];
26 }
27
28 defineModels([Localization]);
29
30 test("in a list view", async () => {
31 onRpc("has_group", () => true);
32 await mountView({
33 type: "list",
34 resModel: "localization",
35 resId: 1,
36 arch: /*xml*/ `
37 <list string="Localizations" editable="top">
38 <field name="tz_offset" column_invisible="True"/>
39 <field name="country" widget="timezone_mismatch" />
40 </list>
41 `,
42 });
43 expect("td:contains(Belgium)").toHaveCount(1);
44 await contains(".o_data_cell").click();
45 await editSelectMenu(".o_field_widget[name='country'] input", { value: "United States" });
46 expect(".o_data_cell input").toHaveValue(
47 /United States\s+\([0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+:[0-9]+\)/
48 );
49 expect(".o_tz_warning").toHaveCount(1);
50 });
51
52 test("in a form view", async () => {
53 await mountView({
54 type: "form",
55 resModel: "localization",
56 resId: 1,
57 arch: /*xml*/ `
58 <form>
59 <field name="tz_offset" invisible="True"/>
60 <field name="country" widget="timezone_mismatch" />
61 </form>
62 `,
63 });
64 await contains(".o_field_widget[name='country'] input").click();
65 expect(".o_select_menu_item:contains(Belgium)").toHaveCount(1);
66 await editSelectMenu(".o_field_widget[name='country'] input", { value: "United States" });
67 expect(".o_field_widget[name='country'] input").toHaveValue(
68 /United States\s+\([0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+:[0-9]+\)/
69 );
70 expect(".o_tz_warning").toHaveCount(1);
71 });
72
73 test("timezone_mismatch_field mismatch property", () => {
74 const testCases = [
75 {userOffset: "-1030", browserOffset: 630, expectedMismatch: false},
76 {userOffset: "+0000", browserOffset: 0, expectedMismatch: false},
77 {userOffset: "+0345", browserOffset: -225, expectedMismatch: false},
78 {userOffset: "+0500", browserOffset: -300, expectedMismatch: false},
79 {userOffset: "+0200", browserOffset: 120, expectedMismatch: true},
80 {userOffset: "+1200", browserOffset: 0, expectedMismatch: true},
81 ];
82
83 for (const testCase of testCases) {
84 patchWithCleanup(Date.prototype, {
85 getTimezoneOffset: () => testCase.browserOffset,
86 });
87
88 patchWithCleanup(TimezoneMismatchField.prototype, {
89 props: {
90 name: "tz",
91 tzOffsetField: "tz_offset",
92 record: {
93 data: {
94 tz: "Test/Test",
95 tz_offset: testCase.userOffset,
96 },
97 },
98 },
99 });
100
101 const mockField = Object.create(TimezoneMismatchField.prototype);
102
103 expect(mockField.mismatch).toBe(testCase.expectedMismatch);
104 }
105 });