Debian Codegraph @

addons/spreadsheet/static/tests/charts/model/odoo_chart_plugin.test.js

raw
1 import { animationFrame } from "@odoo/hoot-mock";
2 import { describe, expect, test } from "@odoo/hoot";
3
4 import { OdooBarChart } from "@spreadsheet/chart/odoo_chart/odoo_bar_chart";
5 import { OdooChart } from "@spreadsheet/chart/odoo_chart/odoo_chart";
6 import { OdooLineChart } from "@spreadsheet/chart/odoo_chart/odoo_line_chart";
7 import { ChartDataSource } from "@spreadsheet/chart/data_source/chart_data_source";
8
9 import {
10 createSpreadsheetWithChart,
11 insertChartInSpreadsheet,
12 } from "@spreadsheet/../tests/helpers/chart";
13 import { insertListInSpreadsheet } from "@spreadsheet/../tests/helpers/list";
14 import { createModelWithDataSource } from "@spreadsheet/../tests/helpers/model";
15 import { addGlobalFilter, updateChart } from "@spreadsheet/../tests/helpers/commands";
16 import { THIS_YEAR_GLOBAL_FILTER } from "@spreadsheet/../tests/helpers/global_filter";
17 import {
18 mockService,
19 makeServerError,
20 fields,
21 onRpc,
22 patchWithCleanup,
23 } from "@web/../tests/web_test_helpers";
24 import * as spreadsheet from "@odoo/o-spreadsheet";
25
26 import { user } from "@web/core/user";
27 import {
28 getBasicServerData,
29 defineSpreadsheetActions,
30 defineSpreadsheetModels,
31 Partner,
32 } from "@spreadsheet/../tests/helpers/data";
33 import { waitForDataLoaded } from "@spreadsheet/helpers/model";
34 import { setGlobalFilterValue } from "../../helpers/commands";
35
36 const { toZone } = spreadsheet.helpers;
37
38 const cumulativeDateServerData = getBasicServerData();
39 cumulativeDateServerData.models.partner.records = [
40 { date: "2020-01-01", probability: 10 },
41 { date: "2021-01-01", probability: 2 },
42 { date: "2022-01-01", probability: 3 },
43 { date: "2022-03-01", probability: 4 },
44 { date: "2022-06-01", probability: 5 },
45 ];
46
47 const cumulativeChartDefinition = {
48 type: "odoo_line",
49 metaData: {
50 groupBy: ["date"],
51 measure: "probability",
52 order: null,
53 resModel: "partner",
54 },
55 searchParams: {
56 comparison: null,
57 context: {},
58 domain: [
59 ["date", ">=", "2022-01-01"],
60 ["date", "<=", "2022-12-31"],
61 ],
62 groupBy: [],
63 orderBy: [],
64 },
65 cumulative: true,
66 title: { text: "Partners" },
67 dataSourceId: "42",
68 id: "42",
69 };
70
71 const action = {
72 domain: [
73 "&",
74 "&",
75 ["date", ">=", "2022-01-01"],
76 ["date", "<=", "2022-12-31"],
77 "&",
78 ["date", ">=", "2022-01-01"],
79 ["date", "<", "2022-02-01"],
80 ],
81 name: "January 2022 / Probability",
82 res_model: "partner",
83 target: "current",
84 type: "ir.actions.act_window",
85 views: [
86 [false, "list"],
87 [false, "form"],
88 ],
89 };
90
91 const fakeActionService = {
92 doAction: async (request, options = {}) => {
93 if (request.type === "ir.actions.act_window") {
94 expect.step("do-action");
95 expect(request).toEqual(action);
96 }
97 },
98 loadAction(actionRequest) {
99 expect.step("load-action");
100 expect(actionRequest).toBe("test.my_action");
101 return action;
102 },
103 };
104
105 describe.current.tags("headless");
106 defineSpreadsheetModels();
107 defineSpreadsheetActions();
108
109 test("Can add an Odoo Bar chart", async () => {
110 const { model } = await createSpreadsheetWithChart({ type: "odoo_bar" });
111 const sheetId = model.getters.getActiveSheetId();
112 expect(model.getters.getChartIds(sheetId).length).toBe(1);
113 const chartId = model.getters.getChartIds(sheetId)[0];
114 const chart = model.getters.getChart(chartId);
115 expect(chart instanceof OdooBarChart).toBe(true);
116 expect(chart.getDefinitionForExcel()).toBe(undefined);
117 expect(model.getters.getChartRuntime(chartId).chartJsConfig.type).toBe("bar");
118 });
119
120 test("Can add an Odoo Line chart", async () => {
121 const { model } = await createSpreadsheetWithChart({ type: "odoo_line" });
122 const sheetId = model.getters.getActiveSheetId();
123 expect(model.getters.getChartIds(sheetId).length).toBe(1);
124 const chartId = model.getters.getChartIds(sheetId)[0];
125 const chart = model.getters.getChart(chartId);
126 expect(chart instanceof OdooLineChart).toBe(true);
127 expect(chart.getDefinitionForExcel()).toBe(undefined);
128 expect(model.getters.getChartRuntime(chartId).chartJsConfig.type).toBe("line");
129 });
130
131 test("Can add an Odoo Pie chart", async () => {
132 const { model } = await createSpreadsheetWithChart({ type: "odoo_pie" });
133 const sheetId = model.getters.getActiveSheetId();
134 expect(model.getters.getChartIds(sheetId).length).toBe(1);
135 const chartId = model.getters.getChartIds(sheetId)[0];
136 const chart = model.getters.getChart(chartId);
137 expect(chart instanceof OdooChart).toBe(true);
138 expect(chart.getDefinitionForExcel()).toBe(undefined);
139 expect(model.getters.getChartRuntime(chartId).chartJsConfig.type).toBe("pie");
140 });
141
142 test("A data source is added after a chart creation", async () => {
143 const { model } = await createSpreadsheetWithChart();
144 const sheetId = model.getters.getActiveSheetId();
145 const chartId = model.getters.getChartIds(sheetId)[0];
146 expect(model.getters.getChartDataSource(chartId)).not.toBe(undefined);
147 });
148
149 test("Odoo bar chart runtime loads the data", async () => {
150 const { model } = await createSpreadsheetWithChart({
151 type: "odoo_bar",
152 mockRPC: async function (route, args) {
153 if (args.method === "formatted_read_group") {
154 expect.step("formatted_read_group");
155 }
156 },
157 });
158 const sheetId = model.getters.getActiveSheetId();
159 const chartId = model.getters.getChartIds(sheetId)[0];
160 // it should not be loaded eagerly
161 expect.verifySteps([]);
162 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data).toEqual({
163 datasets: [],
164 labels: [],
165 });
166 await animationFrame();
167 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data).toEqual({
168 datasets: [
169 {
170 backgroundColor: "#4EA7F2",
171 borderColor: "#FFFFFF",
172 borderWidth: 1,
173 data: [1, 3],
174 label: "Count",
175 xAxisID: "x",
176 yAxisID: "y",
177 hidden: undefined,
178 },
179 ],
180 labels: ["false", "true"],
181 });
182 // it should have loaded the data
183 expect.verifySteps(["formatted_read_group"]);
184 });
185
186 test("Odoo pie chart runtime loads the data", async () => {
187 const { model } = await createSpreadsheetWithChart({
188 type: "odoo_pie",
189 mockRPC: async function (route, args) {
190 if (args.method === "formatted_read_group") {
191 expect.step("formatted_read_group");
192 }
193 },
194 });
195 const sheetId = model.getters.getActiveSheetId();
196 const chartId = model.getters.getChartIds(sheetId)[0];
197 // it should not be loaded eagerly
198 expect.verifySteps([]);
199 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data).toEqual({
200 datasets: [],
201 labels: [],
202 });
203 await animationFrame();
204 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data).toEqual({
205 datasets: [
206 {
207 backgroundColor: ["#4EA7F2", "#EA6175", "#43C5B1"],
208 borderColor: "#FFFFFF",
209 data: [1, 3],
210 hoverOffset: 10,
211 label: "",
212 },
213 ],
214 labels: ["false", "true"],
215 });
216 // it should have loaded the data
217 expect.verifySteps(["formatted_read_group"]);
218 });
219
220 test("Odoo line chart runtime loads the data", async () => {
221 const { model } = await createSpreadsheetWithChart({
222 type: "odoo_line",
223 mockRPC: async function (route, args) {
224 if (args.method === "formatted_read_group") {
225 expect.step("formatted_read_group");
226 }
227 },
228 });
229 const sheetId = model.getters.getActiveSheetId();
230 const chartId = model.getters.getChartIds(sheetId)[0];
231 // it should not be loaded eagerly
232 expect.verifySteps([]);
233 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data).toEqual({
234 datasets: [],
235 labels: [],
236 });
237 await animationFrame();
238 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data).toEqual({
239 datasets: [
240 {
241 backgroundColor: "#4EA7F2",
242 borderColor: "#4EA7F2",
243 data: [1, 3],
244 label: "Count",
245 tension: 0,
246 fill: false,
247 pointBackgroundColor: "#4EA7F2",
248 pointRadius: 3,
249 yAxisID: "y",
250 hidden: undefined,
251 },
252 ],
253 labels: ["false", "true"],
254 });
255 // it should have loaded the data
256 expect.verifySteps(["formatted_read_group"]);
257 });
258
259 test("Area charts are supported", async () => {
260 const { model } = await createSpreadsheetWithChart({ type: "odoo_line" });
261 await waitForDataLoaded(model);
262 const sheetId = model.getters.getActiveSheetId();
263 const chartId = model.getters.getChartIds(sheetId)[0];
264 const definition = model.getters.getChartDefinition(chartId);
265 model.dispatch("UPDATE_CHART", {
266 definition: { ...definition, fillArea: true, stacked: false },
267 figureId: model.getters.getFigureIdFromChartId(chartId),
268 chartId,
269 sheetId,
270 });
271 let runtime = model.getters.getChartRuntime(chartId).chartJsConfig;
272 expect(runtime.options.scales.x.stacked).toBe(undefined);
273 expect(runtime.options.scales.y.stacked).toBe(false);
274 expect(runtime.data.datasets[0].fill).toBe("origin");
275 model.dispatch("UPDATE_CHART", {
276 definition: { ...definition, fillArea: true, stacked: true },
277 figureId: model.getters.getFigureIdFromChartId(chartId),
278 chartId,
279 sheetId,
280 });
281 runtime = model.getters.getChartRuntime(chartId).chartJsConfig;
282 expect(runtime.options.scales.x.stacked).toBe(undefined);
283 expect(runtime.options.scales.y.stacked).toBe(true);
284 expect(runtime.data.datasets[0].fill).toBe("origin");
285 });
286
287 test("Data reloaded strictly upon domain update", async () => {
288 const { model } = await createSpreadsheetWithChart({
289 type: "odoo_line",
290 mockRPC: async function (route, args) {
291 if (args.method === "formatted_read_group") {
292 expect.step("formatted_read_group");
293 }
294 },
295 });
296 const sheetId = model.getters.getActiveSheetId();
297 const chartId = model.getters.getChartIds(sheetId)[0];
298 const definition = model.getters.getChartDefinition(chartId);
299
300 // force runtime computation
301 model.getters.getChartRuntime(chartId);
302 await animationFrame();
303 // it should have loaded the data
304 expect.verifySteps(["formatted_read_group"]);
305
306 model.dispatch("UPDATE_CHART", {
307 definition: {
308 ...definition,
309 searchParams: { ...definition.searchParams, domain: [["1", "=", "1"]] },
310 },
311 figureId: model.getters.getFigureIdFromChartId(chartId),
312 chartId,
313 sheetId,
314 });
315 // force runtime computation
316 model.getters.getChartRuntime(chartId);
317 await animationFrame();
318 // it should have loaded the data with a new domain
319 expect.verifySteps(["formatted_read_group"]);
320
321 const newDefinition = model.getters.getChartDefinition(chartId);
322 model.dispatch("UPDATE_CHART", {
323 definition: {
324 ...newDefinition,
325 background: "#00FF00",
326 },
327 chartId,
328 figureId: model.getters.getFigureIdFromChartId(chartId),
329 sheetId,
330 });
331 // force runtime computation
332 model.getters.getChartRuntime(chartId);
333 await animationFrame();
334 // it should have not have loaded the data since the domain was unchanged
335 expect.verifySteps([]);
336 });
337
338 test("Data reloaded upon domain update for charts other than pie/bar/line", async () => {
339 const { model } = await createSpreadsheetWithChart({
340 type: "odoo_line",
341 mockRPC: async function (route, args) {
342 if (args.method === "formatted_read_group") {
343 expect.step("formatted_read_group");
344 }
345 },
346 });
347 const sheetId = model.getters.getActiveSheetId();
348 const chartId = model.getters.getChartIds(sheetId)[0];
349
350 await waitForDataLoaded(model);
351 expect.verifySteps(["formatted_read_group"]); // Data loaded
352
353 updateChart(model, chartId, { type: "odoo_pie" });
354 await waitForDataLoaded(model);
355 expect.verifySteps([]); // Chart type changed
356
357 const newDefinition = model.getters.getChartDefinition(chartId);
358 updateChart(model, chartId, {
359 searchParams: { ...newDefinition.searchParams, domain: [["1", "=", "1"]] },
360 });
361 await waitForDataLoaded(model);
362 expect.verifySteps(["formatted_read_group"]); // Data re-loaded on domain update
363 });
364
365 test("Updating the domain keeps the global filters domain", async () => {
366 let lastReadGroupDomain = undefined;
367 const { model } = await createSpreadsheetWithChart({
368 type: "odoo_line",
369 mockRPC: async function (route, args) {
370 if (args.method === "formatted_read_group") {
371 expect.step("formatted_read_group");
372 lastReadGroupDomain = args.kwargs.domain;
373 }
374 },
375 });
376 const sheetId = model.getters.getActiveSheetId();
377 const chartId = model.getters.getChartIds(model.getters.getActiveSheetId())[0];
378 const definition = model.getters.getChartDefinition(chartId);
379 const filter = {
380 id: "42",
381 type: "relation",
382 label: "filter",
383 modelName: "product",
384 defaultValue: { operator: "in", ids: [41] },
385 };
386 await addGlobalFilter(model, filter, {
387 chart: { [chartId]: { chain: "product", type: "many2one" } },
388 });
389
390 model.getters.getChartRuntime(chartId); // force runtime computation
391 await waitForDataLoaded(model);
392 expect.verifySteps(["formatted_read_group"]);
393 expect(lastReadGroupDomain).toEqual([["product", "in", [41]]]);
394
395 const updatedDefinition = {
396 ...definition,
397 searchParams: { ...definition.searchParams, domain: [["1", "=", "1"]] },
398 };
399 model.dispatch("UPDATE_CHART", {
400 definition: updatedDefinition,
401 chartId,
402 figureId: model.getters.getFigureIdFromChartId(chartId),
403 sheetId,
404 });
405
406 model.getters.getChartRuntime(chartId); // force runtime computation
407 await waitForDataLoaded(model);
408 expect.verifySteps(["formatted_read_group"]);
409 expect(lastReadGroupDomain).toEqual(["&", ["1", "=", "1"], ["product", "in", [41]]]);
410 });
411
412 test("Can import/export an Odoo chart", async () => {
413 const { model } = await createModelWithDataSource();
414 insertChartInSpreadsheet(model, "odoo_line");
415 const data = model.exportData();
416 const figures = data.sheets[0].figures;
417 expect(figures.length).toBe(1);
418 const figure = figures[0];
419 expect(figure.tag).toBe("chart");
420 expect(figure.data.type).toBe("odoo_line");
421 const { model: m1 } = await createModelWithDataSource({ spreadsheetData: data });
422 const sheetId = m1.getters.getActiveSheetId();
423 expect(m1.getters.getChartIds(sheetId).length).toBe(1);
424 const chartId = m1.getters.getChartIds(sheetId)[0];
425 expect(model.getters.getChartDataSource(chartId)).not.toBe(undefined);
426 expect(m1.getters.getChartRuntime(chartId).chartJsConfig.type).toBe("line");
427 });
428
429 test("can import (export) contextual domain", async function () {
430 const chartId = "1";
431 const uid = user.userId;
432 const spreadsheetData = {
433 sheets: [
434 {
435 figures: [
436 {
437 id: chartId,
438 x: 10,
439 y: 10,
440 width: 536,
441 height: 335,
442 tag: "chart",
443 data: {
444 chartId,
445 type: "odoo_line",
446 title: { text: "Partners" },
447 legendPosition: "top",
448 searchParams: {
449 domain: '[("foo", "=", uid)]',
450 groupBy: [],
451 orderBy: [],
452 },
453 metaData: {
454 groupBy: ["foo"],
455 measure: "__count",
456 resModel: "partner",
457 },
458 },
459 },
460 ],
461 },
462 ],
463 };
464 const { model } = await createModelWithDataSource({
465 spreadsheetData,
466 mockRPC: function (route, args) {
467 if (args.method === "formatted_read_group") {
468 expect(args.kwargs.domain).toEqual([["foo", "=", uid]]);
469 expect.step("formatted_read_group");
470 }
471 },
472 });
473 model.getters.getChartRuntime(chartId).chartJsConfig.data; // force loading the chart data
474 await animationFrame();
475 expect(model.exportData().sheets[0].figures[0].data.searchParams.domain).toBe(
476 '[("foo", "=", uid)]',
477 { message: "the domain is exported with the dynamic parts" }
478 );
479 expect.verifySteps(["formatted_read_group"]);
480 });
481
482 test("Can undo/redo an Odoo chart creation", async () => {
483 const { model } = await createModelWithDataSource();
484 insertChartInSpreadsheet(model, "odoo_line");
485 const sheetId = model.getters.getActiveSheetId();
486 const chartId = model.getters.getChartIds(sheetId)[0];
487 expect(model.getters.getChartDataSource(chartId)).not.toBe(undefined);
488 model.dispatch("REQUEST_UNDO");
489 expect(model.getters.getChartIds(sheetId).length).toBe(0);
490 model.dispatch("REQUEST_REDO");
491 expect(model.getters.getChartDataSource(chartId)).not.toBe(undefined);
492 expect(model.getters.getChartIds(sheetId).length).toBe(1);
493 });
494
495 test("charts with no legend", async () => {
496 const { model } = await createSpreadsheetWithChart({ type: "odoo_pie" });
497 insertChartInSpreadsheet(model, "odoo_bar");
498 insertChartInSpreadsheet(model, "odoo_line");
499 const sheetId = model.getters.getActiveSheetId();
500 const [pieChartId, barChartId, lineChartId] = model.getters.getChartIds(sheetId);
501 const pie = model.getters.getChartDefinition(pieChartId);
502 const bar = model.getters.getChartDefinition(barChartId);
503 const line = model.getters.getChartDefinition(lineChartId);
504 expect(
505 model.getters.getChartRuntime(pieChartId).chartJsConfig.options.plugins.legend.display
506 ).toBe(true);
507 expect(
508 model.getters.getChartRuntime(barChartId).chartJsConfig.options.plugins.legend.display
509 ).toBe(true);
510 expect(
511 model.getters.getChartRuntime(lineChartId).chartJsConfig.options.plugins.legend.display
512 ).toBe(true);
513 model.dispatch("UPDATE_CHART", {
514 definition: {
515 ...pie,
516 legendPosition: "none",
517 },
518 chartId: pieChartId,
519 figureId: model.getters.getFigureIdFromChartId(pieChartId),
520 sheetId,
521 });
522 model.dispatch("UPDATE_CHART", {
523 definition: {
524 ...bar,
525 legendPosition: "none",
526 },
527 chartId: barChartId,
528 figureId: model.getters.getFigureIdFromChartId(barChartId),
529 sheetId,
530 });
531 model.dispatch("UPDATE_CHART", {
532 definition: {
533 ...line,
534 legendPosition: "none",
535 },
536 chartId: lineChartId,
537 figureId: model.getters.getFigureIdFromChartId(lineChartId),
538 sheetId,
539 });
540 expect(
541 model.getters.getChartRuntime(pieChartId).chartJsConfig.options.plugins.legend.display
542 ).toBe(false);
543 expect(
544 model.getters.getChartRuntime(barChartId).chartJsConfig.options.plugins.legend.display
545 ).toBe(false);
546 expect(
547 model.getters.getChartRuntime(lineChartId).chartJsConfig.options.plugins.legend.display
548 ).toBe(false);
549 });
550
551 test("Bar chart with stacked attribute is supported", async () => {
552 const { model } = await createSpreadsheetWithChart({ type: "odoo_bar" });
553 const sheetId = model.getters.getActiveSheetId();
554 const chartId = model.getters.getChartIds(sheetId)[0];
555 const definition = model.getters.getChartDefinition(chartId);
556 model.dispatch("UPDATE_CHART", {
557 definition: {
558 ...definition,
559 stacked: true,
560 },
561 chartId,
562 figureId: model.getters.getFigureIdFromChartId(chartId),
563 sheetId,
564 });
565 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.x.stacked).toBe(
566 true
567 );
568 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.y.stacked).toBe(
569 true
570 );
571 model.dispatch("UPDATE_CHART", {
572 definition: {
573 ...definition,
574 stacked: false,
575 },
576 chartId,
577 figureId: model.getters.getFigureIdFromChartId(chartId),
578 sheetId,
579 });
580 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.x.stacked).toBe(
581 false
582 );
583 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.y.stacked).toBe(
584 false
585 );
586 });
587
588 test("Can copy/paste Odoo chart", async () => {
589 const { model } = await createSpreadsheetWithChart({ type: "odoo_pie" });
590 const sheetId = model.getters.getActiveSheetId();
591 const chartId = model.getters.getChartIds(sheetId)[0];
592 model.dispatch("SELECT_FIGURE", { figureId: model.getters.getFigureIdFromChartId(chartId) });
593 model.dispatch("COPY");
594 model.dispatch("PASTE", { target: [toZone("A1")] });
595 const chartIds = model.getters.getChartIds(sheetId);
596 expect(chartIds.length).toBe(2);
597 expect(model.getters.getChart(chartIds[1]) instanceof OdooChart).toBe(true);
598 expect(JSON.stringify(model.getters.getChartRuntime(chartIds[1]))).toBe(
599 JSON.stringify(model.getters.getChartRuntime(chartId))
600 );
601
602 expect(model.getters.getChart(chartId).dataSource).not.toBe(
603 model.getters.getChart(chartIds[1]).dataSource,
604 { message: "The datasource is also duplicated" }
605 );
606 });
607
608 test("Can cut/paste Odoo chart", async () => {
609 const { model } = await createSpreadsheetWithChart({ type: "odoo_pie" });
610 const sheetId = model.getters.getActiveSheetId();
611 const chartId = model.getters.getChartIds(sheetId)[0];
612 const chartRuntime = model.getters.getChartRuntime(chartId);
613 model.dispatch("SELECT_FIGURE", { figureId: model.getters.getFigureIdFromChartId(chartId) });
614 model.dispatch("CUT");
615 model.dispatch("PASTE", { target: [toZone("A1")] });
616 const chartIds = model.getters.getChartIds(sheetId);
617 expect(chartIds.length).toBe(1);
618 expect(chartIds[0]).not.toBe(chartId);
619 expect(model.getters.getChart(chartIds[0]) instanceof OdooChart).toBe(true);
620 expect(JSON.stringify(model.getters.getChartRuntime(chartIds[0]))).toBe(
621 JSON.stringify(chartRuntime)
622 );
623 });
624
625 test("Duplicating a sheet correctly duplicates Odoo chart", async () => {
626 const { model } = await createSpreadsheetWithChart({ type: "odoo_bar" });
627 const sheetId = model.getters.getActiveSheetId();
628 const secondSheetId = "secondSheetId";
629 const chartId = model.getters.getChartIds(sheetId)[0];
630 model.dispatch("DUPLICATE_SHEET", {
631 sheetId,
632 sheetIdTo: secondSheetId,
633 sheetNameTo: "Next name",
634 });
635 const chartIds = model.getters.getChartIds(secondSheetId);
636 expect(chartIds.length).toBe(1);
637 expect(model.getters.getChart(chartIds[0]) instanceof OdooChart).toBe(true);
638 expect(JSON.stringify(model.getters.getChartRuntime(chartIds[0]))).toBe(
639 JSON.stringify(model.getters.getChartRuntime(chartId))
640 );
641
642 expect(model.getters.getChart(chartId).dataSource).not.toBe(
643 model.getters.getChart(chartIds[0]).dataSource,
644 { message: "The datasource is also duplicated" }
645 );
646 });
647
648 test("Line chart with stacked attribute is supported", async () => {
649 const { model } = await createSpreadsheetWithChart({ type: "odoo_line" });
650 const sheetId = model.getters.getActiveSheetId();
651 const chartId = model.getters.getChartIds(sheetId)[0];
652 const definition = model.getters.getChartDefinition(chartId);
653 model.dispatch("UPDATE_CHART", {
654 definition: {
655 ...definition,
656 stacked: true,
657 },
658 chartId,
659 figureId: model.getters.getFigureIdFromChartId(chartId),
660 sheetId,
661 });
662 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.x.stacked).toBe(
663 undefined
664 );
665 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.y.stacked).toBe(
666 true
667 );
668 model.dispatch("UPDATE_CHART", {
669 definition: {
670 ...definition,
671 stacked: false,
672 },
673 chartId,
674 figureId: model.getters.getFigureIdFromChartId(chartId),
675 sheetId,
676 });
677 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.x.stacked).toBe(
678 undefined
679 );
680 expect(model.getters.getChartRuntime(chartId).chartJsConfig.options.scales.y.stacked).toBe(
681 false
682 );
683 });
684
685 test("Load odoo chart spreadsheet with models that cannot be accessed", async function () {
686 let hasAccessRights = true;
687 const { model } = await createSpreadsheetWithChart({
688 mockRPC: async function (route, args) {
689 if (
690 args.model === "partner" &&
691 args.method === "formatted_read_group" &&
692 !hasAccessRights
693 ) {
694 throw makeServerError({ description: "ya done!" });
695 }
696 },
697 });
698 const chartId = model.getters.getChartIds(model.getters.getActiveSheetId())[0];
699 const chartDataSource = model.getters.getChartDataSource(chartId);
700 await waitForDataLoaded(model);
701 const data = chartDataSource.getData();
702 expect(data.datasets.length).toBe(1);
703 expect(data.labels.length).toBe(2);
704
705 hasAccessRights = false;
706 chartDataSource.load({ reload: true });
707 await waitForDataLoaded(model);
708 expect(chartDataSource.getData()).toEqual({ datasets: [], labels: [] });
709 });
710
711 test("Line chart to support cumulative data", async () => {
712 const { model } = await createSpreadsheetWithChart({ type: "odoo_line" });
713 const sheetId = model.getters.getActiveSheetId();
714 const chartId = model.getters.getChartIds(sheetId)[0];
715 const definition = model.getters.getChartDefinition(chartId);
716 await waitForDataLoaded(model);
717 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data.datasets[0].data).toEqual([
718 1, 3,
719 ]);
720 model.dispatch("UPDATE_CHART", {
721 definition: {
722 ...definition,
723 cumulative: true,
724 },
725 chartId,
726 figureId: model.getters.getFigureIdFromChartId(chartId),
727 sheetId,
728 });
729 await waitForDataLoaded(model);
730 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data.datasets[0].data).toEqual([
731 1, 4,
732 ]);
733 model.dispatch("UPDATE_CHART", {
734 definition: {
735 ...definition,
736 cumulative: false,
737 },
738 chartId,
739 figureId: model.getters.getFigureIdFromChartId(chartId),
740 sheetId,
741 });
742 await waitForDataLoaded(model);
743 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data.datasets[0].data).toEqual([
744 1, 3,
745 ]);
746 });
747
748 test("cumulative line chart with past data before domain period without cumulated start", async () => {
749 const { model } = await createSpreadsheetWithChart({
750 type: "odoo_line",
751 serverData: cumulativeDateServerData,
752 definition: {
753 ...cumulativeChartDefinition,
754 },
755 });
756 const sheetId = model.getters.getActiveSheetId();
757 const chartId = model.getters.getChartIds(sheetId)[0];
758 await waitForDataLoaded(model);
759 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data.datasets[0].data).toEqual([
760 3, 7, 12,
761 ]);
762 const figure = model.exportData().sheets[0].figures[0];
763 expect(figure.data.cumulative).toBe(true);
764 expect(figure.data.cumulatedStart).toBe(undefined);
765 });
766
767 test("cumulative line chart with past data before domain period with cumulated start", async () => {
768 const { model } = await createSpreadsheetWithChart({
769 type: "odoo_line",
770 serverData: cumulativeDateServerData,
771 definition: {
772 ...cumulativeChartDefinition,
773 cumulative: true,
774 cumulatedStart: true,
775 },
776 });
777 const sheetId = model.getters.getActiveSheetId();
778 const chartId = model.getters.getChartIds(sheetId)[0];
779 await waitForDataLoaded(model);
780 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data.datasets[0].data).toEqual([
781 15, 19, 24,
782 ]);
783 const figure = model.exportData().sheets[0].figures[0];
784 expect(figure.data.cumulative).toBe(true);
785 expect(figure.data.cumulatedStart).toBe(true);
786 });
787
788 test("update existing chart to cumulate past data", async () => {
789 const { model } = await createSpreadsheetWithChart({
790 type: "odoo_line",
791 serverData: cumulativeDateServerData,
792 definition: {
793 ...cumulativeChartDefinition,
794 cumulative: true,
795 cumulatedStart: false,
796 },
797 });
798 const sheetId = model.getters.getActiveSheetId();
799 const chartId = model.getters.getChartIds(sheetId)[0];
800 await waitForDataLoaded(model);
801 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data.datasets[0].data).toEqual([
802 3, 7, 12,
803 ]);
804 const figure = model.exportData().sheets[0].figures[0];
805 expect(figure.data.cumulative).toBe(true);
806 expect(figure.data.cumulatedStart).toBe(false);
807
808 model.dispatch("UPDATE_CHART", {
809 definition: {
810 ...cumulativeChartDefinition,
811 cumulatedStart: true,
812 },
813 chartId,
814 figureId: model.getters.getFigureIdFromChartId(chartId),
815 sheetId,
816 });
817 await waitForDataLoaded(model);
818 expect(model.getters.getChartRuntime(chartId).chartJsConfig.data.datasets[0].data).toEqual([
819 15, 19, 24,
820 ]);
821 });
822
823 test("Can insert odoo chart from a different model", async () => {
824 const { model } = await createModelWithDataSource();
825 insertListInSpreadsheet(model, { model: "product", columns: ["name"] });
826 await addGlobalFilter(model, THIS_YEAR_GLOBAL_FILTER);
827 const sheetId = model.getters.getActiveSheetId();
828 expect(model.getters.getChartIds(sheetId).length).toBe(0);
829 insertChartInSpreadsheet(model);
830 expect(model.getters.getChartIds(sheetId).length).toBe(1);
831 });
832
833 test("Odoo chart legend color changes with background color update", async () => {
834 const { model } = await createSpreadsheetWithChart({ type: "odoo_bar" });
835 const sheetId = model.getters.getActiveSheetId();
836 const chartId = model.getters.getChartIds(sheetId)[0];
837 const definition = model.getters.getChartDefinition(chartId);
838 expect(
839 model.getters.getChartRuntime(chartId).chartJsConfig.options.plugins.legend.labels.color
840 ).toBe("#000000");
841 model.dispatch("UPDATE_CHART", {
842 definition: {
843 ...definition,
844 background: "#000000",
845 },
846 chartId,
847 figureId: model.getters.getFigureIdFromChartId(chartId),
848 sheetId,
849 });
850 expect(
851 model.getters.getChartRuntime(chartId).chartJsConfig.options.plugins.legend.labels.color
852 ).toBe("#FFFFFF");
853 });
854
855 test("Remove odoo chart when sheet is deleted", async () => {
856 const { model } = await createSpreadsheetWithChart({ type: "odoo_line" });
857 const sheetId = model.getters.getActiveSheetId();
858 model.dispatch("CREATE_SHEET", {
859 sheetId: model.uuidGenerator.smallUuid(),
860 position: model.getters.getSheetIds().length,
861 });
862 expect(model.getters.getOdooChartIds().length).toBe(1);
863 model.dispatch("DELETE_SHEET", { sheetId });
864 expect(model.getters.getOdooChartIds().length).toBe(0);
865 });
866
867 test("Odoo chart datasource display name has a default when the chart title is empty", async () => {
868 const { model } = await createSpreadsheetWithChart({ type: "odoo_line" });
869 const sheetId = model.getters.getActiveSheetId();
870 const chartId = model.getters.getChartIds(sheetId)[0];
871 const definition = model.getters.getChartDefinition(chartId);
872 expect(model.getters.getOdooChartDisplayName(chartId)).toBe("(#1) Partners");
873 model.dispatch("UPDATE_CHART", {
874 definition: {
875 ...definition,
876 title: { text: "" },
877 },
878 chartId,
879 figureId: model.getters.getFigureIdFromChartId(chartId),
880 sheetId,
881 });
882 expect(model.getters.getOdooChartDisplayName(chartId)).toBe("(#1) Odoo Line Chart");
883 });
884
885 test("See records when clicking on a bar chart bar", async () => {
886 mockService("action", fakeActionService);
887 const { model } = await createSpreadsheetWithChart({
888 type: "odoo_bar",
889 serverData: cumulativeDateServerData,
890 definition: {
891 ...cumulativeChartDefinition,
892 type: "odoo_bar",
893 actionXmlId: "test.my_action",
894 cumulative: true,
895 },
896 });
897 const sheetId = model.getters.getActiveSheetId();
898 const chartId = model.getters.getChartIds(sheetId)[0];
899 await waitForDataLoaded(model);
900 const runtime = model.getters.getChartRuntime(chartId);
901 expect.verifySteps([]);
902
903 const event = { type: "click", native: new Event("click") };
904 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }]);
905 await animationFrame();
906 expect.verifySteps(["load-action", "do-action"]);
907 });
908
909 test("See records in new tab on middle click of chart element", async () => {
910 const fakeActionService = {
911 doAction: async (request, options = {}) => {
912 if (request.type === "ir.actions.act_window") {
913 expect(request.res_model).toEqual("partner");
914 if (options.newWindow) {
915 expect.step("do-action-new-window");
916 }
917 }
918 },
919 };
920 mockService("action", fakeActionService);
921 const { model } = await createSpreadsheetWithChart({ type: "odoo_bar" });
922 const sheetId = model.getters.getActiveSheetId();
923 const chartId = model.getters.getChartIds(sheetId)[0];
924 await waitForDataLoaded(model);
925 const runtime = model.getters.getChartRuntime(chartId);
926 expect.verifySteps([]);
927
928 const event = { type: "mouseup", native: new MouseEvent("mouseup", { button: 1 }) }; // Middle mouse button
929 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }]);
930 expect.verifySteps(["do-action-new-window"]);
931 });
932
933 test("See records when clicking on a line chart point", async () => {
934 mockService("action", fakeActionService);
935 const { model } = await createSpreadsheetWithChart({
936 type: "odoo_line",
937 serverData: cumulativeDateServerData,
938 definition: {
939 ...cumulativeChartDefinition,
940 actionXmlId: "test.my_action",
941 cumulative: true,
942 },
943 });
944 const sheetId = model.getters.getActiveSheetId();
945 const chartId = model.getters.getChartIds(sheetId)[0];
946 await waitForDataLoaded(model);
947 const runtime = model.getters.getChartRuntime(chartId);
948 expect.verifySteps([]);
949
950 const event = { type: "click", native: new Event("click") };
951 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }]);
952 await animationFrame();
953 expect.verifySteps(["load-action", "do-action"]);
954 });
955
956 test("Actions not triggered by trendline clicks", async () => {
957 mockService("action", fakeActionService);
958 const { model } = await createSpreadsheetWithChart({
959 type: "odoo_line",
960 serverData: cumulativeDateServerData,
961 definition: {
962 ...cumulativeChartDefinition,
963 cumulative: true,
964 trend: "polynomial",
965 },
966 });
967
968 const sheetId = model.getters.getActiveSheetId();
969 const chartId = model.getters.getChartIds(sheetId)[0];
970 await waitForDataLoaded(model);
971 const runtime = model.getters.getChartRuntime(chartId);
972 expect.verifySteps([]);
973
974 const trendlineDatasetIndex = runtime.chartJsConfig.data.datasets.length;
975 const event = { type: "click", native: new Event("click") };
976 await runtime.chartJsConfig.options.onClick(event, [
977 { datasetIndex: trendlineDatasetIndex, index: 0 },
978 ]);
979 await animationFrame();
980 expect.verifySteps([]);
981 });
982
983 test("See records when clicking on a pie chart slice", async () => {
984 const fakeActionService = {
985 doAction: async (request, options = {}) => {
986 if (request.type === "ir.actions.act_window") {
987 expect.step("do-action");
988 expect(request).toEqual({
989 ...action,
990 name: "January 2022",
991 });
992 }
993 },
994 };
995 mockService("action", fakeActionService);
996 const { model } = await createSpreadsheetWithChart({
997 type: "odoo_pie",
998 serverData: cumulativeDateServerData,
999 definition: {
1000 ...cumulativeChartDefinition,
1001 type: "odoo_pie",
1002 cumulative: true,
1003 },
1004 });
1005 const sheetId = model.getters.getActiveSheetId();
1006 const chartId = model.getters.getChartIds(sheetId)[0];
1007 await waitForDataLoaded(model);
1008
1009 const runtime = model.getters.getChartRuntime(chartId);
1010 expect.verifySteps([]);
1011
1012 const event = { type: "click", native: new Event("click") };
1013 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }]);
1014 await animationFrame();
1015 expect.verifySteps(["do-action"]);
1016 });
1017
1018 test("See records when clicking on a waterfall chart bar", async () => {
1019 let lastActionCalled = undefined;
1020 const fakeActionService = {
1021 doAction: async (request, options = {}) => (lastActionCalled = request),
1022 loadAction(actionRequest) {
1023 expect(actionRequest).toBe("test.my_action");
1024 return { type: "ir.actions.act_window" };
1025 },
1026 };
1027 mockService("action", fakeActionService);
1028 const serverData = getBasicServerData();
1029 serverData.models.partner.records = [
1030 { date: "2020-01-01", probability: 10, bar: true },
1031 { date: "2020-02-01", probability: 2, bar: true },
1032 { date: "2020-01-01", probability: 4, bar: false },
1033 { date: "2020-02-01", probability: 5, bar: false },
1034 ];
1035 const { model } = await createSpreadsheetWithChart({
1036 type: "odoo_waterfall",
1037 serverData,
1038 definition: {
1039 type: "odoo_waterfall",
1040 metaData: {
1041 groupBy: ["bar", "date"],
1042 measure: "probability",
1043 order: null,
1044 resModel: "partner",
1045 },
1046 searchParams: { context: {}, domain: [], groupBy: [], orderBy: [] },
1047 actionXmlId: "test.my_action",
1048 title: { text: "Partners" },
1049 dataSourceId: "42",
1050 id: "42",
1051 showSubTotals: true,
1052 },
1053 });
1054 const sheetId = model.getters.getActiveSheetId();
1055 const chartId = model.getters.getChartIds(sheetId)[0];
1056 await waitForDataLoaded(model);
1057 const runtime = model.getters.getChartRuntime(chartId);
1058
1059 // First dataset
1060 const event = { type: "click", native: new Event("click") };
1061 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }]);
1062 await animationFrame();
1063 expect(lastActionCalled?.domain).toEqual([
1064 "&",
1065 "&",
1066 ["date", ">=", "2020-01-01"],
1067 ["date", "<", "2020-02-01"],
1068 ["bar", "=", false],
1069 ]);
1070
1071 // First dataset subtotal
1072 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 2 }]);
1073 await animationFrame();
1074 expect(lastActionCalled?.domain).toEqual([
1075 "&",
1076 "&",
1077 ["date", ">=", "2020-01-01"],
1078 ["date", "<", "2020-02-01"],
1079 [1, "=", 1],
1080 ]);
1081
1082 // Second dataset
1083 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 3 }]);
1084 await animationFrame();
1085 expect(lastActionCalled?.domain).toEqual([
1086 "&",
1087 "&",
1088 ["date", ">=", "2020-02-01"],
1089 ["date", "<", "2020-03-01"],
1090 ["bar", "=", false],
1091 ]);
1092 });
1093
1094 test("See records when clicking on a geo chart country", async () => {
1095 const country_id = fields.Many2one({ string: "Country", relation: "res.country" });
1096 Partner._fields = { ...Partner._fields, country_id };
1097 Partner._records = [
1098 { id: 1, country_id: 1, probability: 10 },
1099 { id: 2, country_id: 2, probability: 2 },
1100 ];
1101
1102 const fakeActionService = {
1103 doAction: async (request, options = {}) => {
1104 if (request.type === "ir.actions.act_window") {
1105 expect.step("do-action");
1106 expect(request.res_model).toBe("partner");
1107 expect(request.name).toBe("Belgium / Probability");
1108 expect(request.domain).toEqual([["country_id", "=", 1]]);
1109 }
1110 },
1111 };
1112 mockService("action", fakeActionService);
1113 const { model } = await createSpreadsheetWithChart({
1114 type: "odoo_geo",
1115 modelConfig: { external: { geoJsonService: { getAvailableRegions: () => [] } } },
1116 definition: {
1117 type: "odoo_geo",
1118 legendPosition: "top",
1119 metaData: {
1120 groupBy: ["country_id"],
1121 measure: "probability",
1122 resModel: "partner",
1123 order: null,
1124 },
1125 searchParams: { context: {}, domain: [], groupBy: [], orderBy: [] },
1126 cumulative: true,
1127 title: { text: "Partners" },
1128 dataSourceId: "42",
1129 id: "42",
1130 },
1131 });
1132 const sheetId = model.getters.getActiveSheetId();
1133 const chartId = model.getters.getChartIds(sheetId)[0];
1134 await waitForDataLoaded(model);
1135
1136 const runtime = model.getters.getChartRuntime(chartId);
1137 expect.verifySteps([]);
1138 const mockElement = { feature: { properties: { name: "Belgium" } } };
1139 const event = { type: "click", native: new Event("click") };
1140 await runtime.chartJsConfig.options.onClick(event, [
1141 { datasetIndex: 0, index: 0, element: mockElement },
1142 ]);
1143 expect.verifySteps(["do-action"]);
1144 });
1145
1146 test("See records when clicking on a sunburst chart slice", async () => {
1147 let lastActionCalled = undefined;
1148 const fakeActionService = {
1149 doAction: async (request, options = {}) => (lastActionCalled = request),
1150 };
1151 mockService("action", fakeActionService);
1152 const serverData = getBasicServerData();
1153 serverData.models.partner.records = [
1154 { date: "2020-01-01", probability: 10, bar: true },
1155 { date: "2020-02-01", probability: 2, bar: true },
1156 { date: "2020-01-01", probability: 4, bar: false },
1157 { date: "2020-02-01", probability: 5, bar: false },
1158 ];
1159 const { model } = await createSpreadsheetWithChart({
1160 type: "odoo_sunburst",
1161 serverData,
1162 definition: {
1163 type: "odoo_sunburst",
1164 metaData: {
1165 groupBy: ["date:month", "bar"],
1166 measure: "probability",
1167 order: null,
1168 resModel: "partner",
1169 },
1170 searchParams: { context: {}, domain: [], groupBy: [], orderBy: [] },
1171 title: { text: "Partners" },
1172 dataSourceId: "42",
1173 id: "42",
1174 },
1175 });
1176 const sheetId = model.getters.getActiveSheetId();
1177 const chartId = model.getters.getChartIds(sheetId)[0];
1178 await waitForDataLoaded(model);
1179 const runtime = model.getters.getChartRuntime(chartId);
1180
1181 // Leaf value
1182 let mockChart = { data: { datasets: [{ data: [{ groups: ["January 2020", "false"] }] }] } };
1183 const event = { type: "click", native: new Event("click") };
1184 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }], mockChart);
1185 expect(lastActionCalled?.domain).toEqual([
1186 "&",
1187 "&",
1188 ["date", ">=", "2020-01-01"],
1189 ["date", "<", "2020-02-01"],
1190 ["bar", "=", false],
1191 ]);
1192
1193 // Non-leaf value
1194 mockChart = { data: { datasets: [{ data: [{ groups: ["February 2020"] }] }] } };
1195 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }], mockChart);
1196 expect(lastActionCalled?.domain).toEqual([
1197 "&",
1198 ["date", ">=", "2020-02-01"],
1199 ["date", "<", "2020-03-01"],
1200 ]);
1201 });
1202
1203 test("See records when clicking on a treemap chart item", async () => {
1204 let lastActionCalled = undefined;
1205 const fakeActionService = {
1206 doAction: async (request, options = {}) => (lastActionCalled = request),
1207 };
1208 mockService("action", fakeActionService);
1209 const serverData = getBasicServerData();
1210 serverData.models.partner.records = [
1211 { date: "2020-01-01", probability: 10, bar: true },
1212 { date: "2020-02-01", probability: 2, bar: true },
1213 { date: "2020-01-01", probability: 4, bar: false },
1214 { date: "2020-02-01", probability: 5, bar: false },
1215 ];
1216 const { model } = await createSpreadsheetWithChart({
1217 type: "odoo_treemap",
1218 serverData,
1219 definition: {
1220 type: "odoo_treemap",
1221 metaData: {
1222 groupBy: ["date:month", "bar"],
1223 measure: "probability",
1224 order: null,
1225 resModel: "partner",
1226 },
1227 searchParams: { context: {}, domain: [], groupBy: [], orderBy: [] },
1228 title: { text: "Partners" },
1229 dataSourceId: "42",
1230 id: "42",
1231 },
1232 });
1233 const sheetId = model.getters.getActiveSheetId();
1234 const chartId = model.getters.getChartIds(sheetId)[0];
1235 await waitForDataLoaded(model);
1236 const runtime = model.getters.getChartRuntime(chartId);
1237
1238 function buildMockTreemapChart(groups) {
1239 const depth = groups.length - 1;
1240 const data = {};
1241 for (let i = 0; i <= depth; i++) {
1242 data[i] = groups[i];
1243 }
1244 return { data: { datasets: [{ data: [{ l: depth, _data: data }] }] } };
1245 }
1246
1247 // Leaf value
1248 let mockChart = buildMockTreemapChart(["January 2020", "false"]);
1249 const event = { type: "click", native: new Event("click") };
1250 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }], mockChart);
1251 expect(lastActionCalled?.domain).toEqual([
1252 "&",
1253 "&",
1254 ["date", ">=", "2020-01-01"],
1255 ["date", "<", "2020-02-01"],
1256 ["bar", "=", false],
1257 ]);
1258
1259 // Non-leaf value
1260 mockChart = buildMockTreemapChart(["February 2020"]);
1261 await runtime.chartJsConfig.options.onClick(event, [{ datasetIndex: 0, index: 0 }], mockChart);
1262 expect(lastActionCalled?.domain).toEqual([
1263 "&",
1264 ["date", ">=", "2020-02-01"],
1265 ["date", "<", "2020-03-01"],
1266 ]);
1267 });
1268
1269 test("import/export action xml id", async () => {
1270 const { model } = await createSpreadsheetWithChart({
1271 type: "odoo_bar",
1272 definition: {
1273 type: "odoo_bar",
1274 metaData: {
1275 groupBy: [],
1276 measure: "probability",
1277 resModel: "partner",
1278 },
1279 searchParams: {
1280 domain: [],
1281 groupBy: [],
1282 orderBy: [],
1283 },
1284 actionXmlId: "test.my_action",
1285 cumulative: true,
1286 title: { text: "Partners" },
1287 dataSourceId: "42",
1288 id: "42",
1289 },
1290 });
1291 const exported = model.exportData();
1292 expect(exported.sheets[0].figures[0].data.actionXmlId).toBe("test.my_action");
1293
1294 const { model: model2 } = await createModelWithDataSource({ spreadsheetData: exported });
1295 const sheetId = model2.getters.getActiveSheetId();
1296 const chartId = model2.getters.getChartIds(sheetId)[0];
1297 expect(model2.getters.getChartDefinition(chartId).actionXmlId).toBe("test.my_action");
1298 });
1299
1300 test("Show values is taken into account in the runtime", async () => {
1301 const { model } = await createSpreadsheetWithChart({ type: "odoo_bar" });
1302 const sheetId = model.getters.getActiveSheetId();
1303 const chartId = model.getters.getChartIds(sheetId)[0];
1304 const definition = model.getters.getChartDefinition(chartId);
1305 model.dispatch("UPDATE_CHART", {
1306 definition: {
1307 ...definition,
1308 showValues: true,
1309 },
1310 chartId,
1311 figureId: model.getters.getFigureIdFromChartId(chartId),
1312 sheetId,
1313 });
1314 const runtime = model.getters.getChartRuntime(chartId);
1315 expect(runtime.chartJsConfig.options.plugins.chartShowValuesPlugin.showValues).toBe(true);
1316 });
1317
1318 test("Odoo line and bar charts display only horizontal grid lines", async () => {
1319 const { model } = await createSpreadsheetWithChart({
1320 type: "odoo_line",
1321 });
1322 const sheetId = model.getters.getActiveSheetId();
1323 const chartId = model.getters.getChartIds(sheetId)[0];
1324 const lineChartConfig = model.getters.getChartRuntime(chartId).chartJsConfig;
1325
1326 expect(lineChartConfig.options.scales.x.grid.display).toBe(false);
1327 expect(lineChartConfig.options.scales.y.grid.display).toBe(true);
1328
1329 const lineChartDefinition = model.getters.getChartDefinition(chartId);
1330 model.dispatch("UPDATE_CHART", {
1331 definition: {
1332 ...lineChartDefinition,
1333 type: "odoo_bar",
1334 },
1335 chartId,
1336 figureId: model.getters.getFigureIdFromChartId(chartId),
1337 sheetId,
1338 });
1339
1340 const barChartConfig = model.getters.getChartRuntime(chartId).chartJsConfig;
1341
1342 expect(barChartConfig.options.scales.x.grid.display).toBe(false);
1343 expect(barChartConfig.options.scales.y.grid.display).toBe(true);
1344 });
1345
1346 test("Can configure the chart datasets", async () => {
1347 const searchParams = { comparison: null, context: {}, domain: [], groupBy: [], orderBy: [] };
1348 const metaData = {
1349 groupBy: ["name", "bar"],
1350 measure: "probability",
1351 order: null,
1352 resModel: "partner",
1353 };
1354
1355 const serverData = getBasicServerData();
1356 serverData.models.partner.records = [
1357 { name: "Frank", bar: true, probability: 10, foo: 5 },
1358 { name: "Marc", bar: false, probability: 2, foo: 5 },
1359 ];
1360 const { model } = await createSpreadsheetWithChart({
1361 type: "odoo_bar",
1362 serverData,
1363 definition: { type: "odoo_bar", metaData, searchParams, id: "42" },
1364 });
1365 await waitForDataLoaded(model);
1366 const sheetId = model.getters.getActiveSheetId();
1367 const chartId = model.getters.getChartIds(sheetId)[0];
1368 let definition = model.getters.getChartDefinition(chartId);
1369 expect(definition.dataSets).toEqual([{}, {}]);
1370
1371 model.dispatch("UPDATE_CHART", {
1372 definition: { ...definition, dataSets: [{ label: "My dataset" }, { label: "Second" }] },
1373 chartId,
1374 figureId: model.getters.getFigureIdFromChartId(chartId),
1375 sheetId,
1376 });
1377 definition = model.getters.getChartDefinition(chartId);
1378 expect(definition.dataSets).toEqual([{ label: "My dataset" }, { label: "Second" }]);
1379
1380 model.dispatch("UPDATE_CHART", {
1381 definition: {
1382 ...definition,
1383 searchParams: { ...searchParams, domain: [["bar", "=", false]] },
1384 },
1385 chartId,
1386 figureId: model.getters.getFigureIdFromChartId(chartId),
1387 sheetId,
1388 });
1389 model.dispatch("REFRESH_ALL_DATA_SOURCES");
1390 await waitForDataLoaded(model);
1391 definition = model.getters.getChartDefinition(chartId);
1392 // the second dataset was dropped from the definition since there is now only a single dataset in the data source
1393 expect(definition.dataSets).toEqual([{ label: "My dataset" }]);
1394 });
1395
1396 test("Chart data source is updated when changing chart type", async () => {
1397 patchWithCleanup(ChartDataSource.prototype, {
1398 changeChartType(newMode) {
1399 expect.step("changeChartType");
1400 expect(newMode).toBe("line");
1401 super.changeChartType(newMode);
1402 },
1403 });
1404
1405 const { model } = await createSpreadsheetWithChart({ type: "odoo_bar" });
1406 const sheetId = model.getters.getActiveSheetId();
1407 const chartId = model.getters.getChartIds(sheetId)[0];
1408 const chartDataSource = model.getters.getChartDataSource(chartId);
1409
1410 model.dispatch("UPDATE_CHART", {
1411 definition: {
1412 ...model.getters.getChartDefinition(chartId),
1413 type: "odoo_line",
1414 },
1415 chartId,
1416 figureId: model.getters.getFigureIdFromChartId(chartId),
1417 sheetId,
1418 });
1419 expect.verifySteps(["changeChartType"]);
1420 expect(chartDataSource._metaData.mode).toBe("line");
1421 });
1422
1423 test("Non-web chart types are using data source in 'bar' mode", async () => {
1424 const { model } = await createSpreadsheetWithChart({ type: "odoo_radar" });
1425 const sheetId = model.getters.getActiveSheetId();
1426 const chartId = model.getters.getChartIds(sheetId)[0];
1427
1428 const chart = model.getters.getChart(chartId);
1429 expect(chart.metaData.mode).toBe("bar");
1430
1431 const chartDataSource = model.getters.getChartDataSource(chartId);
1432 expect(chartDataSource._metaData.mode).toBe("bar");
1433 });
1434
1435 test("Long labels are only truncated in the axis callback, not in the data given to the chart", async () => {
1436 const serverData = getBasicServerData();
1437 serverData.models.partner.records = [
1438 { name: "Guy", probability: 10 },
1439 { name: "Guy with a very very very long name", probability: 2 },
1440 ];
1441 const searchParams = { comparison: null, context: {}, domain: [], groupBy: [], orderBy: [] };
1442 const { model } = await createSpreadsheetWithChart({
1443 type: "odoo_line",
1444 serverData,
1445 definition: {
1446 type: "odoo_line",
1447 metaData: {
1448 groupBy: ["name"],
1449 measure: "probability",
1450 order: null,
1451 resModel: "partner",
1452 },
1453 searchParams,
1454 title: { text: "Partners" },
1455 dataSourceId: "42",
1456 id: "42",
1457 },
1458 });
1459 const sheetId = model.getters.getActiveSheetId();
1460 const chartId = model.getters.getChartIds(sheetId)[0];
1461 await waitForDataLoaded(model);
1462 const config = model.getters.getChartRuntime(chartId).chartJsConfig;
1463 expect(config.data.labels).toEqual(["Guy", "Guy with a very very very long name"]);
1464
1465 const fakeChart = { getLabelForValue: (value) => value };
1466 const scaleCallback = config.options.scales.x.ticks.callback.bind(fakeChart);
1467 expect(scaleCallback("Guy")).toBe("Guy");
1468 expect(scaleCallback("Guy with a very very very long name")).toBe("Guy with a very very…");
1469 });
1470
1471 test("can change chart granularity", async () => {
1472 const { model } = await createSpreadsheetWithChart({
1473 type: "odoo_bar",
1474 definition: {
1475 metaData: {
1476 groupBy: ["date:month"],
1477 measure: "probability",
1478 resModel: "partner",
1479 },
1480 },
1481 });
1482 const sheetId = model.getters.getActiveSheetId();
1483 const chartId = model.getters.getChartIds(sheetId)[0];
1484 model.dispatch("UPDATE_CHART_GRANULARITY", {
1485 chartId,
1486 granularity: "year",
1487 });
1488 expect(model.getters.getChartDefinition(chartId).metaData.groupBy).toEqual(["date:year"]);
1489 });
1490
1491 test("changing chart granularity reloads data source once with global filter", async () => {
1492 onRpc("partner", "formatted_read_group", ({ kwargs }) => {
1493 expect.step(kwargs.groupby[0]);
1494 expect(kwargs.domain.length).toBe(3, { message: "Global filter domain is applied" });
1495 });
1496 const { model } = await createSpreadsheetWithChart({
1497 type: "odoo_bar",
1498 definition: {
1499 metaData: {
1500 groupBy: ["date:month"],
1501 measure: "probability",
1502 resModel: "partner",
1503 },
1504 },
1505 });
1506 const sheetId = model.getters.getActiveSheetId();
1507 const chartId = model.getters.getChartIds(sheetId)[0];
1508 await addGlobalFilter(
1509 model,
1510 { id: "42", type: "date", label: "Date", defaultValue: "last_90_days" },
1511 {
1512 chart: { [chartId]: { chain: "date", type: "date" } },
1513 }
1514 );
1515 model.getters.getChartRuntime(chartId); // load the data
1516 await animationFrame();
1517 expect.verifySteps(["date:month"]);
1518 model.dispatch("UPDATE_CHART_GRANULARITY", {
1519 chartId,
1520 granularity: "year",
1521 });
1522 model.getters.getChartRuntime(chartId); // load the data
1523 await animationFrame();
1524 expect.verifySteps(["date:year"]);
1525 });
1526
1527 test("available granularities without filter", async () => {
1528 const { model } = await createSpreadsheetWithChart({
1529 type: "odoo_bar",
1530 definition: {
1531 metaData: {
1532 groupBy: ["date:month"],
1533 measure: "probability",
1534 resModel: "partner",
1535 order: null,
1536 },
1537 },
1538 });
1539 const sheetId = model.getters.getActiveSheetId();
1540 const chartId = model.getters.getChartIds(sheetId)[0];
1541
1542 expect(model.getters.getAvailableChartGranularities(chartId).map((g) => g.value)).toEqual([
1543 "day",
1544 "week",
1545 "month",
1546 "quarter",
1547 "year",
1548 ]);
1549 });
1550
1551 test("no available granularities when not grouped by a date", async () => {
1552 const { model } = await createSpreadsheetWithChart({
1553 type: "odoo_bar",
1554 definition: {
1555 metaData: {
1556 groupBy: ["name"],
1557 measure: "probability",
1558 resModel: "partner",
1559 order: null,
1560 },
1561 },
1562 });
1563 const sheetId = model.getters.getActiveSheetId();
1564 const chartId = model.getters.getChartIds(sheetId)[0];
1565 expect(model.getters.getAvailableChartGranularities(chartId)).toEqual([]);
1566 });
1567
1568 test("available granularities with a date filter", async () => {
1569 const { model } = await createSpreadsheetWithChart({
1570 type: "odoo_bar",
1571 definition: {
1572 metaData: {
1573 groupBy: ["date:month"],
1574 measure: "probability",
1575 resModel: "partner",
1576 order: null,
1577 },
1578 },
1579 });
1580 const sheetId = model.getters.getActiveSheetId();
1581 const chartId = model.getters.getChartIds(sheetId)[0];
1582 const filterId = "42";
1583 await addGlobalFilter(
1584 model,
1585 { id: filterId, type: "date", label: "Date" },
1586 {
1587 chart: { [chartId]: { chain: "date", type: "date" } },
1588 }
1589 );
1590 model.updateMode("dashboard");
1591 expect(model.getters.getAvailableChartGranularities(chartId).map((g) => g.value)).toEqual([
1592 "day",
1593 "week",
1594 "month",
1595 "quarter",
1596 "year",
1597 ]);
1598 await setGlobalFilterValue(model, {
1599 id: filterId,
1600 value: { type: "relative", period: "last_90_days" },
1601 });
1602 expect(model.getters.getAvailableChartGranularities(chartId).map((g) => g.value)).toEqual([
1603 "day",
1604 "week",
1605 "month",
1606 "quarter",
1607 ]);
1608 await setGlobalFilterValue(model, {
1609 id: filterId,
1610 value: { type: "relative", period: "today" },
1611 });
1612 expect(model.getters.getAvailableChartGranularities(chartId).map((g) => g.value)).toEqual([
1613 "day",
1614 ]);
1615 });
1616
1617 test("hour is an available granularity with a filtered datetime field", async () => {
1618 const { model } = await createSpreadsheetWithChart({
1619 type: "odoo_bar",
1620 definition: {
1621 metaData: {
1622 groupBy: ["create_date:month"],
1623 measure: "probability",
1624 resModel: "partner",
1625 order: null,
1626 },
1627 },
1628 });
1629 const sheetId = model.getters.getActiveSheetId();
1630 const chartId = model.getters.getChartIds(sheetId)[0];
1631 const filterId = "42";
1632 await addGlobalFilter(
1633 model,
1634 { id: filterId, type: "date", label: "Date" },
1635 {
1636 chart: { [chartId]: { chain: "create_date", type: "datetime" } },
1637 }
1638 );
1639 model.updateMode("dashboard");
1640 expect(model.getters.getAvailableChartGranularities(chartId).map((g) => g.value)).toEqual([
1641 "day",
1642 "week",
1643 "month",
1644 "quarter",
1645 "year",
1646 ]);
1647 await setGlobalFilterValue(model, {
1648 id: filterId,
1649 value: { type: "relative", period: "today" },
1650 });
1651 expect(model.getters.getAvailableChartGranularities(chartId).map((g) => g.value)).toEqual([
1652 "hour",
1653 "day",
1654 ]);
1655 });
1656
1657 test("filtering a chart axis changes its granularity", async () => {
1658 const { model } = await createSpreadsheetWithChart({
1659 type: "odoo_bar",
1660 definition: {
1661 metaData: {
1662 groupBy: ["create_date:year"],
1663 measure: "probability",
1664 resModel: "partner",
1665 order: null,
1666 },
1667 },
1668 });
1669 const sheetId = model.getters.getActiveSheetId();
1670 const chartId = model.getters.getChartIds(sheetId)[0];
1671 const filterId = "42";
1672 await addGlobalFilter(
1673 model,
1674 { id: filterId, type: "date", label: "Date" },
1675 {
1676 chart: { [chartId]: { chain: "create_date", type: "datetime" } },
1677 }
1678 );
1679 model.updateMode("dashboard");
1680
1681 const cases = [
1682 [{ type: "year", year: 2024 }, ["create_date:month"]],
1683 [{ type: "quarter", year: 2024, quarter: 1 }, ["create_date:month"]],
1684 [{ type: "month", year: 2024, month: 1 }, ["create_date:day"]],
1685 [{ type: "relative", period: "last_12_months" }, ["create_date:month"]],
1686 [{ type: "relative", period: "last_90_days" }, ["create_date:day"]],
1687 [{ type: "relative", period: "last_30_days" }, ["create_date:day"]],
1688 [{ type: "relative", period: "last_7_days" }, ["create_date:day"]],
1689 [{ type: "relative", period: "today" }, ["create_date:hour"]],
1690 ];
1691
1692 for (const [value, expectedGroupBy] of cases) {
1693 await setGlobalFilterValue(model, {
1694 id: filterId,
1695 value,
1696 });
1697 expect(model.getters.getChartDefinition(chartId).metaData.groupBy).toEqual(
1698 expectedGroupBy,
1699 {
1700 message: `Expected groupBy to be ${expectedGroupBy} for value ${JSON.stringify(
1701 value
1702 )}`,
1703 }
1704 );
1705 }
1706 });
1707
1708 test("filtering doesn't change its granularity if not the horizontal axis", async () => {
1709 const { model } = await createSpreadsheetWithChart({
1710 type: "odoo_bar",
1711 definition: {
1712 metaData: {
1713 groupBy: ["create_date:year"],
1714 measure: "probability",
1715 resModel: "partner",
1716 order: null,
1717 },
1718 },
1719 });
1720 const sheetId = model.getters.getActiveSheetId();
1721 const chartId = model.getters.getChartIds(sheetId)[0];
1722 const filterId = "42";
1723 await addGlobalFilter(
1724 model,
1725 { id: filterId, type: "date", label: "Date" },
1726 {
1727 chart: { [chartId]: { chain: "date", type: "date" } }, // the horizontal axis is create_date, not date
1728 }
1729 );
1730 model.updateMode("dashboard");
1731
1732 await setGlobalFilterValue(model, {
1733 id: filterId,
1734 value: { type: "relative", period: "last_7_days" },
1735 });
1736 expect(model.getters.getChartDefinition(chartId).metaData.groupBy).toEqual([
1737 "create_date:year",
1738 ]);
1739 });
1740
1741 test("filtering preserves manually changed granularity", async () => {
1742 const { model } = await createSpreadsheetWithChart({
1743 type: "odoo_bar",
1744 definition: {
1745 metaData: {
1746 groupBy: ["create_date:year"],
1747 measure: "probability",
1748 resModel: "partner",
1749 order: null,
1750 },
1751 },
1752 });
1753 const sheetId = model.getters.getActiveSheetId();
1754 const chartId = model.getters.getChartIds(sheetId)[0];
1755 const filterId = "42";
1756 await addGlobalFilter(
1757 model,
1758 { id: filterId, type: "date", label: "Date" },
1759 {
1760 chart: { [chartId]: { chain: "create_date", type: "datetime" } },
1761 }
1762 );
1763 model.updateMode("dashboard");
1764 await setGlobalFilterValue(model, {
1765 id: filterId,
1766 value: { type: "relative", period: "last_90_days" },
1767 });
1768 expect(model.getters.getChartDefinition(chartId).metaData.groupBy).toEqual(["create_date:day"]);
1769 model.dispatch("UPDATE_CHART_GRANULARITY", {
1770 chartId,
1771 granularity: "week", // Manually change granularity to from day to week
1772 });
1773
1774 // filtering on an equivalent range should not change the granularity
1775 await setGlobalFilterValue(model, {
1776 id: filterId,
1777 value: {
1778 type: "range",
1779 from: "2025-01-24",
1780 to: "2025-04-23",
1781 },
1782 });
1783 expect(model.getters.getChartDefinition(chartId).metaData.groupBy).toEqual([
1784 "create_date:week",
1785 ]);
1786
1787 // changing to a non-compatible range should change the granularity
1788 await setGlobalFilterValue(model, {
1789 id: filterId,
1790 value: { type: "relative", period: "today" },
1791 });
1792 expect(model.getters.getChartDefinition(chartId).metaData.groupBy).toEqual([
1793 "create_date:hour",
1794 ]);
1795 });
1796
1797 test("filtering on 'day' doesn't change to hour if not datetime", async () => {
1798 const { model } = await createSpreadsheetWithChart({
1799 type: "odoo_bar",
1800 definition: {
1801 metaData: {
1802 groupBy: ["date:year"],
1803 measure: "probability",
1804 resModel: "partner",
1805 order: null,
1806 },
1807 },
1808 });
1809 const sheetId = model.getters.getActiveSheetId();
1810 const chartId = model.getters.getChartIds(sheetId)[0];
1811 const filterId = "42";
1812 await addGlobalFilter(
1813 model,
1814 { id: filterId, type: "date", label: "Date" },
1815 {
1816 chart: { [chartId]: { chain: "date", type: "date" } },
1817 }
1818 );
1819 model.updateMode("dashboard");
1820
1821 await setGlobalFilterValue(model, {
1822 id: filterId,
1823 value: { type: "relative", period: "today" },
1824 });
1825 expect(model.getters.getChartDefinition(chartId).metaData.groupBy).toEqual(["date:day"]);
1826 });