Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, edit } from "@odoo/hoot-dom";
3 import { animationFrame, runAllTimers } from "@odoo/hoot-mock";
4 import {
5 defineModels,
6 fields,
7 models,
8 mountView,
9 patchWithCleanup,
10 } from "@web/../tests/web_test_helpers";
11
12 import { KanbanController } from "@web/views/kanban/kanban_controller";
13
14 const FR_FLAG_URL = "/base/static/img/country_flags/fr.png";
15 const EN_FLAG_URL = "/base/static/img/country_flags/gb.png";
16
17 class Partner extends models.Model {
18 name = fields.Char();
19 foo = fields.Char();
20 p = fields.One2many({ relation: "partner" });
21 timmy = fields.Many2many({ relation: "partner.type" });
22
23 _records = [{ id: 1, foo: FR_FLAG_URL, timmy: [] }];
24 }
25
26 class PartnerType extends models.Model {
27 name = fields.Char();
28 color = fields.Integer();
29
30 _records = [
31 { id: 12, display_name: "gold", color: 2 },
32 { id: 14, display_name: "silver", color: 5 },
33 ];
34 }
35
36 class User extends models.Model {
37 _name = "res.users";
38 has_group() {
39 return true;
40 }
41 }
42
43 defineModels([Partner, PartnerType, User]);
44
45 test("image fields are correctly rendered", async () => {
46 await mountView({
47 type: "form",
48 resModel: "partner",
49 arch: /* xml */ `
50 <form>
51 <field name="foo" widget="image_url" options="{'size': [90, 90]}"/>
52 </form>
53 `,
54 resId: 1,
55 });
56
57 expect(`div[name="foo"]`).toHaveClass("o_field_image_url", {
58 message: "the widget should have the correct class",
59 });
60 expect(`div[name="foo"] > img`).toHaveCount(1, {
61 message: "the widget should contain an image",
62 });
63 expect(`div[name="foo"] > img`).toHaveAttribute("data-src", FR_FLAG_URL, {
64 message: "the image should have the correct src",
65 });
66 expect(`div[name="foo"] > img`).toHaveClass("img-fluid", {
67 message: "the image should have the correct class",
68 });
69 expect(`div[name="foo"] > img`).toHaveAttribute("width", "90", {
70 message: "the image should correctly set its attributes",
71 });
72 expect(`div[name="foo"] > img`).toHaveStyle("maxWidth: 90px", {
73 message: "the image should correctly set its attributes",
74 });
75 });
76
77 test("ImageUrlField in subviews are loaded correctly", async () => {
78 PartnerType._fields.image = fields.Char();
79 PartnerType._records[0].image = EN_FLAG_URL;
80 Partner._records[0].timmy = [12];
81
82 await mountView({
83 type: "form",
84 resModel: "partner",
85 arch: /* xml */ `
86 <form>
87 <field name="foo" widget="image_url" options="{'size': [90, 90]}"/>
88 <field name="timmy" widget="many2many" mode="kanban">
89 <kanban>
90 <templates>
91 <t t-name="card">
92 <field name="display_name"/>
93 </t>
94 </templates>
95 </kanban>
96 <form>
97 <field name="image" widget="image_url"/>
98 </form>
99 </field>
100 </form>
101 `,
102 resId: 1,
103 });
104
105 expect(`img[data-src="${FR_FLAG_URL}"]`).toHaveCount(1, {
106 message: "The view's image is in the DOM",
107 });
108 expect(".o_kanban_record:not(.o_kanban_ghost):not(.o-kanban-button-new)").toHaveCount(1, {
109 message: "There should be one record in the many2many",
110 });
111
112 // Actual flow: click on an element of the m2m to get its form view
113 await click(".o_kanban_record:not(.o_kanban_ghost):not(.o-kanban-button-new)");
114 await animationFrame();
115 expect(".modal").toHaveCount(1, { message: "The modal should have opened" });
116 expect(`img[data-src="${EN_FLAG_URL}"]`).toHaveCount(1, {
117 message: "The dialog's image is in the DOM",
118 });
119 });
120
121 test("image fields in x2many list are loaded correctly", async () => {
122 PartnerType._fields.image = fields.Char();
123 PartnerType._records[0].image = EN_FLAG_URL;
124 Partner._records[0].timmy = [12];
125
126 await mountView({
127 type: "form",
128 resModel: "partner",
129 arch: /* xml */ `
130 <form>
131 <field name="timmy" widget="many2many">
132 <list>
133 <field name="image" widget="image_url"/>
134 </list>
135 </field>
136 </form>
137 `,
138 resId: 1,
139 });
140
141 expect("tr.o_data_row").toHaveCount(1, {
142 message: "There should be one record in the many2many",
143 });
144 expect(`img[data-src="${EN_FLAG_URL}"]`).toHaveCount(1, {
145 message: "The list's image is in the DOM",
146 });
147 });
148
149 test("image url fields in kanban don't stop opening record", async () => {
150 patchWithCleanup(KanbanController.prototype, {
151 openRecord() {
152 expect.step("open record");
153 },
154 });
155
156 await mountView({
157 type: "kanban",
158 resModel: "partner",
159 arch: /* xml */ `
160 <kanban>
161 <templates>
162 <t t-name="card">
163 <field name="foo" widget="image_url"/>
164 </t>
165 </templates>
166 </kanban>
167 `,
168 });
169
170 await click(".o_kanban_record");
171 await animationFrame();
172 expect.verifySteps(["open record"]);
173 });
174
175 test("image fields with empty value", async () => {
176 Partner._records[0].foo = false;
177
178 await mountView({
179 type: "form",
180 resModel: "partner",
181 arch: /* xml */ `
182 <form>
183 <field name="foo" widget="image_url" options="{'size': [90, 90]}"/>
184 </form>
185 `,
186 resId: 1,
187 });
188
189 expect(`div[name="foo"]`).toHaveClass("o_field_image_url", {
190 message: "the widget should have the correct class",
191 });
192 expect(`div[name="foo"] > img`).toHaveCount(0, {
193 message: "the widget should not contain an image",
194 });
195 });
196
197 test("onchange update image fields", async () => {
198 const srcTest = "/my/test/src";
199 Partner._onChanges.name = (record) => {
200 record.foo = srcTest;
201 };
202
203 await mountView({
204 type: "form",
205 resModel: "partner",
206 arch: /* xml */ `
207 <form>
208 <field name="name"/>
209 <field name="foo" widget="image_url" options="{'size': [90, 90]}"/>
210 </form>
211 `,
212 resId: 1,
213 });
214
215 expect(`div[name="foo"] > img`).toHaveAttribute("data-src", FR_FLAG_URL, {
216 message: "the image should have the correct src",
217 });
218
219 await click(`[name="name"] input`);
220 await edit("test", { confirm: "enter" });
221 await runAllTimers();
222 await animationFrame();
223 expect(`div[name="foo"] > img`).toHaveAttribute("data-src", srcTest, {
224 message: "the image should have the onchange src",
225 });
226 });