Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { press, queryAll, queryOne } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 contains,
6 defineModels,
7 fieldInput,
8 fields,
9 models,
10 mountView,
11 onRpc,
12 serverState,
13 } from "@web/../tests/web_test_helpers";
14
15 function fieldTextArea(name) {
16 return contains(`.o_field_widget[name='${name}'] textarea`);
17 }
18
19 class Product extends models.Model {
20 description = fields.Text();
21 }
22
23 defineModels([Product]);
24
25 onRpc("has_group", () => true);
26
27 test("basic rendering", async () => {
28 Product._records = [{ id: 1, description: "Description as text" }];
29 await mountView({
30 type: "form",
31 resModel: "product",
32 resId: 1,
33 arch: '<form><field name="description"/></form>',
34 });
35 expect(".o_field_text textarea").toHaveCount(1);
36 expect(".o_field_text textarea").toHaveValue("Description as text");
37 });
38
39 test("doesn't have a scrollbar with long content", async () => {
40 Product._records = [{ id: 1, description: "L\no\nn\ng\nD\ne\ns\nc\nr\ni\np\nt\ni\no\nn\n" }];
41 await mountView({
42 type: "form",
43 resModel: "product",
44 resId: 1,
45 arch: '<form><field name="description"/></form>',
46 });
47 const textarea = queryOne(".o_field_text textarea");
48 expect(textarea.clientHeight).toBe(textarea.scrollHeight);
49 });
50
51 test("basic rendering char field", async () => {
52 Product._fields.name = fields.Char();
53 Product._records = [{ id: 1, name: "Description\nas\ntext" }];
54 await mountView({
55 type: "form",
56 resModel: "product",
57 resId: 1,
58 arch: '<form><field name="name" widget="text"/></form>',
59 });
60 expect(".o_field_text textarea").toHaveCount(1);
61 expect(".o_field_text textarea").toHaveValue("Description\nas\ntext");
62 });
63
64 test("char field with widget='text' trims trailing spaces", async () => {
65 Product._fields.name = fields.Char({ trim: true });
66 await mountView({
67 type: "form",
68 resModel: "product",
69 arch: '<form><field name="name" widget="text"/></form>',
70 });
71 await fieldTextArea("name").edit("test ");
72 expect(".o_field_text textarea").toHaveValue("test");
73 });
74
75 test("render following an onchange", async () => {
76 Product._fields.name = fields.Char({
77 onChange: (record) => {
78 expect.step("onchange");
79 record.description = "Content ".repeat(100); // long text
80 },
81 });
82 Product._records = [{ id: 1, description: "Description as text" }];
83 await mountView({
84 type: "form",
85 resModel: "product",
86 resId: 1,
87 arch: `<form><field name="description"/><field name="name"/></form>`,
88 });
89 const textarea = queryOne(".o_field_text textarea");
90 const initialHeight = textarea.offsetHeight;
91 await fieldInput("name").edit("Let's trigger the onchange");
92 await animationFrame();
93 expect(textarea.offsetHeight).toBeGreaterThan(initialHeight);
94 await fieldTextArea("description").edit("Description as text");
95 expect(textarea.offsetHeight).toBe(initialHeight);
96 expect(textarea.clientHeight).toBe(textarea.scrollHeight);
97 expect.verifySteps(["onchange"]);
98 });
99
100 test("no scroll bar in editable list", async () => {
101 Product._records = [{ id: 1, description: "L\no\nn\ng\nD\ne\ns\nc\nr\ni\np\nt\ni\no\nn\n" }];
102 await mountView({
103 type: "list",
104 resModel: "product",
105 arch: '<list editable="top"><field name="description"/></list>',
106 });
107 await contains(".o_data_row .o_data_cell").click();
108 const textarea = queryOne(".o_field_text textarea");
109 expect(textarea.clientHeight).toBe(textarea.scrollHeight);
110 await contains("tr:not(.o_data_row)").click();
111 const cell = queryOne(".o_data_row .o_data_cell");
112 expect(cell.clientHeight).toBe(cell.scrollHeight);
113 });
114
115 test("set row on text fields", async () => {
116 Product._records = [{ id: 1, description: "Description as text" }];
117 await mountView({
118 type: "form",
119 resModel: "product",
120 resId: 1,
121 arch: `<form><field name="description" rows="40"/><field name="description"/></form>`,
122 });
123 const textareas = queryAll(".o_field_text textarea");
124 expect(textareas[0].rows).toBe(40);
125 expect(textareas[0].clientHeight).toBeGreaterThan(textareas[1].clientHeight);
126 });
127
128 test("is translatable", async () => {
129 Product._fields.description.translate = true;
130 Product._records = [{ id: 1, description: "Description as text" }];
131
132 serverState.multiLang = true;
133
134 onRpc("get_installed", () => [
135 ["en_US", "English"],
136 ["fr_BE", "French (Belgium)"],
137 ]);
138 onRpc("get_field_translations", () => [
139 [
140 { lang: "en_US", source: "Description as text", value: "Description as text" },
141 {
142 lang: "fr_BE",
143 source: "Description as text",
144 value: "Description sous forme de texte",
145 },
146 ],
147 { translation_type: "text", translation_show_source: false },
148 ]);
149 await mountView({
150 type: "form",
151 resModel: "product",
152 resId: 1,
153 arch: `<form><sheet><group><field name="description"/></group></sheet></form>`,
154 });
155 expect(".o_field_text textarea").toHaveClass("o_field_translate");
156 await contains(".o_field_text textarea").click();
157 expect(".o_field_text .btn.o_field_translate").toHaveCount(1);
158 await contains(".o_field_text .btn.o_field_translate").click();
159 expect(".modal").toHaveCount(1);
160 });
161
162 test("is translatable on new record", async () => {
163 Product._fields.description.translate = true;
164 Product._records = [{ id: 1, description: "Description as text" }];
165
166 serverState.multiLang = true;
167
168 await mountView({
169 type: "form",
170 resModel: "product",
171 arch: `<form><sheet><group><field name="description"/></group></sheet></form>`,
172 });
173 expect(".o_field_text .btn.o_field_translate").toHaveCount(1);
174 });
175
176 test("press enter inside editable list", async () => {
177 Product._records = [{ id: 1, description: "Description as text" }];
178 await mountView({
179 type: "list",
180 resModel: "product",
181 arch: `
182 <list editable="top">
183 <field name="description" />
184 </list>`,
185 });
186 await contains(".o_data_row .o_data_cell").click();
187 expect("textarea.o_input").toHaveCount(1);
188 expect("textarea.o_input").toHaveValue("Description as text");
189 expect("textarea.o_input").toBeFocused();
190 expect("textarea.o_input").toHaveValue("Description as text");
191 // clear selection before enter
192 await fieldTextArea("description").press(["right", "Enter"]);
193 expect("textarea.o_input").toHaveValue("Description as text\n");
194 expect("textarea.o_input").toBeFocused();
195 expect("tr.o_data_row").toHaveCount(1);
196 });
197
198 test("in editable list view", async () => {
199 Product._records = [{ id: 1, description: "Description as text" }];
200 await mountView({
201 type: "list",
202 resModel: "product",
203 arch: '<list editable="top"><field name="description"/></list>',
204 });
205 await contains(".o_list_button_add").click();
206 expect("textarea").toBeFocused();
207 });
208
209 test("placeholder_field shows as placeholder", async () => {
210 Product._fields.char = fields.Char({
211 default: "My Placeholder",
212 });
213 await mountView({
214 type: "form",
215 resModel: "product",
216 arch: `<form>
217 <field name="description" options="{'placeholder_field' : 'char'}"/>
218 <field name="char"/>
219 </form>`,
220 });
221 expect(`.o_field_text textarea`).toHaveAttribute("placeholder", "My Placeholder");
222 });
223
224 test.tags("desktop");
225 test("with dynamic placeholder", async () => {
226 onRpc("mail_allowed_qweb_expressions", () => []);
227
228 Product._fields.placeholder = fields.Char({ default: "product" });
229 await mountView({
230 type: "form",
231 resModel: "product",
232 arch: `
233 <form>
234 <field name="placeholder" invisible="1"/>
235 <sheet>
236 <group>
237 <field
238 name="description"
239 options="{
240 'dynamic_placeholder': true,
241 'dynamic_placeholder_model_reference_field': 'placeholder'
242 }"
243 />
244 </group>
245 </sheet>
246 </form>`,
247 });
248 expect(".o_popover .o_model_field_selector_popover").toHaveCount(0);
249 await press(["alt", "#"]);
250 await animationFrame();
251 expect(".o_popover .o_model_field_selector_popover").toHaveCount(1);
252 });
253
254 test.tags("mobile");
255 test("with dynamic placeholder in mobile", async () => {
256 onRpc("mail_allowed_qweb_expressions", () => []);
257
258 Product._fields.placeholder = fields.Char({ default: "product" });
259 await mountView({
260 type: "form",
261 resModel: "product",
262 arch: `
263 <form>
264 <field name="placeholder" invisible="1"/>
265 <sheet>
266 <group>
267 <field
268 name="description"
269 options="{
270 'dynamic_placeholder': true,
271 'dynamic_placeholder_model_reference_field': 'placeholder'
272 }"
273 />
274 </group>
275 </sheet>
276 </form>`,
277 });
278 expect(".o_popover .o_model_field_selector_popover").toHaveCount(0);
279 await fieldTextArea("description").focus();
280 await press(["alt", "#"]);
281 await animationFrame();
282 expect(".o_popover .o_model_field_selector_popover").toHaveCount(1);
283 });
284
285 test("text field without line breaks", async () => {
286 Product._records = [{ id: 1, description: "Description as text" }];
287 await mountView({
288 type: "form",
289 resModel: "product",
290 resId: 1,
291 arch: `<form><field name="description" options="{'line_breaks': False}"/></form>`,
292 });
293
294 expect(".o_field_text textarea").toHaveCount(1);
295 expect(".o_field_text textarea").toHaveValue("Description as text");
296 await contains(".o_field_text textarea").click();
297 await press("Enter");
298 expect(".o_field_text textarea").toHaveValue("Description as text");
299
300 await contains(".o_field_text textarea").clear({ confirm: false });
301 await navigator.clipboard.writeText("text\nwith\nline\nbreaks\n"); // copy
302 await press(["ctrl", "v"]); // paste
303 expect(".o_field_text textarea").toHaveValue("text with line breaks ", {
304 message: "no line break should appear",
305 });
306 });