Debian Codegraph @

addons/spreadsheet/static/tests/charts/ui/link_chart_figure.test.js

raw
1 import { click } from "@odoo/hoot-dom";
2 import { animationFrame } from "@odoo/hoot-mock";
3 import { expect, test, beforeEach } from "@odoo/hoot";
4 import { getBasicData, defineSpreadsheetModels } from "@spreadsheet/../tests/helpers/data";
5 import { createBasicChart } from "@spreadsheet/../tests/helpers/commands";
6 import { mountSpreadsheet } from "@spreadsheet/../tests/helpers/ui";
7 import { createModelWithDataSource } from "@spreadsheet/../tests/helpers/model";
8 import { mockService, serverState } from "@web/../tests/web_test_helpers";
9
10 defineSpreadsheetModels();
11
12 /**
13 * @typedef {import("@spreadsheet/../tests/helpers/data").ServerData} ServerData
14 */
15
16 const chartId = "uuid1";
17 let serverData = /** @type {ServerData} */ ({});
18
19 /**
20 * The chart menu is hidden by default, and visible on :hover, but this property
21 * can't be triggered programmatically, so we artificially make it visible to be
22 * able to interact with it.
23 */
24 async function showChartMenu(fixture) {
25 const chartMenu = fixture.querySelector(".o-figure-menu");
26 chartMenu.style.display = "flex";
27 await animationFrame();
28 }
29
30 /** Click on external link of the first chart found in the page*/
31 async function clickChartExternalLink(fixture) {
32 await showChartMenu(fixture);
33 const chartMenuItem = fixture.querySelector(".o-figure-menu-item.o-chart-external-link");
34 await click(chartMenuItem);
35 await animationFrame();
36 }
37
38 function mockActionService(doActionStep) {
39 const fakeActionService = {
40 doAction: async (actionRequest, options = {}) => {
41 if (actionRequest === "menuAction2") {
42 expect.step(doActionStep);
43 }
44 },
45 };
46 mockService("action", fakeActionService);
47 }
48
49 beforeEach(() => {
50 serverData = {};
51 serverData.menus = {
52 1: {
53 id: 1,
54 name: "test menu 1",
55 xmlid: "spreadsheet.test.menu",
56 appID: 1,
57 actionID: "menuAction",
58 },
59 2: {
60 id: 2,
61 name: "test menu 2",
62 xmlid: "spreadsheet.test.menu2",
63 appID: 1,
64 actionID: "menuAction2",
65 },
66 3: {
67 id: 3,
68 name: "test menu 2",
69 xmlid: "spreadsheet.test.menu_without_action",
70 appID: 1,
71 },
72 };
73 serverData.actions = {
74 menuAction: {
75 id: 99,
76 xml_id: "ir.ui.menu",
77 name: "menuAction",
78 res_model: "ir.ui.menu",
79 type: "ir.actions.act_window",
80 views: [[false, "list"]],
81 },
82 menuAction2: {
83 id: 100,
84 xml_id: "ir.ui.menu",
85 name: "menuAction2",
86 res_model: "ir.ui.menu",
87 type: "ir.actions.act_window",
88 views: [[false, "list"]],
89 },
90 };
91 serverData.models = {
92 ...getBasicData(),
93 "ir.ui.menu": {
94 records: [
95 { id: 1, name: "test menu 1", action: "action1", group_ids: [10] },
96 { id: 2, name: "test menu 2", action: "action2", group_ids: [10] },
97 ],
98 },
99 "res.group": { records: [{ id: 10, name: "test group" }] },
100 "res.users": {
101 records: [{ id: 1, active: true, partner_id: serverState.partnerId, name: "Raoul" }],
102 },
103 "ir.actions": { records: [{ id: 1 }] },
104 };
105 serverState.userId = 1;
106 });
107
108 test("icon external link isn't on the chart when its not linked to an odoo menu", async function () {
109 const { model } = await createModelWithDataSource({
110 serverData,
111 });
112 const fixture = await mountSpreadsheet(model);
113 createBasicChart(model, chartId);
114 await animationFrame();
115 const odooMenu = model.getters.getChartOdooMenu(chartId);
116 expect(odooMenu).toBe(undefined, { message: "No menu linked with the chart" });
117
118 const externalRefIcon = fixture.querySelector(".o-chart-external-link");
119 expect(externalRefIcon).toBe(null);
120 });
121
122 test("icon external link is on the chart when its linked to an odoo menu", async function () {
123 const { model } = await createModelWithDataSource({
124 serverData,
125 });
126 await mountSpreadsheet(model);
127 createBasicChart(model, chartId);
128 model.dispatch("LINK_ODOO_MENU_TO_CHART", {
129 chartId,
130 odooMenuId: 1,
131 });
132
133 const chartMenu = model.getters.getChartOdooMenu(chartId);
134 expect(chartMenu.id).toBe(1, { message: "Odoo menu is linked to chart" });
135 await animationFrame();
136 expect(".o-chart-external-link").toHaveCount(1);
137 });
138
139 test("icon external link is not on the chart when its linked to a wrong odoo menu", async function () {
140 const { model } = await createModelWithDataSource({
141 serverData,
142 });
143 await mountSpreadsheet(model);
144 createBasicChart(model, chartId);
145 model.dispatch("LINK_ODOO_MENU_TO_CHART", {
146 chartId,
147 odooMenuId: "menu which does not exist",
148 });
149 const chartMenu = model.getters.getChartOdooMenu(chartId);
150 expect(chartMenu).toBe(undefined, { message: "cannot get a wrong menu" });
151 await animationFrame();
152 expect(".o-chart-external-link").toHaveCount(0);
153 });
154
155 test("icon external link isn't on the chart in dashboard mode", async function () {
156 const { model } = await createModelWithDataSource({
157 serverData,
158 });
159 await mountSpreadsheet(model);
160 createBasicChart(model, chartId);
161 model.dispatch("LINK_ODOO_MENU_TO_CHART", {
162 chartId,
163 odooMenuId: 1,
164 });
165 const chartMenu = model.getters.getChartOdooMenu(chartId);
166 expect(chartMenu.id).toBe(1, { message: "Odoo menu is linked to chart" });
167 model.updateMode("dashboard");
168 await animationFrame();
169 expect(".o-chart-external-link").toHaveCount(0, { message: "No link icon in dashboard" });
170 });
171
172 test("click on icon external link on chart redirect to the odoo menu", async function () {
173 const doActionStep = "doAction";
174 mockActionService(doActionStep);
175
176 const { model } = await createModelWithDataSource({
177 serverData,
178 });
179 const fixture = await mountSpreadsheet(model);
180
181 createBasicChart(model, chartId);
182 model.dispatch("LINK_ODOO_MENU_TO_CHART", {
183 chartId,
184 odooMenuId: 2,
185 });
186 const chartMenu = model.getters.getChartOdooMenu(chartId);
187 expect(chartMenu.id).toBe(2, { message: "Odoo menu is linked to chart" });
188 await animationFrame();
189
190 await clickChartExternalLink(fixture);
191
192 expect.verifySteps([doActionStep]);
193 });
194
195 test("can use menus xmlIds instead of menu ids", async function () {
196 mockActionService("doAction");
197 const { model } = await createModelWithDataSource({
198 serverData,
199 });
200 const fixture = await mountSpreadsheet(model);
201
202 createBasicChart(model, chartId);
203 model.dispatch("LINK_ODOO_MENU_TO_CHART", {
204 chartId,
205 odooMenuId: "spreadsheet.test.menu2",
206 });
207 await animationFrame();
208
209 await clickChartExternalLink(fixture);
210
211 expect.verifySteps(["doAction"]);
212 });
213
214 test("Trying to open a menu without an action sends a notification to the user", async function () {
215 mockActionService("doAction");
216 mockService("notification", {
217 add: (message) => {
218 expect.step(message);
219 return () => {};
220 },
221 });
222
223 const { model } = await createModelWithDataSource({
224 serverData,
225 });
226 const fixture = await mountSpreadsheet(model);
227
228 createBasicChart(model, chartId);
229 model.dispatch("LINK_ODOO_MENU_TO_CHART", {
230 chartId,
231 odooMenuId: "spreadsheet.test.menu_without_action",
232 });
233 await animationFrame();
234
235 await clickChartExternalLink(fixture);
236
237 const expectedNotificationMessage =
238 "The menu linked to this chart doesn't have an corresponding action. Please link the chart to another menu.";
239 // Notification was send and doAction wasn't called
240 expect.verifySteps([expectedNotificationMessage]);
241 });