Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { setInputFiles } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 clickSave,
6 contains,
7 defineModels,
8 fields,
9 MockServer,
10 mockService,
11 models,
12 mountView,
13 onRpc,
14 } from "@web/../tests/web_test_helpers";
15
16 class Turtle extends models.Model {
17 picture_ids = fields.Many2many({
18 string: "Pictures",
19 relation: "ir.attachment",
20 });
21 _records = [{ id: 1, picture_ids: [17] }];
22 }
23
24 class IrAttachment extends models.Model {
25 _name = "ir.attachment";
26 name = fields.Char();
27 mimetype = fields.Char();
28 _records = [{ id: 17, name: "Marley&Me.jpg", mimetype: "jpg" }];
29 }
30
31 defineModels([Turtle, IrAttachment]);
32
33 test("widget many2many_binary", async () => {
34 expect.assertions(17);
35
36 mockService("http", {
37 post(route, { ufile }) {
38 expect(route).toBe("/web/binary/upload_attachment");
39 expect(ufile[0].name).toBe("fake_file.tiff", {
40 message: "file is correctly uploaded to the server",
41 });
42 const ids = MockServer.env["ir.attachment"].create(
43 ufile.map(({ name }) => ({ name, mimetype: "text/plain" }))
44 );
45 return JSON.stringify(MockServer.env["ir.attachment"].read(ids));
46 },
47 });
48
49 IrAttachment._views.list = '<list string="Pictures"><field name="name"/></list>';
50 onRpc((args) => {
51 if (args.method !== "get_views") {
52 expect.step(args.route);
53 }
54 if (args.method === "web_read" && args.model === "turtle") {
55 expect(args.kwargs.specification).toEqual({
56 display_name: {},
57 picture_ids: {
58 fields: {
59 mimetype: {},
60 name: {},
61 },
62 },
63 });
64 }
65 if (args.method === "web_save" && args.model === "turtle") {
66 expect(args.kwargs.specification).toEqual({
67 display_name: {},
68 picture_ids: {
69 fields: {
70 mimetype: {},
71 name: {},
72 },
73 },
74 });
75 }
76 if (args.method === "web_read" && args.model === "ir.attachment") {
77 expect(args.kwargs.specification).toEqual({
78 mimetype: {},
79 name: {},
80 });
81 }
82 });
83
84 await mountView({
85 type: "form",
86 resModel: "turtle",
87 arch: `
88 <form>
89 <group>
90 <field name="picture_ids" widget="many2many_binary" options="{'accepted_file_extensions': 'image/*'}"/>
91 </group>
92 </form>`,
93 resId: 1,
94 });
95
96 expect("div.o_field_widget .oe_fileupload").toHaveCount(1);
97 expect("div.o_field_widget .oe_fileupload .o_attachments").toHaveCount(1);
98 expect("div.o_field_widget .oe_fileupload .o_attachment .o_attachment_delete").toHaveCount(1);
99 expect("div.o_field_widget .oe_fileupload .o_attach").toHaveCount(1);
100 expect("div.o_field_widget .oe_fileupload .o_attach").toHaveText("Pictures");
101
102 expect("input.o_input_file").toHaveAttribute("accept", "image/*");
103 expect.verifySteps(["/web/dataset/call_kw/turtle/web_read"]);
104
105 // Set and trigger the change of a file for the input
106 await contains(".o_file_input_trigger").click();
107 await setInputFiles(new File(["fake_file"], "fake_file.tiff", { type: "text/plain" }));
108 await animationFrame();
109
110 expect(".o_attachment:nth-child(2) .caption a:eq(0)").toHaveText("fake_file.tiff", {
111 message: 'value of attachment should be "fake_file.tiff"',
112 });
113 expect(".o_attachment:nth-child(2) .caption.small a").toHaveText("TIFF", {
114 message: "file extension should be correct",
115 });
116 expect(".o_attachment:nth-child(2) .o_image.o_hover").toHaveAttribute(
117 "data-mimetype",
118 "text/plain",
119 { message: "preview displays the right mimetype" }
120 );
121
122 // delete the attachment
123 await contains("div.o_field_widget .oe_fileupload .o_attachment .o_attachment_delete").click();
124
125 await clickSave();
126 expect("div.o_field_widget .oe_fileupload .o_attachments").toHaveCount(1);
127 expect.verifySteps([
128 "/web/dataset/call_kw/ir.attachment/web_read",
129 "/web/dataset/call_kw/turtle/web_save",
130 ]);
131 });
132
133 test("widget many2many_binary displays notification on error", async () => {
134 expect.assertions(11);
135
136 mockService("http", {
137 post(route, { ufile }) {
138 expect(route).toBe("/web/binary/upload_attachment");
139 expect([ufile[0].name, ufile[1].name]).toEqual(["good_file.txt", "bad_file.txt"], {
140 message: "files are correctly sent to the server",
141 });
142 const ids = MockServer.env["ir.attachment"].create({
143 name: ufile[0].name,
144 mimetype: "text/plain",
145 });
146 return JSON.stringify([
147 ...MockServer.env["ir.attachment"].read(ids),
148 {
149 name: ufile[1].name,
150 mimetype: "text/plain",
151 error: `Error on file: ${ufile[1].name}`,
152 },
153 ]);
154 },
155 });
156
157 IrAttachment._views.list = '<list string="Pictures"><field name="name"/></list>';
158
159 await mountView({
160 type: "form",
161 resModel: "turtle",
162 arch: `
163 <form>
164 <group>
165 <field name="picture_ids" widget="many2many_binary" options="{'accepted_file_extensions': 'image/*'}"/>
166 </group>
167 </form>`,
168 resId: 1,
169 });
170
171 expect("div.o_field_widget .oe_fileupload").toHaveCount(1);
172 expect("div.o_field_widget .oe_fileupload .o_attachments").toHaveCount(1);
173 expect("div.o_field_widget .oe_fileupload .o_attach").toHaveCount(1);
174 expect("div.o_field_widget .oe_fileupload .o_attachment .o_attachment_delete").toHaveCount(1);
175
176 // Set and trigger the import of 2 files in the input
177 await contains(".o_file_input_trigger").click();
178 await setInputFiles([
179 new File(["good_file"], "good_file.txt", { type: "text/plain" }),
180 new File(["bad_file"], "bad_file.txt", { type: "text/plain" }),
181 ]);
182 await animationFrame();
183
184 expect(".o_attachment:nth-child(2) .caption a:eq(0)").toHaveText("good_file.txt", {
185 message: 'value of attachment should be "good_file.txt"',
186 });
187 expect("div.o_field_widget .oe_fileupload .o_attachments").toHaveCount(1);
188 expect(".o_notification").toHaveCount(1);
189 expect(".o_notification_content").toHaveText("Uploading error. Error on file: bad_file.txt");
190 expect(".o_notification_bar").toHaveClass("bg-danger");
191 });
192
193 test("widget many2many_binary image MIME type preview", async () => {
194 expect.assertions(9);
195
196 const IMAGE_B64 =
197 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z9DwHwAGBQKA3H7sNwAAAABJRU5ErkJggg==";
198 const imageData = Uint8Array.from([...atob(IMAGE_B64)].map((c) => c.charCodeAt(0)));
199
200 mockService("http", {
201 post(route, { ufile }) {
202 expect(route).toBe("/web/binary/upload_attachment");
203 expect(ufile[0].name).toBe("fake_image.png", {
204 message: "file is correctly uploaded to the server",
205 });
206 const ids = MockServer.env["ir.attachment"].create(
207 ufile.map(({ name }) => ({ name, mimetype: "image/png" }))
208 );
209 return JSON.stringify(MockServer.env["ir.attachment"].read(ids));
210 },
211 });
212
213 IrAttachment._views.list = '<list string="Pictures"><field name="name"/></list>';
214
215 await mountView({
216 type: "form",
217 resModel: "turtle",
218 arch: `
219 <form>
220 <group>
221 <field name="picture_ids" widget="many2many_binary" options="{'accepted_file_extensions': 'image/*'}"/>
222 </group>
223 </form>`,
224 resId: 1,
225 });
226
227 expect("div.o_field_widget .oe_fileupload").toHaveCount(1);
228 expect("div.o_field_widget .oe_fileupload .o_attachments").toHaveCount(1);
229 expect("div.o_field_widget .oe_fileupload .o_attach").toHaveCount(1);
230 expect("div.o_field_widget .oe_fileupload .o_attachment .o_attachment_delete").toHaveCount(1);
231
232 // Set and trigger the import of a png image in the input
233 await contains(".o_file_input_trigger").click();
234 await setInputFiles(new File([imageData], "fake_image.png", { type: "image/png" }));
235 await animationFrame();
236
237 expect(".o_attachment:nth-child(2) .caption a:eq(0)").toHaveText("fake_image.png", {
238 message: 'value of attachment should be "fake_image.png"',
239 });
240 expect(".o_attachment:nth-child(2) .caption.small a").toHaveText("PNG", {
241 message: "file extension should be correct",
242 });
243 expect(".o_attachment:nth-child(2) .o_preview_image.o_hover").toHaveAttribute(
244 "src",
245 `data:image/png;base64,${IMAGE_B64}`,
246 { message: "preview should display the image preview" }
247 );
248 });