Debian Codegraph @

addons/web/static/tests/views/widgets/attach_document.test.js

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, manuallyDispatchProgrammaticEvent } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 contains,
6 defineModels,
7 fields,
8 mockService,
9 models,
10 mountView,
11 onRpc,
12 patchWithCleanup,
13 } from "@web/../tests/web_test_helpers";
14
15 import { AttachDocumentWidget } from "@web/views/widgets/attach_document/attach_document";
16
17 class Partner extends models.Model {
18 display_name = fields.Char({ string: "Displayed name" });
19 _records = [
20 {
21 id: 1,
22 display_name: "first record",
23 },
24 ];
25 }
26
27 defineModels([Partner]);
28
29 test("attach document widget calls action with attachment ids", async () => {
30 // FIXME: This ugly hack is needed because the input is not attached in the DOM
31 // The input should be attached to the component and hidden in some way to make
32 // the interaction easier and more natural.
33 let fileInput;
34 patchWithCleanup(AttachDocumentWidget.prototype, {
35 setup() {
36 super.setup();
37 fileInput = this.fileInput;
38 },
39 });
40
41 mockService("http", {
42 post(route, params) {
43 expect.step("post");
44 expect(route).toBe("/web/binary/upload_attachment");
45 expect(params.model).toBe("partner");
46 expect(params.id).toBe(1);
47 return '[{ "id": 5 }, { "id": 2 }]';
48 },
49 });
50
51 onRpc(({ args, kwargs, method, model }) => {
52 expect.step(method);
53 if (method === "my_action") {
54 expect(model).toBe("partner");
55 expect(args).toEqual([1]);
56 expect(kwargs.attachment_ids).toEqual([5, 2]);
57 return true;
58 }
59 if (method === "web_save") {
60 expect(args[1]).toEqual({ display_name: "yop" });
61 }
62 if (method === "web_read") {
63 expect(args[0]).toEqual([1]);
64 }
65 });
66
67 await mountView({
68 type: "form",
69 resModel: "partner",
70 resId: 1,
71 arch: /* xml */ `
72 <form>
73 <widget name="attach_document" action="my_action" string="Attach document"/>
74 <field name="display_name" required="1"/>
75 </form>`,
76 });
77 expect.verifySteps(["get_views", "web_read"]);
78
79 await contains("[name='display_name'] input").edit("yop");
80 await animationFrame();
81 await click(".o_attach_document");
82 await animationFrame();
83 await manuallyDispatchProgrammaticEvent(fileInput, "change");
84 await animationFrame();
85 expect.verifySteps(["web_save", "post", "my_action", "web_read"]);
86 });
87
88 test("attach document widget calls action with attachment ids on a new record", async () => {
89 // FIXME: This ugly hack is needed because the input is not attached in the DOM
90 // The input should be attached to the component and hidden in some way to make
91 // the interaction easier and more natural.
92 let fileInput;
93 patchWithCleanup(AttachDocumentWidget.prototype, {
94 setup() {
95 super.setup();
96 fileInput = this.fileInput;
97 },
98 });
99
100 mockService("http", {
101 post(route, params) {
102 expect.step("post");
103 expect(route).toBe("/web/binary/upload_attachment");
104 expect(params.model).toBe("partner");
105 expect(params.id).toBe(2);
106 return '[{ "id": 5 }, { "id": 2 }]';
107 },
108 });
109
110 onRpc(({ args, kwargs, method, model }) => {
111 expect.step(method);
112 if (method === "my_action") {
113 expect(model).toBe("partner");
114 expect(args).toEqual([2]);
115 expect(kwargs.attachment_ids).toEqual([5, 2]);
116 return true;
117 }
118 if (method === "web_save") {
119 expect(args[1]).toEqual({ display_name: "yop" });
120 }
121 if (method === "web_read") {
122 expect(args[0]).toEqual([2]);
123 }
124 });
125
126 await mountView({
127 type: "form",
128 resModel: "partner",
129 arch: /* xml */ `
130 <form>
131 <widget name="attach_document" action="my_action" string="Attach document"/>
132 <field name="display_name" required="1"/>
133 </form>`,
134 });
135 expect.verifySteps(["get_views", "onchange"]);
136 await contains("[name='display_name'] input").edit("yop");
137 await click(".o_attach_document");
138 await animationFrame();
139 await manuallyDispatchProgrammaticEvent(fileInput, "change");
140 await animationFrame();
141 expect.verifySteps(["web_save", "post", "my_action", "web_read"]);
142 });