Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { queryAllProperties, queryAllTexts } from "@odoo/hoot-dom";
3 import {
4 contains,
5 defineModels,
6 fields,
7 models,
8 mountView,
9 onRpc,
10 } from "@web/../tests/web_test_helpers";
11
12 class User extends models.Model {
13 _name = "res.users";
14 has_group() {
15 return true;
16 }
17 }
18
19 class Partner extends models.Model {
20 bar = fields.Boolean({ default: true });
21
22 _records = [
23 { id: 1, bar: true },
24 { id: 2, bar: true },
25 { id: 3, bar: true },
26 { id: 4, bar: true },
27 { id: 5, bar: false },
28 ];
29 }
30
31 defineModels([Partner, User]);
32
33 test("FavoriteField in kanban view", async () => {
34 await mountView({
35 resModel: "partner",
36 domain: [["id", "=", 1]],
37 type: "kanban",
38 arch: `
39 <kanban>
40 <templates>
41 <t t-name="card">
42 <field name="bar" widget="boolean_favorite"/>
43 </t>
44 </templates>
45 </kanban>
46 `,
47 });
48 expect(`.o_kanban_record .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(1, {
49 message: "should be favorite",
50 });
51 expect(`.o_kanban_record .o_field_widget .o_favorite > a`).toHaveText("Remove from Favorites", {
52 message: `the label should say "Remove from Favorites"`,
53 });
54
55 // click on favorite
56 await contains(`.o_field_widget .o_favorite`).click();
57 expect(`.o_kanban_record .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(0, {
58 message: "should not be favorite",
59 });
60 expect(`.o_kanban_record .o_field_widget .o_favorite > a`).toHaveText("Add to Favorites", {
61 message: `the label should say "Add to Favorites"`,
62 });
63 });
64
65 test("FavoriteField saves changes by default", async () => {
66 onRpc("web_save", ({ args }) => {
67 expect.step("save");
68 expect(args).toEqual([[1], { bar: false }]);
69 });
70
71 await mountView({
72 resModel: "partner",
73 domain: [["id", "=", 1]],
74 type: "kanban",
75 arch: `
76 <kanban>
77 <templates>
78 <t t-name="card">
79 <field name="bar" widget="boolean_favorite"/>
80 </t>
81 </templates>
82 </kanban>
83 `,
84 });
85
86 // click on favorite
87 await contains(`.o_field_widget .o_favorite`).click();
88 expect(`.o_kanban_record .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(0, {
89 message: "should not be favorite",
90 });
91 expect(`.o_kanban_record .o_field_widget .o_favorite > a`).toHaveText("Add to Favorites", {
92 message: `the label should say "Add to Favorites"`,
93 });
94 expect.verifySteps(["save"]);
95 });
96
97 test("FavoriteField does not save if autosave option is set to false", async () => {
98 onRpc("web_save", () => {
99 expect.step("save");
100 });
101
102 await mountView({
103 resModel: "partner",
104 domain: [["id", "=", 1]],
105 type: "kanban",
106 arch: `
107 <kanban>
108 <templates>
109 <t t-name="card">
110 <field name="bar" widget="boolean_favorite" options="{'autosave': False}"/>
111 </t>
112 </templates>
113 </kanban>
114 `,
115 });
116
117 // click on favorite
118 await contains(`.o_field_widget .o_favorite`).click();
119 expect(`.o_kanban_record .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(0, {
120 message: "should not be favorite",
121 });
122 expect(`.o_kanban_record .o_field_widget .o_favorite > a`).toHaveText("Add to Favorites", {
123 message: `the label should say "Add to Favorites"`,
124 });
125 expect.verifySteps([]);
126 });
127
128 test("FavoriteField in form view", async () => {
129 onRpc("web_save", () => {
130 expect.step("save");
131 });
132
133 await mountView({
134 resModel: "partner",
135 resId: 1,
136 type: "form",
137 arch: `<form><field name="bar" widget="boolean_favorite"/></form>`,
138 });
139 expect(`.o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(1, {
140 message: "should be favorite",
141 });
142 expect(`.o_field_widget .o_favorite > a`).toHaveText("Remove from Favorites", {
143 message: `the label should say "Remove from Favorites"`,
144 });
145
146 // click on favorite
147 await contains(`.o_field_widget .o_favorite`).click();
148 expect.verifySteps(["save"]);
149 expect(`.o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(0, {
150 message: "should not be favorite",
151 });
152 expect(`.o_field_widget .o_favorite > a i.fa.fa-star-o`).toHaveCount(1, {
153 message: "should not be favorite",
154 });
155 expect(`.o_field_widget .o_favorite > a`).toHaveText("Add to Favorites", {
156 message: `the label should say "Add to Favorites"`,
157 });
158
159 // click on favorite
160 await contains(`.o_field_widget .o_favorite`).click();
161 expect.verifySteps(["save"]);
162 expect(`.o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(1, {
163 message: "should be favorite",
164 });
165 expect(`.o_field_widget .o_favorite > a`).toHaveText("Remove from Favorites", {
166 message: `the label should say "Remove from Favorites"`,
167 });
168 });
169
170 test.tags("desktop");
171 test("FavoriteField in editable list view without label", async () => {
172 onRpc("has_group", () => true);
173
174 await mountView({
175 resModel: "partner",
176 type: "list",
177 arch: `
178 <list editable="bottom">
179 <field name="bar" widget="boolean_favorite" nolabel="1" options="{'autosave': False}"/>
180 </list>
181 `,
182 });
183 expect(`.o_data_row:first .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(1, {
184 message: "should be favorite",
185 });
186
187 // switch to edit mode
188 await contains(`tbody td:not(.o_list_record_selector)`).click();
189 expect(`.o_data_row:first .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(1, {
190 message: "should be favorite",
191 });
192
193 // click on favorite
194 await contains(`.o_data_row .o_field_widget .o_favorite > a`).click();
195 expect(`.o_data_row:first .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(0, {
196 message: "should not be favorite",
197 });
198
199 // save
200 await contains(`.o_list_button_save`).click();
201 expect(`.o_data_row:first .o_field_widget .o_favorite > a i.fa.fa-star-o`).toHaveCount(1, {
202 message: "should not be favorite",
203 });
204 });
205
206 test.tags("desktop");
207 test("FavoriteField in list has a fixed width if no label", async () => {
208 onRpc("has_group", () => true);
209 Partner._fields.char = fields.Char();
210
211 await mountView({
212 resModel: "partner",
213 type: "list",
214 arch: `
215 <list editable="bottom">
216 <field name="bar" widget="boolean_favorite" nolabel="1"/>
217 <field name="bar" widget="boolean_favorite"/>
218 <field name="char"/>
219 </list>
220 `,
221 });
222 const columnWidths = queryAllProperties(".o_list_table thead th", "offsetWidth");
223 const columnLabels = queryAllTexts(".o_list_table thead th");
224 expect(columnWidths[1]).toBe(29);
225 expect(columnLabels[1]).toBe("");
226 expect(columnWidths[2]).toBeGreaterThan(29);
227 expect(columnLabels[2]).toBe("Bar");
228 });
229
230 test("FavoriteField in kanban view with readonly attribute", async () => {
231 onRpc("web_save", () => {
232 expect.step("should not save");
233 });
234 await mountView({
235 resModel: "partner",
236 domain: [["id", "=", 1]],
237 type: "kanban",
238 arch: `
239 <kanban>
240 <templates>
241 <t t-name="card">
242 <field name="bar" widget="boolean_favorite" readonly="1"/>
243 </t>
244 </templates>
245 </kanban>
246 `,
247 });
248 expect(`.o_kanban_record .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(1, {
249 message: "should be favorite",
250 });
251 expect(`.o_kanban_record .o_field_widget .o_favorite > a`).toHaveClass("pe-none");
252 expect(`.o_kanban_record .o_field_widget .o_favorite`).toHaveClass("o_disabled");
253 expect(`.o_kanban_record .o_field_widget`).toHaveText("");
254
255 // click on favorite
256 await contains(`.o_field_widget .o_favorite`).click();
257 // expect nothing to change since its readonly
258 expect(`.o_kanban_record .o_field_widget .o_favorite > a i.fa.fa-star`).toHaveCount(1, {
259 message: "should remain favorite",
260 });
261 expect.verifySteps([]);
262 });