Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, edit, pointerDown, queryAll, queryFirst } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 clickSave,
6 contains,
7 defineModels,
8 fields,
9 models,
10 mountView,
11 onRpc,
12 serverState,
13 } from "@web/../tests/web_test_helpers";
14
15 const RED_TEXT = /* html */ `<div class="kek" style="color:red">some text</div>`;
16 const GREEN_TEXT = /* html */ `<div class="kek" style="color:green">hello</div>`;
17 const BLUE_TEXT = /* html */ `<div class="kek" style="color:blue">hello world</div>`;
18
19 class Partner extends models.Model {
20 txt = fields.Html({ string: "txt", trim: true });
21 _records = [{ id: 1, txt: RED_TEXT }];
22 }
23 class User extends models.Model {
24 _name = "res.users";
25 has_group() {
26 return true;
27 }
28 }
29 defineModels([Partner, User]);
30
31 test("html fields are correctly rendered in form view (readonly)", async () => {
32 await mountView({
33 type: "form",
34 resModel: "partner",
35 resId: 1,
36 arch: /* xml */ `<form><field name="txt" readonly="1" /></form>`,
37 });
38
39 expect("div.kek").toHaveCount(1);
40 expect(".o_field_html .kek").toHaveStyle({ color: "rgb(255, 0, 0)" });
41 expect(".o_field_html").toHaveText("some text");
42 });
43
44 test("html field with required attribute", async () => {
45 await mountView({
46 type: "form",
47 resModel: "partner",
48 resId: 1,
49 arch: /* xml */ `<form><field name="txt" required="1"/></form>`,
50 });
51
52 expect(".o_field_html textarea").toHaveCount(1, { message: "should have a text area" });
53 await click(".o_field_html textarea");
54 await edit("");
55 await animationFrame();
56 expect(".o_field_html textarea").toHaveValue("");
57
58 await clickSave();
59 expect(queryFirst(".o_notification_content")).toHaveText("Missing required fields");
60 });
61
62 test("html fields are correctly rendered (edit)", async () => {
63 onRpc("has_group", () => true);
64 await mountView({
65 type: "form",
66 resModel: "partner",
67 resId: 1,
68 arch: /* xml */ `<form><field name="txt" /></form>`,
69 });
70
71 expect(".o_field_html textarea").toHaveCount(1, { message: "should have a text area" });
72 expect(".o_field_html textarea").toHaveValue(RED_TEXT);
73 await click(".o_field_html textarea");
74 await edit(GREEN_TEXT);
75 await animationFrame();
76 expect(".o_field_html textarea").toHaveValue(GREEN_TEXT);
77 expect(".o_field_html .kek").toHaveCount(0);
78
79 await edit(BLUE_TEXT);
80 await animationFrame();
81 expect(".o_field_html textarea").toHaveValue(BLUE_TEXT);
82 });
83
84 test("html fields are correctly rendered in list view", async () => {
85 onRpc("has_group", () => true);
86 await mountView({
87 type: "list",
88 resModel: "partner",
89 arch: /* xml */ `<list editable="top"><field name="txt"/></list>`,
90 });
91 expect(".o_data_row [name='txt']").toHaveText("some text");
92 expect(".o_data_row [name='txt'] .kek").toHaveStyle({ color: "rgb(255, 0, 0)" });
93
94 await click(".o_data_row [name='txt']");
95 await animationFrame();
96 expect(".o_data_row [name='txt'] textarea").toHaveValue(
97 '<div class="kek" style="color:red">some text</div>'
98 );
99 });
100
101 test("html field displays an empty string for the value false in list view", async () => {
102 Partner._records[0].txt = false;
103 onRpc("has_group", () => true);
104
105 await mountView({
106 type: "list",
107 resModel: "partner",
108 arch: /* xml */ `<list editable="top"><field name="txt"/></list>`,
109 });
110
111 expect(".o_data_row [name='txt']").toHaveText("");
112
113 await click(".o_data_row [name='txt']");
114 await animationFrame();
115
116 expect(".o_data_row [name='txt'] textarea").toHaveValue("");
117 });
118
119 test("html fields are correctly rendered in kanban view", async () => {
120 await mountView({
121 type: "kanban",
122 resModel: "partner",
123 arch: /* xml */ `
124 <kanban class="o_kanban_test">
125 <templates>
126 <t t-name="card">
127 <field name="txt"/>
128 </t>
129 </templates>
130 </kanban>`,
131 });
132 expect(".kek").toHaveText("some text");
133 expect(".kek").toHaveStyle({ color: "rgb(255, 0, 0)" });
134 });
135
136 test("field html translatable", async () => {
137 expect.assertions(10);
138
139 Partner._fields.txt = fields.Html({ string: "txt", trim: true, translate: true });
140
141 serverState.lang = "en_US";
142 serverState.multiLang = true;
143
144 onRpc("has_group", () => true);
145 onRpc("get_field_translations", function ({ args }) {
146 expect(args).toEqual([[1], "txt"], {
147 message: "should translate the txt field of the record",
148 });
149 return [
150 [
151 { lang: "en_US", source: "first paragraph", value: "first paragraph" },
152 {
153 lang: "en_US",
154 source: "second paragraph",
155 value: "second paragraph",
156 },
157 {
158 lang: "fr_BE",
159 source: "first paragraph",
160 value: "",
161 },
162 {
163 lang: "fr_BE",
164 source: "second paragraph",
165 value: "deuxième paragraphe",
166 },
167 ],
168 { translation_type: "char", translation_show_source: true },
169 ];
170 });
171 onRpc("get_installed", () => [
172 ["en_US", "English"],
173 ["fr_BE", "French (Belgium)"],
174 ]);
175 onRpc("update_field_translations", ({ args }) => {
176 expect(args).toEqual(
177 [
178 [1],
179 "txt",
180 {
181 en_US: { "first paragraph": "first paragraph modified" },
182 fr_BE: {
183 "first paragraph": "premier paragraphe modifié",
184 "second paragraph": "deuxième paragraphe modifié",
185 },
186 },
187 ],
188 { message: "the new translation value should be written" }
189 );
190 return [];
191 });
192
193 await mountView({
194 type: "form",
195 resModel: "partner",
196 resId: 1,
197 arch: /* xml */ `
198 <form string="Partner">
199 <sheet>
200 <group>
201 <field name="txt" widget="html"/>
202 </group>
203 </sheet>
204 </form>`,
205 });
206
207 expect("[name=txt] textarea").toHaveClass("o_field_translate");
208 await contains("[name=txt] textarea").click();
209 expect(".o_field_html .btn.o_field_translate").toHaveCount(1, {
210 message: "should have a translate button",
211 });
212 expect(".o_field_html .btn.o_field_translate").toHaveText("EN", {
213 message: "the button should have as test the current language",
214 });
215
216 await click(".o_field_html .btn.o_field_translate");
217 await animationFrame();
218
219 expect(".modal").toHaveCount(1, { message: "a translate modal should be visible" });
220 expect(".translation").toHaveCount(4, { message: "four rows should be visible" });
221
222 const translations = queryAll(".modal .o_translation_dialog .translation input");
223
224 const enField1 = translations[0];
225 expect(enField1).toHaveValue("first paragraph", {
226 message: "first part of english translation should be filled",
227 });
228 await click(enField1);
229 await edit("first paragraph modified");
230
231 const frField1 = translations[2];
232 expect(frField1).toHaveValue("", {
233 message: "first part of french translation should not be filled",
234 });
235 await click(frField1);
236 await edit("premier paragraphe modifié");
237
238 const frField2 = translations[3];
239 expect(frField2).toHaveValue("deuxième paragraphe", {
240 message: "second part of french translation should be filled",
241 });
242
243 await click(frField2);
244 await edit("deuxième paragraphe modifié");
245
246 await click(".modal button.btn-primary"); // save
247 await animationFrame();
248 });
249
250 test("html fields: spellcheck is disabled on blur", async () => {
251 await mountView({
252 type: "form",
253 resModel: "partner",
254 resId: 1,
255 arch: /* xml */ `<form><field name="txt" /></form>`,
256 });
257
258 const textarea = queryFirst(".o_field_html textarea");
259 expect(textarea).toHaveProperty("spellcheck", true, {
260 message: "by default, spellcheck is enabled",
261 });
262 await click(textarea);
263
264 await edit("nev walue");
265 await pointerDown(document.body);
266 await animationFrame();
267 expect(textarea).toHaveProperty("spellcheck", false, {
268 message: "spellcheck is disabled once the field has lost its focus",
269 });
270
271 await pointerDown(textarea);
272
273 expect(textarea).toHaveProperty("spellcheck", true, {
274 message: "spellcheck is re-enabled once the field is focused",
275 });
276 });
277
278 test("Setting an html field to empty string is saved as a false value", async () => {
279 expect.assertions(1);
280 onRpc("web_save", ({ args }) => {
281 expect(args[1].txt).toBe(false, { message: "the txt value should be false" });
282 });
283 await mountView({
284 type: "form",
285 resModel: "partner",
286 arch: /* xml */ `
287 <form>
288 <sheet>
289 <group>
290 <field name="txt" />
291 </group>
292 </sheet>
293 </form>`,
294 resId: 1,
295 });
296
297 await click(".o_field_widget[name=txt] textarea");
298 await edit("");
299 await clickSave();
300 });
301
302 test("html field: correct value is used to evaluate the modifiers", async () => {
303 Partner._fields.foo = fields.Char({
304 string: "foo",
305 onChange: (obj) => {
306 if (obj.foo === "a") {
307 obj.txt = false;
308 } else if (obj.foo === "b") {
309 obj.txt = "";
310 }
311 },
312 });
313
314 Partner._records[0].foo = false;
315 Partner._records[0].txt = false;
316 await mountView({
317 type: "form",
318 resModel: "partner",
319 resId: 1,
320 arch: /* xml */ `
321 <form>
322 <field name="foo" />
323 <field name="txt" invisible="'' == txt"/>
324 </form>`,
325 });
326 expect("[name='txt'] textarea").toHaveCount(1);
327
328 await click("[name='foo'] input");
329 await edit("a", { confirm: "enter" });
330 await animationFrame();
331 expect("[name='txt'] textarea").toHaveCount(1);
332
333 await edit("b", { confirm: "enter" });
334 await animationFrame();
335 expect("[name='txt'] textarea").toHaveCount(0);
336 });