Debian Codegraph @

addons/web/static/tests/views/graph/graph_view.test.js

raw
1 import { expect, test } from "@odoo/hoot";
2 import { queryAllTexts } from "@odoo/hoot-dom";
3 import { Deferred, animationFrame, mockDate } from "@odoo/hoot-mock";
4 import { onRendered } from "@odoo/owl";
5 import {
6 contains,
7 defineModels,
8 editFavoriteName,
9 fields,
10 getService,
11 makeMockServer,
12 mockService,
13 models,
14 mountView,
15 mountWithCleanup,
16 onRpc,
17 patchWithCleanup,
18 saveFavorite,
19 serverState,
20 switchView,
21 toggleMenu,
22 toggleMenuItem,
23 toggleMenuItemOption,
24 toggleSaveFavorite,
25 toggleSearchBarMenu,
26 validateSearch,
27 } from "@web/../tests/web_test_helpers";
28 import {
29 checkDatasets,
30 checkLabels,
31 checkLegend,
32 checkModeIs,
33 checkTooltip,
34 checkYTicks,
35 clickOnDataset,
36 clickOnLegend,
37 clickSort,
38 getChart,
39 getGraphModel,
40 getGraphModelMetaData,
41 getGraphRenderer,
42 getModeButton,
43 getScaleY,
44 getYAxisLabel,
45 selectMode,
46 setupChartJsForTests,
47 } from "./graph_test_helpers";
48
49 import { DEFAULT_BG, getBorderWhite, getColors, lightenColor } from "@web/core/colors/colors";
50 import { Domain } from "@web/core/domain";
51 import { SampleServer } from "@web/model/sample_server";
52 import { GraphArchParser } from "@web/views/graph/graph_arch_parser";
53 import { GraphModel } from "@web/views/graph/graph_model";
54 import { GraphRenderer } from "@web/views/graph/graph_renderer";
55 import { WebClient } from "@web/webclient/webclient";
56
57 class Color extends models.Model {
58 name = fields.Char();
59
60 _records = [
61 {
62 id: 1,
63 name: "black",
64 },
65 {
66 id: 2,
67 name: "red",
68 },
69 ];
70 }
71
72 class Product extends models.Model {
73 name = fields.Char();
74
75 _records = [
76 {
77 id: 100,
78 name: "xphone",
79 },
80 {
81 id: 200,
82 name: "xpad",
83 },
84 ];
85 }
86
87 class Foo extends models.Model {
88 bar = fields.Boolean({ default: false });
89 color_id = fields.Many2one({ relation: "color" });
90 color_ids = fields.Many2many({ relation: "color" });
91 date = fields.Date();
92 foo = fields.Integer();
93 product_id = fields.Many2one({ relation: "product" });
94 revenue = fields.Float();
95
96 _records = [
97 {
98 id: 1,
99 foo: 3,
100 bar: true,
101 product_id: 100,
102 date: "2016-01-01",
103 revenue: 1,
104 color_ids: [2],
105 },
106 {
107 id: 2,
108 foo: 53,
109 bar: true,
110 product_id: 100,
111 color_id: 2,
112 date: "2016-01-03",
113 revenue: 2,
114 color_ids: [1],
115 },
116 {
117 id: 3,
118 foo: 2,
119 bar: true,
120 product_id: 100,
121 date: "2016-03-04",
122 revenue: 3,
123 color_ids: [1, 2],
124 },
125 {
126 id: 4,
127 foo: 24,
128 product_id: 100,
129 date: "2016-03-07",
130 revenue: 4,
131 color_ids: [2],
132 },
133 {
134 id: 5,
135 foo: 4,
136 product_id: 200,
137 date: "2016-05-01",
138 revenue: 5,
139 color_ids: [1, 2],
140 },
141 {
142 id: 6,
143 foo: 63,
144 product_id: 200,
145 },
146 {
147 id: 7,
148 foo: 42,
149 product_id: 200,
150 },
151 {
152 id: 8,
153 foo: 48,
154 product_id: 200,
155 date: "2016-04-01",
156 revenue: 8,
157 },
158 ];
159 _views = {
160 search: /* xml */ `
161 <search>
162 <filter name="false_domain" string="False Domain" domain="[(0, '=', 1)]" />
163 <filter name="filter_with_context"
164 string="Filter With Context"
165 domain="[]"
166 context="{ 'graph_measure': 'foo', 'graph_mode': 'line', 'graph_groupbys': ['color_id'] }"
167 />
168 <filter name="group_by_color" string="Color" context="{ 'group_by': 'color_id' }" />
169 <filter name="group_by_product" string="Product" context="{ 'group_by': 'product_id' }" />
170 </search>
171 `,
172 };
173 }
174
175 class Currency extends models.Model {
176 _name = "res.currency";
177
178 name = fields.Char();
179 symbol = fields.Char();
180 position = fields.Selection({
181 selection: [
182 ["after", "A"],
183 ["before", "B"],
184 ],
185 });
186
187 _records = [
188 { id: 1, name: "USD", symbol: "$", position: "before" },
189 { id: 2, name: "EUR", symbol: "€", position: "after" },
190 ];
191 }
192
193 defineModels([Foo, Color, Product, Currency]);
194
195 setupChartJsForTests();
196
197 test('graph view with "class" attribute', async () => {
198 await mountView({
199 type: "graph",
200 resModel: "foo",
201 arch: `<graph class="foobar-class"/>`,
202 });
203 expect(".o_graph_view").toHaveClass("foobar-class");
204 });
205
206 test("simple bar chart rendering", async () => {
207 const view = await mountView({ type: "graph", resModel: "foo" });
208
209 const { measure, mode, order, stacked } = getGraphModelMetaData(view);
210
211 expect(".o_graph_view").toHaveClass("o_view_controller");
212 expect(".o_graph_canvas_container canvas").toHaveCount(1);
213 expect(measure).toBe("__count", {
214 message: `the active measure should be "__count" by default`,
215 });
216 expect(mode).toBe("bar", { message: "should be in bar chart mode by default" });
217 expect(order).toBe(null, { message: "should not be ordered by default" });
218 expect(stacked).toBe(true, { message: "bar charts should be stacked by default" });
219
220 checkLabels(view, ["Total"]);
221 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label", "stack"], {
222 backgroundColor: "#4EA7F2",
223 borderColor: undefined,
224 data: [8],
225 label: "Count",
226 stack: "",
227 });
228 checkLegend(view, "Count");
229 checkTooltip(view, { lines: [{ label: "Total", value: "8" }] }, 0);
230 });
231
232 test("simple bar chart rendering with no data", async () => {
233 Foo._records = [];
234
235 const view = await mountView({ type: "graph", resModel: "foo" });
236
237 expect(".o_graph_canvas_container canvas").toHaveCount(1);
238 expect(".o_nocontent_help").toHaveCount(0);
239 checkLabels(view, []);
240 checkDatasets(view, [], []);
241 });
242
243 test("simple bar chart rendering (one groupBy)", async () => {
244 const view = await mountView({
245 type: "graph",
246 resModel: "foo",
247 arch: /* xml */ `
248 <graph>
249 <field name="bar" />
250 </graph>
251 `,
252 });
253
254 expect(".o_graph_canvas_container canvas").toHaveCount(1);
255 checkLabels(view, ["false", "true"]);
256 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label"], {
257 backgroundColor: "#4EA7F2",
258 borderColor: undefined,
259 data: [5, 3],
260 label: "Count",
261 });
262 checkLegend(view, "Count");
263 checkTooltip(view, { lines: [{ label: "false", value: "5" }] }, 0);
264 checkTooltip(view, { lines: [{ label: "true", value: "3" }] }, 1);
265 });
266
267 test("simple bar chart rendering (two groupBy)", async () => {
268 const view = await mountView({
269 type: "graph",
270 resModel: "foo",
271 arch: /* xml */ `
272 <graph>
273 <field name="bar" />
274 <field name="product_id" />
275 </graph>
276 `,
277 });
278
279 expect(".o_graph_canvas_container canvas").toHaveCount(1);
280 checkLabels(view, ["false", "true"]);
281 checkDatasets(
282 view,
283 ["backgroundColor", "borderColor", "data", "label"],
284 [
285 {
286 backgroundColor: "#4EA7F2",
287 borderColor: undefined,
288 data: [1, 3],
289 label: "xphone",
290 },
291 {
292 backgroundColor: "#EA6175",
293 borderColor: undefined,
294 data: [4, 0],
295 label: "xpad",
296 },
297 {
298 backgroundColor: "#343a40",
299 borderColor: "rgba(0,0,0,.3)",
300 data: [5, 3],
301 label: "Sum",
302 },
303 ]
304 );
305 checkLegend(view, ["xphone", "xpad", "Sum"]);
306 checkTooltip(view, { lines: [{ label: "false / xphone", value: "1" }] }, 0, 0);
307 checkTooltip(view, { lines: [{ label: "true / xphone", value: "3" }] }, 1, 0);
308 checkTooltip(view, { lines: [{ label: "false / xpad", value: "4" }] }, 0, 1);
309 checkTooltip(view, { lines: [{ label: "true / xpad", value: "0" }] }, 1, 1);
310 });
311
312 test("bar chart many2many groupBy", async () => {
313 const view = await mountView({
314 type: "graph",
315 resModel: "foo",
316 arch: /* xml */ `
317 <graph>
318 <field name="revenue" type="measure" />
319 <field name="color_ids" />
320 </graph>
321 `,
322 });
323
324 expect(".o_graph_canvas_container canvas").toHaveCount(1);
325 checkLabels(view, ["black", "red", "None"]);
326 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label"], {
327 backgroundColor: "#4EA7F2",
328 borderColor: undefined,
329 data: [10, 13, 8],
330 label: "Revenue",
331 });
332 checkLegend(view, "Revenue");
333 checkTooltip(view, { lines: [{ label: "black", value: "10" }], title: "Revenue" }, 0);
334 checkTooltip(view, { lines: [{ label: "red", value: "13" }], title: "Revenue" }, 1);
335 checkTooltip(view, { lines: [{ label: "None", value: "8" }], title: "Revenue" }, 2);
336 });
337
338 test("differentiate many2many values with same label", async () => {
339 Color._records.push({ id: 3, name: "red" });
340 Foo._records.push({ color_ids: [3], revenue: 14 });
341
342 const view = await mountView({
343 type: "graph",
344 resModel: "foo",
345 arch: /* xml */ `
346 <graph>
347 <field name="revenue" type="measure" />
348 <field name="color_ids" />
349 </graph>
350 `,
351 });
352
353 expect(".o_graph_canvas_container canvas").toHaveCount(1);
354 checkLabels(view, ["black", "red", "red (2)", "None"]);
355 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label"], {
356 backgroundColor: "#4EA7F2",
357 borderColor: undefined,
358 data: [10, 13, 14, 8],
359 label: "Revenue",
360 });
361 checkTooltip(view, { lines: [{ label: "black", value: "10" }], title: "Revenue" }, 0);
362 checkTooltip(view, { lines: [{ label: "red", value: "13" }], title: "Revenue" }, 1);
363 checkTooltip(view, { lines: [{ label: "red (2)", value: "14" }], title: "Revenue" }, 2);
364 checkTooltip(view, { lines: [{ label: "None", value: "8" }], title: "Revenue" }, 3);
365 });
366
367 test("line chart rendering (no groupBy)", async () => {
368 const view = await mountView({
369 type: "graph",
370 resModel: "foo",
371 arch: /* xml */ `<graph type="line" />`,
372 });
373
374 expect(".o_graph_canvas_container canvas").toHaveCount(1);
375 expect(getGraphModelMetaData(view).mode).toBe("line");
376 checkLabels(view, ["", "Total", ""]);
377 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label", "stack"], {
378 backgroundColor: "#a7d3f9",
379 borderColor: "#4EA7F2",
380 data: [undefined, 8],
381 label: "Count",
382 stack: undefined,
383 });
384 checkLegend(view, "Count");
385 checkTooltip(view, { lines: [{ label: "Total", value: "8" }] }, 1);
386 });
387
388 test("line chart rendering (one groupBy)", async () => {
389 const view = await mountView({
390 type: "graph",
391 resModel: "foo",
392 arch: /* xml */ `
393 <graph type="line">
394 <field name="bar" />
395 </graph>
396 `,
397 });
398
399 expect(".o_graph_canvas_container canvas").toHaveCount(1);
400 checkLabels(view, ["false", "true"]);
401 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label"], {
402 backgroundColor: "#a7d3f9",
403 borderColor: "#4EA7F2",
404 data: [5, 3],
405 label: "Count",
406 });
407 checkLegend(view, "Count");
408 checkTooltip(view, { lines: [{ label: "false", value: "5" }] }, 0);
409 checkTooltip(view, { lines: [{ label: "true", value: "3" }] }, 1);
410 });
411
412 test("line chart rendering (two groupBy)", async () => {
413 const view = await mountView({
414 type: "graph",
415 resModel: "foo",
416 arch: /* xml */ `
417 <graph type="line" stacked="0">
418 <field name="bar" />
419 <field name="product_id" />
420 </graph>
421 `,
422 });
423
424 expect(".o_graph_canvas_container canvas").toHaveCount(1);
425 checkLabels(view, ["false", "true"]);
426 checkDatasets(
427 view,
428 ["backgroundColor", "borderColor", "data", "label"],
429 [
430 {
431 backgroundColor: "#a7d3f9",
432 borderColor: "#4EA7F2",
433 data: [1, 3],
434 label: "xphone",
435 },
436 {
437 backgroundColor: "#f5b0ba",
438 borderColor: "#EA6175",
439 data: [4, 0],
440 label: "xpad",
441 },
442 ]
443 );
444 checkLegend(view, ["xphone", "xpad"]);
445 checkTooltip(
446 view,
447 {
448 lines: [
449 { label: "false / xpad", value: "4" },
450 { label: "false / xphone", value: "1" },
451 ],
452 },
453 0
454 );
455 checkTooltip(
456 view,
457 {
458 lines: [
459 { label: "true / xphone", value: "3" },
460 { label: "true / xpad", value: "0" },
461 ],
462 },
463 1
464 );
465 });
466
467 test("line chart many2many groupBy", async () => {
468 const view = await mountView({
469 type: "graph",
470 resModel: "foo",
471 arch: /* xml */ `
472 <graph type="line">
473 <field name="revenue" type="measure" />
474 <field name="color_ids" />
475 </graph>
476 `,
477 });
478
479 expect(".o_graph_canvas_container canvas").toHaveCount(1);
480 checkLabels(view, ["black", "red"]);
481 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label"], {
482 backgroundColor: "#a7d3f9",
483 borderColor: "#4EA7F2",
484 data: [10, 13],
485 label: "Revenue",
486 });
487 checkLegend(view, "Revenue");
488 checkTooltip(view, { lines: [{ label: "black", value: "10" }], title: "Revenue" }, 0);
489 checkTooltip(view, { lines: [{ label: "red", value: "13" }], title: "Revenue" }, 1);
490 });
491
492 test("Check if values in tooltip are correctly sorted when groupBy filter are applied", async () => {
493 Foo._records = [
494 { product_id: 100, foo: 1, revenue: 12 },
495 { product_id: 100, foo: 2, revenue: 5 },
496 { product_id: 100, foo: 3, revenue: 1.45e2 },
497 { product_id: 100, foo: 4, revenue: -9 },
498 { product_id: 200, foo: 5, revenue: 0 },
499 { product_id: 200, foo: 6, revenue: -1 },
500 { product_id: 200, foo: 7, revenue: Math.PI },
501 { product_id: 200, foo: 8, revenue: 80.67 },
502 ];
503
504 const view = await mountView({
505 type: "graph",
506 resModel: "foo",
507 arch: `
508 <graph type="line" stacked="0">
509 <field name="revenue" type="measure"/>
510 <field name="product_id"/>
511 <field name="foo"/>
512 </graph>
513 `,
514 });
515 checkTooltip(
516 view,
517 {
518 lines: [
519 { label: "xphone / 3", value: "145.00" },
520 { label: "xphone / 1", value: "12.00" },
521 { label: "xphone / 2", value: "5.00" },
522 { label: "xphone / 5", value: "0.00" },
523 { label: "xphone / 6", value: "0.00" },
524 { label: "xphone / 7", value: "0.00" },
525 { label: "xphone / 8", value: "0.00" },
526 { label: "xphone / 4", value: "-9.00" },
527 ],
528 title: "Revenue",
529 },
530 0
531 );
532 checkTooltip(
533 view,
534 {
535 lines: [
536 { label: "xpad / 8", value: "80.67" },
537 { label: "xpad / 7", value: "3.14" },
538 { label: "xpad / 1", value: "0.00" },
539 { label: "xpad / 2", value: "0.00" },
540 { label: "xpad / 3", value: "0.00" },
541 { label: "xpad / 4", value: "0.00" },
542 { label: "xpad / 5", value: "0.00" },
543 { label: "xpad / 6", value: "-1.00" },
544 ],
545 title: "Revenue",
546 },
547 1
548 );
549 });
550
551 test("format total in hh:mm when measure is unit_amount", async () => {
552 Foo._fields.unit_amount = fields.Float({ string: "Unit Amount" });
553 Foo._records = [{ id: 1, unit_amount: 8 }];
554
555 const view = await mountView({
556 type: "graph",
557 resModel: "foo",
558 arch: /* xml */ `
559 <graph>
560 <field name="unit_amount" type="measure" widget="float_time" />
561 </graph>
562 `,
563 });
564
565 const { measure, fieldAttrs } = getGraphModelMetaData(view);
566
567 expect(".o_graph_view").toHaveClass("o_view_controller");
568 expect("div.o_graph_canvas_container canvas").toHaveCount(1);
569 expect(measure).toBe("unit_amount", { message: `the measure should be "unit_amount"` });
570 checkLegend(view, "Unit Amount");
571 checkLabels(view, ["Total"]);
572 expect(fieldAttrs[measure].widget).toBe("float_time", {
573 message: "should be a float_time widget",
574 });
575 checkYTicks(view, [
576 "00:00",
577 "01:00",
578 "02:00",
579 "03:00",
580 "04:00",
581 "05:00",
582 "06:00",
583 "07:00",
584 "08:00",
585 ]);
586 checkTooltip(view, { title: "Unit Amount", lines: [{ label: "Total", value: "08:00" }] }, 0);
587 });
588
589 test("Stacked button visible in the line chart", async () => {
590 const view = await mountView({
591 type: "graph",
592 resModel: "foo",
593 arch: /* xml */ `
594 <graph type="line">
595 <field name="bar" />
596 <field name="product_id" />
597 </graph>
598 `,
599 });
600 const model = getGraphModel(view);
601
602 await selectMode("line");
603
604 checkModeIs(view, "line");
605 expect(model.metaData.stacked).toBe(true, { message: "graph should be stacked." });
606 expect(getScaleY(view).stacked).toBe(true, {
607 message: "The y axes should have stacked property set to true",
608 });
609 expect(`button.o_graph_button[data-tooltip="Stacked"]`).toHaveCount(1);
610
611 await contains(`button.o_graph_button[data-tooltip="Stacked"]`).click();
612
613 expect(model.metaData.stacked).toBe(false, {
614 message: "graph should be a classic line chart.",
615 });
616 expect(getScaleY(view).stacked).toBe(undefined, {
617 message: "The y axes should have stacked property set to undefined",
618 });
619 });
620
621 test("Stacked line prop click false", async () => {
622 const view = await mountView({
623 type: "graph",
624 resModel: "foo",
625 arch: /* xml */ `
626 <graph type="line">
627 <field name="bar" />
628 <field name="product_id" />
629 </graph>
630 `,
631 });
632
633 await contains(`button.o_graph_button[data-tooltip="Stacked"]`).click();
634
635 expect(getGraphModel(view).metaData.stacked).toBe(false, {
636 message: "graph should be a classic line chart.",
637 });
638 expect(!!getScaleY(view).stacked).toBe(false, {
639 message:
640 "the y axes should have a stacked property set to false since the stacked property in line chart is false.",
641 });
642 expect(getGraphRenderer(view).getElementOptions().line.fill).toBe(false, {
643 message: "The fill property should be false since the stacked property is false.",
644 });
645
646 const expectedDatasets = [
647 {
648 backgroundColor: "#a7d3f9",
649 borderColor: "#4EA7F2",
650 pointBackgroundColor: "#4EA7F2",
651 },
652 {
653 backgroundColor: "#f5b0ba",
654 borderColor: "#EA6175",
655 pointBackgroundColor: "#EA6175",
656 },
657 ];
658 const keysToEvaluate = ["backgroundColor", "borderColor", "pointBackgroundColor"];
659 checkDatasets(view, keysToEvaluate, expectedDatasets);
660 });
661
662 test("Stacked prop and default line chart", async () => {
663 const view = await mountView({
664 type: "graph",
665 resModel: "foo",
666 arch: /* xml */ `
667 <graph type="line">
668 <field name="bar" />
669 <field name="product_id" />
670 </graph>
671 `,
672 });
673
674 expect(getGraphModel(view).metaData.mode).toBe("line", {
675 message: "should be in line chart mode.",
676 });
677 expect(getGraphModel(view).metaData.stacked).toBe(true, {
678 message: "should be stacked by default.",
679 });
680
681 expect(getScaleY(view).stacked).toBe(true, {
682 message:
683 "the stacked property in y axes should be true when the stacked is enabled in line chart",
684 });
685 expect(getGraphRenderer(view).getElementOptions().line.fill).toBe(true, {
686 message: "The fill property should be true to add backgroundColor in line chart.",
687 });
688
689 const expectedDatasets = [];
690 const keysToEvaluate = ["backgroundColor", "borderColor", "pointBackgroundColor"];
691 const datasets = getChart(view).data.datasets;
692 const colors = getColors(undefined, "sm");
693 for (let i = 0; i < datasets.length; i++) {
694 const expectedColor = colors[i];
695 expectedDatasets.push({
696 backgroundColor: lightenColor(expectedColor, 0.5),
697 borderColor: expectedColor,
698 pointBackgroundColor: expectedColor,
699 });
700 }
701 checkDatasets(view, keysToEvaluate, expectedDatasets);
702 });
703
704 test("Cumulative prop and default line chart", async () => {
705 const view = await mountView({
706 type: "graph",
707 resModel: "foo",
708 arch: /* xml */ `
709 <graph type="line" stacked="0">
710 <field name="bar" />
711 <field name="product_id" />
712 </graph>
713 `,
714 });
715
716 expect(getGraphModel(view).metaData.mode).toBe("line", {
717 message: "should be in line chart mode.",
718 });
719 expect(getGraphModel(view).metaData.cumulated).toBe(false, {
720 message: "should not be cumulative by default.",
721 });
722
723 await contains('[data-tooltip="Cumulative"]').click();
724
725 expect(getGraphModel(view).metaData.cumulated).toBe(true, {
726 message: "should be in cumulative",
727 });
728 const expectedDatasets = [
729 {
730 data: [1, 4],
731 },
732 {
733 data: [4, 4],
734 },
735 ];
736 checkDatasets(view, ["data"], expectedDatasets);
737 });
738
739 test("Default cumulative prop", async () => {
740 const view = await mountView({
741 type: "graph",
742 resModel: "foo",
743 arch: /* xml */ `
744 <graph type="line" stacked="0" cumulated="1">
745 <field name="bar" />
746 <field name="product_id" />
747 </graph>
748 `,
749 });
750
751 expect(getGraphModel(view).metaData.mode).toBe("line", {
752 message: "should be in line chart mode.",
753 });
754 expect(getGraphModel(view).metaData.cumulated).toBe(true, {
755 message: "should be in cumulative",
756 });
757 expect(getGraphModel(view).metaData.cumulatedStart).toBe(false, {
758 message: "should have cumulated start opted-out",
759 });
760 });
761
762 test("Cumulative prop and cumulated start", async () => {
763 const view = await mountView({
764 type: "graph",
765 resModel: "foo",
766 arch: /* xml */ `
767 <graph type="line" stacked="0" cumulated="1" cumulated_start="1">
768 <field name="date" />
769 <field name="product_id" />
770 </graph>
771 `,
772 searchViewArch: /* xml */ `
773 <search>
774 <filter name="filter_after_march"
775 string="After March 2016"
776 domain="[['date', '>=', '2016-03-01']]"
777 />
778 </search>
779 `,
780 context: {
781 search_default_filter_after_march: 1,
782 },
783 });
784
785 expect(getGraphModel(view).metaData.mode).toBe("line", {
786 message: "should be in line chart mode",
787 });
788 expect(getGraphModel(view).metaData.cumulated).toBe(true, {
789 message: "should be in cumulative",
790 });
791 expect(getGraphModel(view).metaData.cumulatedStart).toBe(true, {
792 message: "should have cumulated start opted-in",
793 });
794
795 const expectedDatasets = [
796 {
797 data: [4, 4, 4],
798 },
799 {
800 data: [0, 1, 2],
801 },
802 ];
803 checkDatasets(view, ["data"], expectedDatasets);
804 });
805
806 test("displaying line chart with only 1 data point", async () => {
807 // this test makes sure the line chart does not crash when only one data
808 // point is displayed.
809 Foo._records = Foo._records.filter((id) => id === 1);
810
811 await mountView({
812 type: "graph",
813 resModel: "foo",
814 arch: /* xml */ `<graph type="line" stacked="0" />`,
815 });
816
817 expect("canvas").toHaveCount(1);
818 });
819
820 test("pie chart rendering (no groupBy)", async () => {
821 const view = await mountView({
822 type: "graph",
823 resModel: "foo",
824 arch: /* xml */ `<graph type="pie" />`,
825 });
826
827 expect(".o_graph_canvas_container canvas").toHaveCount(1);
828 expect(getGraphModelMetaData(view).mode).toBe("pie");
829 checkLabels(view, ["Total"]);
830 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label", "stack"], {
831 backgroundColor: ["#4EA7F2"],
832 borderColor: getBorderWhite(),
833 data: [8],
834 label: "",
835 stack: undefined,
836 });
837 checkLegend(view, "Total");
838 checkTooltip(view, { lines: [{ label: "Total", value: "8 (100.00%)" }] }, 0);
839 });
840
841 test("pie chart rendering (one groupBy)", async () => {
842 const view = await mountView({
843 type: "graph",
844 resModel: "foo",
845 arch: /* xml */ `
846 <graph type="pie">
847 <field name="bar" />
848 </graph>
849 `,
850 });
851
852 expect(".o_graph_canvas_container canvas").toHaveCount(1);
853 checkLabels(view, ["false", "true"]);
854 checkDatasets(view, ["backgroundColor", "borderColor", "data"], {
855 backgroundColor: ["#4EA7F2", "#EA6175"],
856 borderColor: getBorderWhite(),
857 data: [5, 3],
858 });
859 checkLegend(view, ["false", "true"]);
860 checkTooltip(view, { lines: [{ label: "false", value: "5 (62.50%)" }] }, 0);
861 checkTooltip(view, { lines: [{ label: "true", value: "3 (37.50%)" }] }, 1);
862 });
863
864 test("pie chart many2many groupby", async () => {
865 const view = await mountView({
866 type: "graph",
867 resModel: "foo",
868 arch: /* xml */ `
869 <graph type="pie">
870 <field name="revenue" type="measure" />
871 <field name="color_ids" />
872 </graph>
873 `,
874 });
875
876 expect(".o_graph_canvas_container canvas").toHaveCount(1);
877 checkLabels(view, ["black", "red", "None"]);
878 checkDatasets(view, ["backgroundColor", "borderColor", "data"], {
879 backgroundColor: ["#4EA7F2", "#EA6175", "#43C5B1"],
880 borderColor: getBorderWhite(),
881 data: [10, 13, 8],
882 });
883 checkLegend(view, ["black", "red", "None"]);
884 checkTooltip(view, { lines: [{ label: "black", value: "10 (32.26%)" }], title: "Revenue" }, 0);
885 checkTooltip(view, { lines: [{ label: "red", value: "13 (41.94%)" }], title: "Revenue" }, 1);
886 checkTooltip(view, { lines: [{ label: "None", value: "8 (25.81%)" }], title: "Revenue" }, 2);
887 });
888
889 test("pie chart rendering (two groupBy)", async () => {
890 const view = await mountView({
891 type: "graph",
892 resModel: "foo",
893 arch: /* xml */ `
894 <graph type="pie">
895 <field name="bar" />
896 <field name="product_id" />
897 </graph>
898 `,
899 });
900
901 expect(".o_graph_canvas_container canvas").toHaveCount(1);
902 checkLabels(view, ["false / xphone", "false / xpad", "true / xphone"]);
903 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label"], {
904 backgroundColor: ["#4EA7F2", "#EA6175", "#43C5B1"],
905 borderColor: getBorderWhite(),
906 data: [1, 4, 3],
907 label: "",
908 });
909 checkLegend(view, ["false / xphone", "false / xpad", "true / xphone"]);
910 checkTooltip(view, { lines: [{ label: "false / xphone", value: "1 (12.50%)" }] }, 0);
911 checkTooltip(view, { lines: [{ label: "false / xpad", value: "4 (50.00%)" }] }, 1);
912 checkTooltip(view, { lines: [{ label: "true / xphone", value: "3 (37.50%)" }] }, 2);
913 });
914
915 test("pie chart rendering (no data)", async () => {
916 Foo._records = [];
917
918 const view = await mountView({
919 type: "graph",
920 resModel: "foo",
921 arch: /* xml */ `<graph type="pie" />`,
922 });
923
924 checkLabels(view, ["No data"]);
925 checkDatasets(
926 view,
927 ["backgroundColor", "borderColor", "data", "label"],
928 [
929 {
930 backgroundColor: [DEFAULT_BG],
931 borderColor: getBorderWhite(),
932 data: [1],
933 label: "",
934 },
935 ]
936 );
937 checkLegend(view, ["No data"]);
938 checkTooltip(view, { lines: [{ label: "No data", value: "0 (100.00%)" }] }, 0);
939 });
940
941 test("pie chart rendering (mix of positive and negative values)", async () => {
942 Foo._records = [
943 { bar: true, revenue: 2 },
944 { bar: false, revenue: -3 },
945 ];
946
947 const view = await mountView({
948 type: "graph",
949 resModel: "foo",
950 arch: /* xml */ `
951 <graph type="pie">
952 <field name="revenue" type="measure" />
953 <field name="bar" />
954 </graph>
955 `,
956 });
957
958 expect(".o_view_nocontent").toHaveCount(0);
959 expect(".o_graph_canvas_container").toHaveCount(1);
960 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label", "stack"], {
961 backgroundColor: ["#4EA7F2"],
962 borderColor: getBorderWhite(),
963 data: [2],
964 label: "",
965 stack: undefined,
966 });
967 });
968
969 test("pie chart toggling dataset hides label", async () => {
970 const view = await mountView({
971 type: "graph",
972 resModel: "foo",
973 arch: `<graph type="pie"/>`,
974 });
975 checkLabels(view, ["Total"]);
976 await clickOnLegend(view, "Total");
977 expect(getChart(view).legend.legendItems[0].hidden).toBe(true);
978 });
979
980 test("mode props", async () => {
981 const view = await mountView({
982 type: "graph",
983 resModel: "foo",
984 arch: /* xml */ `<graph type="pie" />`,
985 });
986
987 expect(getGraphModelMetaData(view).mode).toBe("pie", {
988 message: "should be in pie chart mode",
989 });
990 expect(getChart(view).config.type).toBe("pie");
991 });
992
993 test("field id not in groupBy", async () => {
994 const view = await mountView({
995 type: "graph",
996 resModel: "foo",
997 arch: /* xml */ `
998 <graph>
999 <field name="id" />
1000 </graph>
1001 `,
1002 });
1003
1004 checkLabels(view, ["Total"]);
1005 checkDatasets(view, ["backgroundColor", "data", "label", "stack"], {
1006 backgroundColor: "#4EA7F2",
1007 data: [8],
1008 label: "Count",
1009 stack: "",
1010 });
1011 checkLegend(view, "Count");
1012 });
1013
1014 test("props modifications", async () => {
1015 const view = await mountView({
1016 type: "graph",
1017 resModel: "foo",
1018 arch: /* xml */ `
1019 <graph>
1020 <field name="bar" />
1021 </graph>
1022 `,
1023 searchViewArch: /* xml */ `
1024 <search>
1025 <filter name="group_by_color" string="Color" context="{ 'group_by': 'color_id' }" />
1026 </search>
1027 `,
1028 });
1029
1030 checkModeIs(view, "bar");
1031 expect(getYAxisLabel(view)).toBe("Count");
1032
1033 await selectMode("line");
1034
1035 checkModeIs(view, "line");
1036
1037 await toggleMenu("Measures");
1038 await toggleMenuItem("Revenue");
1039
1040 expect(getYAxisLabel(view)).toBe("Revenue");
1041
1042 await toggleSearchBarMenu();
1043 await toggleMenuItem("Color");
1044
1045 checkModeIs(view, "line");
1046 expect(getYAxisLabel(view)).toBe("Revenue");
1047 });
1048
1049 test("switching mode", async () => {
1050 const view = await mountView({ type: "graph", resModel: "foo" });
1051
1052 checkModeIs(view, "bar");
1053
1054 await selectMode("bar"); // click on the active mode does not change anything
1055
1056 checkModeIs(view, "bar");
1057
1058 await selectMode("line");
1059
1060 checkModeIs(view, "line");
1061
1062 await selectMode("pie");
1063
1064 checkModeIs(view, "pie");
1065 });
1066
1067 test("switching measure", async () => {
1068 const checkMeasure = (measure) => {
1069 const yAxe = getChart(view).config.options.scales.y;
1070 expect(yAxe.title.text).toBe(measure);
1071 expect(`.o_menu_item:contains(${measure})`).toHaveClass("selected");
1072 };
1073
1074 const view = await mountView({ type: "graph", resModel: "foo" });
1075
1076 await toggleMenu("Measures");
1077
1078 checkMeasure("Count");
1079 checkLegend(view, "Count");
1080
1081 await toggleMenuItem("Foo");
1082
1083 checkMeasure("Foo");
1084 checkLegend(view, "Foo");
1085 });
1086
1087 test("process default view description", async () => {
1088 expect(new GraphArchParser().parse()).toEqual({
1089 fields: {},
1090 fieldAttrs: {},
1091 groupBy: [],
1092 measures: [],
1093 });
1094 });
1095
1096 test("process simple arch (no field tag)", async () => {
1097 const { env } = await makeMockServer();
1098 const fooFields = env["foo"]._fields;
1099
1100 const arch1 = /* xml */ `
1101 <graph order="ASC" disable_linking="1" type="line" />
1102 `;
1103
1104 expect(new GraphArchParser().parse(arch1, fooFields)).toEqual({
1105 disableLinking: true,
1106 fields: fooFields,
1107 fieldAttrs: {},
1108 groupBy: [],
1109 measures: [],
1110 mode: "line",
1111 order: "ASC",
1112 });
1113
1114 const arch2 = /* xml */ `
1115 <graph disable_linking="0" string="Title" stacked="False" />
1116 `;
1117
1118 expect(new GraphArchParser().parse(arch2, fooFields)).toEqual({
1119 disableLinking: false,
1120 fields: fooFields,
1121 fieldAttrs: {},
1122 groupBy: [],
1123 measures: [],
1124 stacked: false,
1125 title: "Title",
1126 });
1127 });
1128
1129 test("process arch with field tags", async () => {
1130 Foo._fields.fighters = fields.Text();
1131
1132 const { env } = await makeMockServer();
1133 const fooFields = env["foo"]._fields;
1134
1135 const arch = /* xml */ `
1136 <graph type="pie">
1137 <field name="revenue" type="measure" />
1138 <field name="date" interval="day" />
1139 <field name="foo" invisible="False" />
1140 <field name="bar" invisible="True" string="My invisible field" />
1141 <field name="id" />
1142 <field name="fighters" string="FooFighters" />
1143 </graph>
1144 `;
1145
1146 expect(new GraphArchParser().parse(arch, fooFields)).toEqual({
1147 fields: fooFields,
1148 fieldAttrs: {
1149 bar: { isInvisible: true, string: "My invisible field" },
1150 fighters: { string: "FooFighters" },
1151 },
1152 measure: "revenue",
1153 measures: ["revenue"],
1154 groupBy: ["date:day", "foo"],
1155 mode: "pie",
1156 });
1157 });
1158
1159 test("process arch with non stored field tags of type measure", async () => {
1160 Foo._fields.revenue.store = false;
1161
1162 const { env } = await makeMockServer();
1163 const fooFields = env["foo"]._fields;
1164 const arch = `
1165 <graph>
1166 <field name="product_id"/>
1167 <field name="revenue" type="measure"/>
1168 <field name="foo" type="measure"/>
1169 </graph>
1170 `;
1171 expect(new GraphArchParser().parse(arch, fooFields)).toEqual({
1172 fields: fooFields,
1173 fieldAttrs: {},
1174 measure: "foo",
1175 measures: ["revenue", "foo"],
1176 groupBy: ["product_id"],
1177 });
1178 });
1179
1180 test("displaying chart data with three groupbys", async () => {
1181 // this test makes sure the line chart shows all data labels (X axis) when
1182 // it is grouped by several fields
1183 const view = await mountView({
1184 type: "graph",
1185 resModel: "foo",
1186 arch: /* xml */ `
1187 <graph type="bar">
1188 <field name="product_id" />
1189 <field name="bar" />
1190 <field name="color_id" />
1191 </graph>
1192 `,
1193 });
1194
1195 checkLabels(view, ["xphone", "xpad"]);
1196 checkLegend(view, ["false / None", "true / red", "true / None", "Sum"]);
1197
1198 await selectMode("line");
1199
1200 checkLabels(view, ["xphone", "xpad"]);
1201 checkLegend(view, ["false / None", "true / red", "true / None"]);
1202
1203 await selectMode("pie");
1204
1205 checkLabels(view, [
1206 "xphone / false / None",
1207 "xphone / true / red",
1208 "xphone / true / None",
1209 "xpad / false / None",
1210 ]);
1211 checkLegend(view, [
1212 "xphone / false / None",
1213 "xphone / true / red",
1214 "xphone / true / None",
1215 "xpad / false / None",
1216 ]);
1217 });
1218
1219 test("no content helper", async () => {
1220 Foo._records = [];
1221
1222 await mountView({
1223 type: "graph",
1224 resModel: "foo",
1225 noContentHelp: /* xml */ `
1226 <p class="abc">This helper should not be displayed in graph views</p>
1227 `,
1228 });
1229
1230 expect(".o_graph_canvas_container canvas").toHaveCount(1);
1231 expect(".o_view_nocontent").toHaveCount(0);
1232 expect(".abc").toHaveCount(0);
1233 });
1234
1235 test("no content helper after update", async () => {
1236 await mountView({
1237 type: "graph",
1238 resModel: "foo",
1239 noContentHelp: /* xml */ `
1240 <p class="abc">This helper should not be displayed in graph views</p>
1241 `,
1242 config: {
1243 views: [[false, "search"]],
1244 },
1245 });
1246
1247 expect(".o_graph_canvas_container canvas").toHaveCount(1);
1248 expect(".o_view_nocontent").toHaveCount(0);
1249 expect(".abc").toHaveCount(0);
1250
1251 await toggleSearchBarMenu();
1252 await toggleMenuItem("False Domain");
1253
1254 expect(".o_graph_canvas_container canvas").toHaveCount(1);
1255 expect(".o_view_nocontent").toHaveCount(0);
1256 expect(".abc").toHaveCount(0);
1257 });
1258
1259 test("display the provided no content helper when search has no matching data", async () => {
1260 Foo._records = [];
1261
1262 await mountView({
1263 type: "graph",
1264 resModel: "foo",
1265 noContentHelp: /* xml */ `
1266 <p class="abc">This helper should be displayed</p>
1267 `,
1268 });
1269
1270 expect(".o_graph_canvas_container canvas").toHaveCount(1);
1271 expect(".o_view_nocontent").toHaveCount(0);
1272
1273 await toggleSearchBarMenu();
1274 await toggleMenuItem("color");
1275
1276 expect(".o_graph_canvas_container canvas").toHaveCount(0);
1277 expect(".o_nocontent_help:contains(This helper should be displayed)").toHaveCount(1);
1278 });
1279
1280 test("can reload with other group by", async () => {
1281 const view = await mountView({
1282 type: "graph",
1283 resModel: "foo",
1284 arch: /* xml */ `
1285 <graph>
1286 <field name="product_id" />
1287 </graph>
1288 `,
1289 searchViewArch: /* xml */ `
1290 <search>
1291 <filter name="group_by_color" string="Color" context="{ 'group_by': 'color_id' }" />
1292 </search>
1293 `,
1294 });
1295
1296 checkLabels(view, ["xphone", "xpad"]);
1297
1298 await toggleSearchBarMenu();
1299 await toggleMenuItem("Color");
1300
1301 checkLabels(view, ["red", "None"]);
1302 });
1303
1304 test("save params succeeds", async () => {
1305 expect.assertions(4);
1306
1307 const expectedContexts = [
1308 {
1309 graph_mode: "bar",
1310 graph_measure: "__count",
1311 graph_groupbys: ["product_id"],
1312 graph_order: null,
1313 graph_stacked: true,
1314 group_by: [],
1315 },
1316 {
1317 graph_mode: "bar",
1318 graph_measure: "foo",
1319 graph_groupbys: ["product_id"],
1320 graph_order: null,
1321 graph_stacked: true,
1322 group_by: [],
1323 },
1324 {
1325 graph_mode: "line",
1326 graph_measure: "foo",
1327 graph_cumulated: false,
1328 graph_groupbys: ["product_id"],
1329 graph_order: null,
1330 graph_stacked: true,
1331 group_by: [],
1332 },
1333 {
1334 graph_mode: "line",
1335 graph_measure: "foo",
1336 graph_cumulated: false,
1337 graph_groupbys: ["product_id", "color_id"],
1338 graph_order: null,
1339 graph_stacked: true,
1340 group_by: ["product_id", "color_id"],
1341 },
1342 ];
1343
1344 let serverId = 1;
1345 onRpc("create_filter", ({ args }) => {
1346 expect(args[0].context).toEqual(expectedContexts.shift());
1347 return [serverId++];
1348 });
1349
1350 await mountView({
1351 resModel: "foo",
1352 type: "graph",
1353 arch: /* xml */ `
1354 <graph>
1355 <field name="product_id" />
1356 </graph>
1357 `,
1358 searchViewId: false,
1359 searchViewArch: /* xml */ `
1360 <search>
1361 <filter name="false_domain" string="False Domain" domain="[(0, '=', 1)]" />
1362 <filter name="filter_with_context"
1363 string="Filter With Context"
1364 domain="[]"
1365 context="{ 'graph_measure': 'foo', 'graph_mode': 'line', 'graph_groupbys': ['color_id'] }"
1366 />
1367 <filter name="group_by_color" string="Color" context="{ 'group_by': 'color_id' }" />
1368 <filter name="group_by_product" string="Product" context="{ 'group_by': 'product_id' }" />
1369 </search>
1370 `,
1371 });
1372
1373 await toggleSaveFavorite();
1374 await editFavoriteName("First Favorite");
1375 await saveFavorite();
1376
1377 await toggleMenu("Measures");
1378 await toggleMenuItem("Foo");
1379
1380 await toggleSaveFavorite();
1381 await editFavoriteName("Second Favorite");
1382 await saveFavorite();
1383
1384 await selectMode("line");
1385
1386 await toggleSaveFavorite();
1387 await editFavoriteName("Third Favorite");
1388 await saveFavorite();
1389
1390 await toggleMenuItem("Product");
1391 await toggleMenuItem("Color");
1392
1393 await editFavoriteName("Fourth Favorite");
1394 await saveFavorite();
1395 });
1396
1397 test("correctly uses graph_ keys from the context", async () => {
1398 Foo._records.at(-1).color_id = 1;
1399
1400 const view = await mountView({
1401 type: "graph",
1402 resModel: "foo",
1403 arch: /* xml */ `
1404 <graph>
1405 <field name="product_id" />
1406 </graph>
1407 `,
1408 context: {
1409 graph_measure: "foo",
1410 graph_mode: "line",
1411 graph_groupbys: ["color_id"],
1412 },
1413 });
1414
1415 checkLabels(view, ["black", "red"]);
1416 checkLegend(view, "Foo");
1417 checkModeIs(view, "line");
1418 expect(getYAxisLabel(view)).toBe("Foo");
1419 expect(getGraphModelMetaData(view).mode).toBe("line");
1420 });
1421
1422 test("correctly uses graph_ keys from the context (at reload)", async () => {
1423 const view = await mountView({
1424 type: "graph",
1425 resModel: "foo",
1426 arch: /* xml */ `
1427 <graph>
1428 <field name="product_id" />
1429 </graph>
1430 `,
1431 searchViewArch: /* xml */ `
1432 <search>
1433 <filter name="context" domain="[]" string="Context" context="{ 'graph_measure': 'foo', 'graph_mode': 'line' }" />
1434 </search>
1435 `,
1436 });
1437
1438 checkLegend(view, "Count");
1439 expect(getYAxisLabel(view)).toBe("Count");
1440 checkModeIs(view, "bar");
1441
1442 await toggleSearchBarMenu();
1443 await toggleMenuItem("Context");
1444
1445 checkLegend(view, "Foo");
1446 expect(getYAxisLabel(view)).toBe("Foo");
1447 checkModeIs(view, "line");
1448 });
1449
1450 test("correctly use group_by key from the context", async () => {
1451 Foo._records.at(-1).color_id = 1;
1452
1453 const view = await mountView({
1454 type: "graph",
1455 resModel: "foo",
1456 arch: /* xml */ `
1457 <graph>
1458 <field name="product_id" />
1459 </graph>
1460 `,
1461 searchViewArch: /* xml */ `
1462 <search>
1463 <filter name="filter_with_context"
1464 string="Filter With Context"
1465 domain="[]"
1466 context="{ 'graph_measure': 'foo', 'graph_mode': 'line', 'graph_groupbys': ['color_id'] }"
1467 />
1468 </search>
1469 `,
1470 context: {
1471 search_default_filter_with_context: 1,
1472 },
1473 });
1474
1475 checkLabels(view, ["black", "red"]);
1476 checkLegend(view, "Foo");
1477 checkModeIs(view, "line");
1478 expect(getYAxisLabel(view)).toBe("Foo");
1479 expect(getGraphModelMetaData(view).mode).toBe("line");
1480 });
1481
1482 test("an invisible field should not be used as groupBy", async () => {
1483 const view = await mountView({
1484 type: "graph",
1485 resModel: "foo",
1486 arch: /* xml */ `
1487 <graph>
1488 <field name="foo" invisible="1" />
1489 </graph>
1490 `,
1491 });
1492
1493 checkLabels(view, ["Total"]);
1494 });
1495
1496 test("format values as float in case at least one value is not an number", async () => {
1497 Foo._records = [
1498 { bar: false, revenue: 1.5 },
1499 { bar: true, revenue: 2 },
1500 ];
1501
1502 const view = await mountView({
1503 type: "graph",
1504 resModel: "foo",
1505 arch: /* xml */ `
1506 <graph>
1507 <field name="revenue" type="measure" />
1508 <field name="bar" />
1509 </graph>
1510 `,
1511 });
1512
1513 checkDatasets(view, "data", { data: [1.5, 2] });
1514 checkLabels(view, ["false", "true"]);
1515 checkTooltip(view, { title: "Revenue", lines: [{ label: "false", value: "1.50" }] }, 0);
1516 checkTooltip(view, { title: "Revenue", lines: [{ label: "true", value: "2.00" }] }, 1);
1517 });
1518
1519 test("the active measure description is the arch string attribute in priority", async () => {
1520 const view = await mountView({
1521 type: "graph",
1522 resModel: "foo",
1523 arch: /* xml */ `
1524 <graph>
1525 <field name="revenue" type="measure" string="Nirvana" />
1526 <field name="foo" type="measure" string="FooFighters" />
1527 </graph>
1528 `,
1529 });
1530
1531 checkTooltip(view, { title: "FooFighters", lines: [{ label: "Total", value: "239" }] }, 0);
1532
1533 await toggleMenu("Measures");
1534 await toggleMenuItem("Nirvana");
1535
1536 checkTooltip(view, { title: "Nirvana", lines: [{ label: "Total", value: "23" }] }, 0);
1537 });
1538
1539 test("reload graph with correct fields", async () => {
1540 expect.assertions(2);
1541
1542 onRpc("formatted_read_group", ({ kwargs }) => {
1543 expect(kwargs.aggregates).toEqual(["__count", "foo:sum"]);
1544 });
1545
1546 await mountView({
1547 type: "graph",
1548 resModel: "foo",
1549 arch: /* xml */ `
1550 <graph>
1551 <field name="foo" type="measure" />
1552 </graph>
1553 `,
1554 searchViewArch: /* xml */ `
1555 <search>
1556 <filter name="false_domain" string="False Domain" domain="[(0, '=', 1)]" />
1557 </search>
1558 `,
1559 });
1560
1561 await toggleSearchBarMenu();
1562 await toggleMenuItem("False Domain");
1563 });
1564
1565 test("initial groupby is kept when reloading", async () => {
1566 expect.assertions(7);
1567
1568 onRpc("formatted_read_group", ({ kwargs }) => {
1569 expect(kwargs.groupby).toEqual(["product_id"]);
1570 });
1571 const view = await mountView({
1572 type: "graph",
1573 resModel: "foo",
1574 arch: /* xml */ `
1575 <graph>
1576 <field name="product_id" />
1577 <field name="foo" type="measure" />
1578 </graph>
1579 `,
1580 searchViewArch: /* xml */ `
1581 <search>
1582 <filter name="false_domain" string="False Domain" domain="[(0, '=', 1)]" />
1583 </search>
1584 `,
1585 });
1586
1587 checkLabels(view, ["xphone", "xpad"]);
1588 checkLegend(view, "Foo");
1589 checkDatasets(view, "data", { data: [82, 157] });
1590 expect(getYAxisLabel(view)).toBe("Foo");
1591
1592 await toggleSearchBarMenu();
1593 await toggleMenuItem("False Domain");
1594 expect(".o_graph_canvas_container").toHaveCount(0);
1595 });
1596
1597 test("use a many2one as a measure should work (without groupBy)", async () => {
1598 const view = await mountView({
1599 type: "graph",
1600 resModel: "foo",
1601 arch: /* xml */ `
1602 <graph>
1603 <field name="product_id" type="measure" />
1604 </graph>
1605 `,
1606 });
1607
1608 checkLabels(view, ["Total"]);
1609 checkLegend(view, "Product");
1610 checkDatasets(view, "data", { data: [2] });
1611 expect(getYAxisLabel(view)).toBe("Product");
1612 });
1613
1614 test("use a many2one as a measure should work (with groupBy)", async () => {
1615 const view = await mountView({
1616 type: "graph",
1617 resModel: "foo",
1618 arch: /* xml */ `
1619 <graph>
1620 <field name="bar" />
1621 <field name="product_id" type="measure" />
1622 </graph>
1623 `,
1624 });
1625
1626 checkLabels(view, ["false", "true"]);
1627 checkLegend(view, "Product");
1628 checkDatasets(view, "data", { data: [2, 1] });
1629 });
1630
1631 test("use a many2one as a measure and as a groupby should work", async () => {
1632 const view = await mountView({
1633 type: "graph",
1634 resModel: "foo",
1635 arch: /* xml */ `
1636 <graph>
1637 <field name="product_id" type="measure" />
1638 <field name="product_id" />
1639 </graph>
1640 `,
1641 });
1642
1643 checkLabels(view, ["xphone", "xpad"]);
1644 checkLegend(view, "Product");
1645 checkDatasets(view, "data", { data: [1, 1] });
1646 expect(getYAxisLabel(view)).toBe("Product");
1647 });
1648
1649 test("differentiate many2one values with same label", async () => {
1650 Product._records.push({ id: 300, name: "xphone" });
1651 Foo._records.push({ product_id: 300 });
1652
1653 const view = await mountView({
1654 type: "graph",
1655 resModel: "foo",
1656 arch: /* xml */ `
1657 <graph>
1658 <field name="product_id" />
1659 </graph>
1660 `,
1661 });
1662
1663 checkLabels(view, ["xphone", "xpad", "xphone (2)"]);
1664 });
1665
1666 test("not use a many2one as a measure by default", async () => {
1667 await mountView({
1668 type: "graph",
1669 resModel: "foo",
1670 viewId: false,
1671 });
1672
1673 await toggleMenu("Measures");
1674
1675 expect(queryAllTexts(".o-dropdown--menu .o_menu_item")).toEqual(["Foo", "Revenue", "Count"]);
1676 });
1677
1678 test.tags("desktop");
1679 test("graph view crash when moving from search view using Down key", async () => {
1680 await mountView({ type: "graph", resModel: "foo" });
1681
1682 await contains(".o_searchview input").press("ArrowDown");
1683
1684 expect(".o_graph_view").toHaveCount(1);
1685 });
1686
1687 test("graph measures should be alphabetically sorted (exception: 'Count' is last)", async () => {
1688 Foo._fields.bouh = fields.Integer();
1689
1690 await mountView({
1691 type: "graph",
1692 resModel: "foo",
1693 arch: /* xml */ `
1694 <graph>
1695 <field name="foo" type="measure" />
1696 <field name="bouh" type="measure" />
1697 </graph>
1698 `,
1699 });
1700
1701 await toggleMenu("Measures");
1702
1703 expect(queryAllTexts(".o-dropdown--menu .o_menu_item")).toEqual([
1704 "Bouh",
1705 "Foo",
1706 "Revenue",
1707 "Count",
1708 ]);
1709 });
1710
1711 test("a many2one field can be added as measure in arch", async () => {
1712 const view = await mountView({
1713 type: "graph",
1714 resModel: "foo",
1715 arch: /* xml */ `
1716 <graph>
1717 <field name="product_id" type="measure" />
1718 </graph>
1719 `,
1720 });
1721
1722 checkLegend(view, "Product");
1723 expect(getYAxisLabel(view)).toBe("Product");
1724 });
1725
1726 test("non store fields defined on the arch are present in the measures", async () => {
1727 Foo._fields.revenue.store = false;
1728 await mountView({
1729 type: "graph",
1730 resModel: "foo",
1731 arch: /* xml */ `
1732 <graph>
1733 <field name="product_id"/>
1734 <field name="revenue" type="measure"/>
1735 <field name="foo" type="measure"/>
1736 </graph>
1737 `,
1738 });
1739
1740 await toggleMenu("Measures");
1741 expect(queryAllTexts(`.o_menu_item`)).toEqual(["Foo", "Revenue", "Count"]);
1742 });
1743
1744 test("graph view `graph_measure` field in context", async () => {
1745 const view = await mountView({
1746 type: "graph",
1747 resModel: "foo",
1748 viewId: false,
1749 context: {
1750 graph_measure: "product_id",
1751 },
1752 });
1753
1754 expect(getYAxisLabel(view)).toBe("Product");
1755 checkLegend(view, "Product");
1756 checkTooltip(view, { title: "Product", lines: [{ label: "Total", value: "2" }] }, 0);
1757 });
1758
1759 test("`graph_measure` in context is prefered to measure in arch", async () => {
1760 const view = await mountView({
1761 type: "graph",
1762 resModel: "foo",
1763 arch: /* xml */ `
1764 <graph>
1765 <field name="revenue" type="measure" />
1766 </graph>
1767 `,
1768 context: {
1769 graph_measure: "product_id",
1770 },
1771 });
1772
1773 expect(getYAxisLabel(view)).toBe("Product");
1774 checkLegend(view, "Product");
1775 checkTooltip(view, { title: "Product", lines: [{ label: "Total", value: "2" }] }, 0);
1776 });
1777
1778 test("None should appear in bar, pie graph but not in line graph with multiple groupbys", async () => {
1779 const view = await mountView({
1780 type: "graph",
1781 resModel: "foo",
1782 arch: /* xml */ `
1783 <graph type="line">
1784 <field name="date" />
1785 <field name="color_id" />
1786 </graph>
1787 `,
1788 });
1789 const someNone = () => getChart(view).data.labels.some((l) => /none/i.test(l));
1790
1791 expect(someNone()).toBe(false);
1792
1793 await selectMode("bar");
1794
1795 expect(someNone()).toBe(true);
1796
1797 await selectMode("pie");
1798
1799 expect(someNone()).toBe(true);
1800
1801 // None should not appear after switching back to line chart
1802 await selectMode("line");
1803
1804 expect(someNone()).toBe(false);
1805 });
1806
1807 test("an invisible field can not be found in the 'Measures' menu", async () => {
1808 const view = await mountView({
1809 type: "graph",
1810 resModel: "foo",
1811 arch: /* xml */ `
1812 <graph>
1813 <field name="revenue" invisible="1" />
1814 </graph>
1815 `,
1816 });
1817
1818 checkTooltip(view, { lines: [{ label: "Total", value: "8" }] }, 0);
1819
1820 await toggleMenu("Measures");
1821
1822 expect(".o_menu_item:contains(Revenue)").toHaveCount(0, {
1823 message: `"Revenue" can not be found in the "Measures" menu`,
1824 });
1825 });
1826
1827 test("graph view only keeps finer groupby filter option for a given groupby", async () => {
1828 const view = await mountView({
1829 type: "graph",
1830 resModel: "foo",
1831 groupBy: ["date:year", "product_id", "date", "date:quarter"],
1832 arch: /* xml */ `<graph type="line" />`,
1833 });
1834
1835 checkLabels(view, ["January 2016", "March 2016", "April 2016", "May 2016"]);
1836 checkLegend(view, ["xphone", "xpad"]);
1837 checkDatasets(
1838 view,
1839 ["label", "data"],
1840 [
1841 {
1842 label: "xphone",
1843 data: [2, 2, 0, 0],
1844 },
1845 {
1846 label: "xpad",
1847 data: [0, 0, 1, 1],
1848 },
1849 ]
1850 );
1851 });
1852
1853 test("action name is displayed in breadcrumbs", async () => {
1854 await mountWithCleanup(WebClient);
1855 await getService("action").doAction({
1856 name: "Glou glou",
1857 res_model: "foo",
1858 type: "ir.actions.act_window",
1859 views: [[false, "graph"]],
1860 });
1861 expect(".o_breadcrumb .active:first").toHaveText("Glou glou");
1862 });
1863
1864 test("clicking on bar charts triggers a do_action", async () => {
1865 expect.assertions(6);
1866
1867 mockService("action", {
1868 doAction(actionRequest, options) {
1869 expect(actionRequest).toEqual({
1870 context: { allowed_company_ids: [1], lang: "en", tz: "taht", uid: 7 },
1871 domain: [["bar", "=", false]],
1872 name: "Foo Analysis",
1873 res_model: "foo",
1874 search_view_id: [67, "search"],
1875 target: "current",
1876 type: "ir.actions.act_window",
1877 views: [
1878 [false, "list"],
1879 [false, "form"],
1880 ],
1881 });
1882 expect(options).toEqual({ newWindow: false, viewType: "list" });
1883 },
1884 });
1885
1886 const view = await mountView({
1887 type: "graph",
1888 resModel: "foo",
1889 searchViewId: 67,
1890 arch: /* xml */ `
1891 <graph string="Foo Analysis">
1892 <field name="bar" />
1893 </graph>
1894 `,
1895 });
1896
1897 checkModeIs(view, "bar");
1898 checkDatasets(view, ["domains"], {
1899 domains: [[["bar", "=", false]], [["bar", "=", true]]],
1900 });
1901
1902 await clickOnDataset(view);
1903 });
1904
1905 test("middle click on bar charts triggers a do_action", async () => {
1906 expect.assertions(6);
1907
1908 mockService("action", {
1909 doAction(actionRequest, options) {
1910 expect(actionRequest).toEqual({
1911 context: { allowed_company_ids: [1], lang: "en", tz: "taht", uid: 7 },
1912 domain: [["bar", "=", false]],
1913 name: "Foo Analysis",
1914 res_model: "foo",
1915 search_view_id: [67, "search"],
1916 target: "current",
1917 type: "ir.actions.act_window",
1918 views: [
1919 [false, "list"],
1920 [false, "form"],
1921 ],
1922 });
1923 expect(options).toEqual({ newWindow: true, viewType: "list" });
1924 },
1925 });
1926
1927 const view = await mountView({
1928 type: "graph",
1929 resModel: "foo",
1930 searchViewId: 67,
1931 arch: /* xml */ `
1932 <graph string="Foo Analysis">
1933 <field name="bar" />
1934 </graph>
1935 `,
1936 });
1937
1938 checkModeIs(view, "bar");
1939 checkDatasets(view, ["domains"], {
1940 domains: [[["bar", "=", false]], [["bar", "=", true]]],
1941 });
1942
1943 await clickOnDataset(view, { ctrlKey: true });
1944 });
1945
1946 test("Clicking on bar charts removes group_by and search_default_* context keys", async () => {
1947 expect.assertions(2);
1948
1949 mockService("action", {
1950 doAction(actionRequest, options) {
1951 expect(actionRequest).toEqual({
1952 context: { allowed_company_ids: [1], lang: "en", tz: "taht", uid: 7 },
1953 domain: [["bar", "=", false]],
1954 name: "Foo Analysis",
1955 res_model: "foo",
1956 search_view_id: [67, "search"],
1957 target: "current",
1958 type: "ir.actions.act_window",
1959 views: [
1960 [false, "list"],
1961 [false, "form"],
1962 ],
1963 });
1964 expect(options).toEqual({ newWindow: false, viewType: "list" });
1965 },
1966 });
1967
1968 const view = await mountView({
1969 type: "graph",
1970 resModel: "foo",
1971 searchViewId: 67,
1972 arch: /* xml */ `
1973 <graph string="Foo Analysis">
1974 <field name="bar" />
1975 </graph>
1976 `,
1977 context: {
1978 search_default_user: 1,
1979 group_by: "bar",
1980 },
1981 });
1982
1983 await clickOnDataset(view);
1984 });
1985
1986 test("clicking on a pie chart trigger a do_action with correct views", async () => {
1987 expect.assertions(6);
1988
1989 Foo._views[["list", 364]] = /* xml */ `<list />`;
1990 Foo._views[["form", 29]] = /* xml */ `<form />`;
1991
1992 mockService("action", {
1993 doAction(actionRequest, options) {
1994 expect(actionRequest).toEqual({
1995 context: { allowed_company_ids: [1], lang: "en", tz: "taht", uid: 7 },
1996 domain: [["bar", "=", false]],
1997 name: "Foo Analysis",
1998 res_model: "foo",
1999 search_view_id: [67, "search"],
2000 target: "current",
2001 type: "ir.actions.act_window",
2002 views: [
2003 [364, "list"],
2004 [29, "form"],
2005 ],
2006 });
2007 expect(options).toEqual({ newWindow: false, viewType: "list" });
2008 },
2009 });
2010
2011 const view = await mountView({
2012 type: "graph",
2013 resModel: "foo",
2014 searchViewId: 67,
2015 arch: /* xml */ `
2016 <graph string="Foo Analysis" type="pie">
2017 <field name="bar" />
2018 </graph>
2019 `,
2020 config: {
2021 views: [
2022 [364, "list"],
2023 [29, "form"],
2024 ],
2025 },
2026 });
2027
2028 checkModeIs(view, "pie");
2029 checkDatasets(view, ["domains"], {
2030 domains: [[["bar", "=", false]], [["bar", "=", true]]],
2031 });
2032
2033 await clickOnDataset(view);
2034 });
2035
2036 test("middle click on a pie chart trigger a do_action with correct views", async () => {
2037 expect.assertions(6);
2038
2039 Foo._views[["list", 364]] = /* xml */ `<list />`;
2040 Foo._views[["form", 29]] = /* xml */ `<form />`;
2041
2042 mockService("action", {
2043 doAction(actionRequest, options) {
2044 expect(actionRequest).toEqual({
2045 context: { allowed_company_ids: [1], lang: "en", tz: "taht", uid: 7 },
2046 domain: [["bar", "=", false]],
2047 name: "Foo Analysis",
2048 res_model: "foo",
2049 search_view_id: [67, "search"],
2050 target: "current",
2051 type: "ir.actions.act_window",
2052 views: [
2053 [364, "list"],
2054 [29, "form"],
2055 ],
2056 });
2057 expect(options).toEqual({ newWindow: true, viewType: "list" });
2058 },
2059 });
2060
2061 const view = await mountView({
2062 type: "graph",
2063 resModel: "foo",
2064 searchViewId: 67,
2065 arch: /* xml */ `
2066 <graph string="Foo Analysis" type="pie">
2067 <field name="bar" />
2068 </graph>
2069 `,
2070 config: {
2071 views: [
2072 [364, "list"],
2073 [29, "form"],
2074 ],
2075 },
2076 });
2077
2078 checkModeIs(view, "pie");
2079 checkDatasets(view, ["domains"], {
2080 domains: [[["bar", "=", false]], [["bar", "=", true]]],
2081 });
2082
2083 await clickOnDataset(view, { ctrlKey: true });
2084 });
2085
2086 test('graph view with attribute disable_linking="1"', async () => {
2087 mockService("action", {
2088 doAction() {
2089 throw new Error("should not perform a `doAction`");
2090 },
2091 });
2092
2093 const view = await mountView({
2094 type: "graph",
2095 resModel: "foo",
2096 arch: /* xml */ `
2097 <graph disable_linking="1">
2098 <field name="bar" />
2099 </graph>
2100 `,
2101 });
2102
2103 checkModeIs(view, "bar");
2104 checkDatasets(view, ["domains"], {
2105 domains: [[["bar", "=", false]], [["bar", "=", true]]],
2106 });
2107
2108 await clickOnDataset(view);
2109 });
2110
2111 test("graph view without invisible attribute on field", async () => {
2112 await mountView({
2113 type: "graph",
2114 resModel: "foo",
2115 });
2116 await toggleMenu("Measures");
2117
2118 expect(".o_menu_item").toHaveCount(3, {
2119 message:
2120 "there should be three menu items in the measures dropdown (count, revenue and foo)",
2121 });
2122 expect(".o_menu_item:contains(Revenue)").toHaveCount(1);
2123 expect(".o_menu_item:contains(Foo)").toHaveCount(1);
2124 expect(".o_menu_item:contains(Count)").toHaveCount(1);
2125 });
2126
2127 test("graph view with invisible attribute on field", async () => {
2128 await mountView({
2129 type: "graph",
2130 resModel: "foo",
2131 arch: /* xml */ `
2132 <graph>
2133 <field name="revenue" invisible="1" />
2134 </graph>
2135 `,
2136 });
2137 await toggleMenu("Measures");
2138
2139 expect(".o_menu_item").toHaveCount(2, {
2140 message: "there should be only two menu items in the measures dropdown (count and foo)",
2141 });
2142 expect(".o_menu_item:contains(Revenue)").toHaveCount(0);
2143 });
2144
2145 test("graph view reserved word", async () => {
2146 // Check that the use of reserved words does not interfere with the view.
2147 Product._records.push({ id: 150, name: "constructor" });
2148 Foo._records.at(-1).product_id = 150;
2149
2150 const view = await mountView({
2151 type: "graph",
2152 resModel: "foo",
2153 arch: /* xml */ `
2154 <graph order="DESC">
2155 <field name="product_id" />
2156 </graph>
2157 `,
2158 });
2159 checkLabels(view, ["xphone", "xpad", "constructor"]);
2160 checkDatasets(view, ["data", "label"], [{ data: [4, 3, 1], label: "Count" }]);
2161 });
2162
2163 test("graph view sort by measure", async () => {
2164 // change last record from foo as there are 4 records count for each product
2165 Product._records.push({ id: 150, name: "zphone" });
2166 Foo._records.at(-1).product_id = 150;
2167
2168 const view = await mountView({
2169 type: "graph",
2170 resModel: "foo",
2171 arch: /* xml */ `
2172 <graph order="DESC">
2173 <field name="product_id" />
2174 </graph>
2175 `,
2176 });
2177
2178 expect(".fa-sort-amount-asc").toHaveCount(1);
2179 expect(".fa-sort-amount-desc").toHaveCount(1);
2180
2181 checkLegend(view, "Count", "measure should be by count");
2182 expect(".fa-sort-amount-desc").toHaveClass("active");
2183 checkDatasets(view, "data", { data: [4, 3, 1] });
2184
2185 await clickSort("asc");
2186
2187 expect(".fa-sort-amount-asc").toHaveClass("active");
2188 checkDatasets(view, "data", { data: [1, 3, 4] });
2189
2190 await clickSort("desc");
2191
2192 expect(".fa-sort-amount-desc").toHaveClass("active");
2193 checkDatasets(view, "data", { data: [4, 3, 1] });
2194
2195 // again click on descending button to deactivate order
2196 await clickSort("desc");
2197
2198 expect(".fa-sort-amount-desc").not.toHaveClass("active");
2199 checkDatasets(view, "data", { data: [4, 1, 3] });
2200
2201 // set line mode
2202 await selectMode("line");
2203 expect(".fa-sort-amount-asc").toHaveCount(1);
2204 expect(".fa-sort-amount-desc").toHaveCount(1);
2205
2206 checkLegend(view, "Count", "measure should be by count");
2207 expect(".fa-sort-amount-desc").not.toHaveClass("active");
2208 checkDatasets(view, "data", { data: [4, 1, 3] });
2209
2210 await clickSort("asc");
2211
2212 expect(".fa-sort-amount-asc").toHaveClass("active");
2213 checkDatasets(view, "data", { data: [1, 3, 4] });
2214
2215 await clickSort("desc");
2216
2217 expect(".fa-sort-amount-desc").toHaveClass("active");
2218 checkDatasets(view, "data", { data: [4, 3, 1] });
2219 });
2220
2221 test("graph view sort by measure for grouped data", async () => {
2222 // change last record from foo as there are 4 records count for each product
2223 Product._records.push({ id: 150, name: "zphone" });
2224 Foo._records.at(-1).product_id = 150;
2225
2226 const view = await mountView({
2227 type: "graph",
2228 resModel: "foo",
2229 arch: /* xml */ `
2230 <graph>
2231 <field name="product_id" />
2232 <field name="bar" />
2233 </graph>
2234 `,
2235 });
2236
2237 checkLegend(view, ["false", "true", "Sum"], "measure should be by count");
2238 checkDatasets(view, "data", [{ data: [1, 1, 3] }, { data: [3, 0, 0] }, { data: [4, 1, 3] }]);
2239
2240 await clickSort("asc");
2241
2242 expect(".fa-sort-amount-asc").toHaveClass("active");
2243 checkDatasets(view, "data", [{ data: [1, 3, 1] }, { data: [0, 0, 3] }, { data: [1, 3, 4] }]);
2244
2245 await clickSort("desc");
2246
2247 expect(".fa-sort-amount-desc").toHaveClass("active");
2248 checkDatasets(view, "data", [{ data: [1, 3, 1] }, { data: [3, 0, 0] }, { data: [4, 3, 1] }]);
2249
2250 // again click on descending button to deactivate order
2251 await clickSort("desc");
2252
2253 expect(".fa-sort-amount-desc").not.toHaveClass("active");
2254 checkDatasets(view, "data", [{ data: [1, 1, 3] }, { data: [3, 0, 0] }, { data: [4, 1, 3] }]);
2255 });
2256
2257 test("graph view sort by measure for multiple grouped data", async () => {
2258 // change last record from foo as there are 4 records count for each product
2259 Product._records.push({ id: 150, name: "zphone" });
2260 Foo._records.at(-1).product_id = 150;
2261 Foo._records.splice(
2262 0,
2263 4,
2264 { id: 1, foo: 48, bar: false, product_id: 200, date: "2016-04-01" },
2265 { id: 2, foo: 49, bar: false, product_id: 200, date: "2016-04-01" },
2266 { id: 3, foo: 50, bar: true, product_id: 100, date: "2016-01-03" },
2267 { id: 4, foo: 50, bar: true, product_id: 200, date: "2016-01-03" }
2268 );
2269
2270 const view = await mountView({
2271 type: "graph",
2272 resModel: "foo",
2273 arch: /* xml */ `
2274 <graph>
2275 <field name="date" />
2276 <field name="product_id" />
2277 </graph>
2278 `,
2279 });
2280
2281 checkLegend(view, ["xphone", "xpad", "zphone", "Sum"], "measure should be by count");
2282 checkDatasets(view, "data", [
2283 { data: [1, 0, 0, 0] },
2284 { data: [1, 2, 1, 2] },
2285 { data: [0, 1, 0, 0] },
2286 { data: [2, 3, 1, 2] },
2287 ]);
2288
2289 await clickSort("asc");
2290
2291 expect(".fa-sort-amount-asc").toHaveClass("active");
2292 checkDatasets(view, "data", [
2293 { data: [1, 1, 2, 2] },
2294 { data: [0, 1, 0, 0] },
2295 { data: [0, 0, 0, 1] },
2296 { data: [1, 2, 2, 3] },
2297 ]);
2298
2299 await clickSort("desc");
2300
2301 expect(".fa-sort-amount-desc").toHaveClass("active");
2302 checkDatasets(view, "data", [
2303 { data: [1, 0, 0, 0] },
2304 { data: [2, 1, 2, 1] },
2305 { data: [0, 1, 0, 0] },
2306 { data: [3, 2, 2, 1] },
2307 ]);
2308
2309 // again click on descending button to deactivate order
2310 await clickSort("desc");
2311
2312 expect(".fa-sort-amount-desc").not.toHaveClass("active");
2313 checkDatasets(view, "data", [
2314 { data: [1, 0, 0, 0] },
2315 { data: [1, 2, 1, 2] },
2316 { data: [0, 1, 0, 0] },
2317 { data: [2, 3, 1, 2] },
2318 ]);
2319 });
2320
2321 test("empty graph view with sample data", async () => {
2322 await mountView({
2323 type: "graph",
2324 resModel: "foo",
2325 arch: /* xml */ `
2326 <graph sample="1">
2327 <field name="product_id" />
2328 <field name="date" />
2329 </graph>
2330 `,
2331 context: {
2332 search_default_false_domain: 1,
2333 },
2334 searchViewArch: /* xml */ `
2335 <search>
2336 <filter name="false_domain" string="False Domain" domain="[(0, '=', 1)]" />
2337 </search>
2338 `,
2339 noContentHelp: /* xml */ `<p class="abc">click to add a foo</p>`,
2340 });
2341
2342 expect(".o_graph_view .o_content").toHaveClass("o_view_sample_data");
2343 expect(".o_view_nocontent").toHaveCount(1);
2344 expect(".o_graph_canvas_container canvas").toHaveCount(1);
2345
2346 await toggleSearchBarMenu();
2347 await toggleMenuItem("False Domain");
2348
2349 expect(".o_graph_view .o_content").not.toHaveClass("o_view_sample_data");
2350 expect(".o_view_nocontent").toHaveCount(0);
2351 expect(".o_graph_canvas_container canvas").toHaveCount(1);
2352 });
2353
2354 test("non empty graph view with sample data", async () => {
2355 await mountView({
2356 type: "graph",
2357 resModel: "foo",
2358 arch: /* xml */ `
2359 <graph sample="1">
2360 <field name="product_id" />
2361 <field name="date" />
2362 </graph>
2363 `,
2364 searchViewArch: /* xml */ `
2365 <search>
2366 <filter name="false_domain" string="False Domain" domain="[(0, '=', 1)]" />
2367 </search>
2368 `,
2369 noContentHelp: /* xml */ `<p class="abc">click to add a foo</p>`,
2370 });
2371
2372 expect(".o_content").not.toHaveClass("o_view_sample_data");
2373 expect(".o_view_nocontent").toHaveCount(0);
2374 expect(".o_graph_canvas_container canvas").toHaveCount(1);
2375
2376 await toggleSearchBarMenu();
2377 await toggleMenuItem("False Domain");
2378
2379 expect(".o_content").not.toHaveClass("o_view_sample_data");
2380 expect(".o_graph_canvas_container canvas").toHaveCount(0);
2381 expect(".o_view_nocontent").toHaveCount(1);
2382 });
2383
2384 test("empty graph view without sample data after filter", async () => {
2385 await mountView({
2386 type: "graph",
2387 resModel: "foo",
2388 arch: /* xml */ `
2389 <graph>
2390 <field name="date" />
2391 </graph>
2392 `,
2393 domain: Domain.FALSE.toList(),
2394 noContentHelp: /* xml */ `<p class="abc">click to add a foo</p>`,
2395 });
2396
2397 expect(".o_graph_canvas_container canvas").toHaveCount(0);
2398 expect(".o_view_nocontent").toHaveCount(1);
2399 });
2400
2401 test.tags("desktop");
2402 test("reload chart with switchView button keep internal state", async () => {
2403 Foo._views.list = /* xml */ `<list />`;
2404
2405 await mountWithCleanup(WebClient);
2406 await getService("action").doAction({
2407 name: "Foo Action 1",
2408 res_model: "foo",
2409 type: "ir.actions.act_window",
2410 views: [
2411 [false, "graph"],
2412 [false, "list"],
2413 ],
2414 });
2415
2416 expect(getModeButton("bar")).toHaveClass("active");
2417
2418 await selectMode("line");
2419
2420 expect(getModeButton("line")).toHaveClass("active");
2421
2422 await switchView("graph");
2423
2424 expect(getModeButton("line")).toHaveClass("active");
2425 });
2426
2427 test("fallback on initial groupby when the groupby from control panel has 0 length", async () => {
2428 const view = await mountView({
2429 type: "graph",
2430 resModel: "foo",
2431 arch: /* xml */ `
2432 <graph type="line">
2433 <field name="product_id" />
2434 </graph>
2435 `,
2436 searchViewArch: /* xml */ `
2437 <search>
2438 <filter name="group_by_foo" string="Foo" domain="[]" context="{ 'group_by': 'foo'}" />
2439 </search>
2440 `,
2441 context: {
2442 search_default_group_by_foo: 1,
2443 },
2444 });
2445
2446 checkLabels(view, ["2", "3", "4", "24", "42", "48", "53", "63"]);
2447
2448 await toggleSearchBarMenu();
2449 await toggleMenuItem("Foo");
2450
2451 checkLabels(view, ["xphone", "xpad"]);
2452 });
2453
2454 test("change mode, stacked, or order via the graph buttons does not reload datapoints, change measure does", async () => {
2455 onRpc("formatted_read_group", ({ kwargs }) => {
2456 expect.step(kwargs.aggregates);
2457 });
2458 const view = await mountView({
2459 type: "graph",
2460 resModel: "foo",
2461 arch: /* xml */ `
2462 <graph type="line">
2463 <field name="product_id" />
2464 </graph>
2465 `,
2466 });
2467
2468 checkModeIs(view, "line");
2469
2470 await selectMode("bar");
2471
2472 checkModeIs(view, "bar");
2473 expect(`[data-tooltip="Stacked"]`).toHaveClass("active");
2474
2475 await contains(`[data-tooltip="Stacked"]`).click();
2476
2477 expect(`[data-tooltip="Stacked"]`).not.toHaveClass("active");
2478 expect(`[data-tooltip="Ascending"]`).not.toHaveClass("active");
2479
2480 await contains(`[data-tooltip="Ascending"]`).click();
2481
2482 expect(`[data-tooltip="Ascending"]`).toHaveClass("active");
2483
2484 await toggleMenu("Measures");
2485 await toggleMenuItem("Foo");
2486
2487 expect.verifySteps([
2488 ["__count"], // first load
2489 ["__count", "foo:sum"], // reload due to change in measure
2490 ]);
2491 });
2492
2493 test("concurrent reloads: add a filter, and directly toggle a measure", async () => {
2494 let def;
2495 onRpc("formatted_read_group", () => def);
2496 const view = await mountView({
2497 type: "graph",
2498 resModel: "foo",
2499 arch: /* xml */ `
2500 <graph type="line">
2501 <field name="product_id" />
2502 </graph>
2503 `,
2504 searchViewArch: /* xml */ `
2505 <search>
2506 <filter name="my_filter" string="My Filter" domain="[('id', '&lt;', 6)]" />
2507 </search>
2508 `,
2509 });
2510
2511 checkDatasets(view, ["data", "label"], {
2512 data: [4, 4],
2513 label: "Count",
2514 });
2515
2516 // Set a domain (this reload is delayed)
2517 def = new Deferred();
2518 await toggleSearchBarMenu();
2519 await toggleMenuItem("My Filter");
2520
2521 checkDatasets(view, ["data", "label"], {
2522 data: [4, 4],
2523 label: "Count",
2524 });
2525
2526 // Toggle a measure
2527 await toggleMenu("Measures");
2528 await toggleMenuItem("Foo");
2529
2530 checkDatasets(view, ["data", "label"], {
2531 data: [4, 4],
2532 label: "Count",
2533 });
2534
2535 def.resolve();
2536 await animationFrame();
2537
2538 checkDatasets(view, ["data", "label"], {
2539 data: [82, 4],
2540 label: "Foo",
2541 });
2542 });
2543
2544 test("change graph mode while loading a filter", async () => {
2545 let def;
2546 onRpc("formatted_read_group", () => def);
2547 const view = await mountView({
2548 type: "graph",
2549 resModel: "foo",
2550 arch: /* xml */ `
2551 <graph type="line">
2552 <field name="product_id" />
2553 </graph>
2554 `,
2555 searchViewArch: /* xml */ `
2556 <search>
2557 <filter name="my_filter" string="My Filter" domain="[('id', '&lt;', 2)]" />
2558 </search>
2559 `,
2560 });
2561
2562 checkDatasets(view, ["data", "label"], {
2563 data: [4, 4],
2564 label: "Count",
2565 });
2566 checkModeIs(view, "line");
2567
2568 // Set a domain (this reload is delayed)
2569 def = new Deferred();
2570 await toggleSearchBarMenu();
2571 await toggleMenuItem("My Filter");
2572
2573 checkDatasets(view, ["data", "label"], {
2574 data: [4, 4],
2575 label: "Count",
2576 });
2577 checkModeIs(view, "line");
2578
2579 // Change graph mode
2580 await selectMode("bar");
2581
2582 checkDatasets(view, ["data", "label"], {
2583 data: [4, 4],
2584 label: "Count",
2585 });
2586 checkModeIs(view, "line");
2587
2588 def.resolve();
2589 await animationFrame();
2590
2591 checkDatasets(view, ["data", "label"], {
2592 data: [1],
2593 label: "Count",
2594 });
2595 checkModeIs(view, "bar");
2596 });
2597
2598 test("only process most recent data for concurrent groupby", async () => {
2599 let def;
2600 onRpc(() => def);
2601 const view = await mountView({
2602 type: "graph",
2603 resModel: "foo",
2604 arch: /* xml */ `
2605 <graph>
2606 <field name="product_id" type="row" />
2607 <field name="foo" type="measure" />
2608 </graph>
2609 `,
2610 searchViewArch: /* xml */ `
2611 <search>
2612 <filter name="group_by_color" string="Color" context="{ 'group_by': 'color_id' }" />
2613 <filter name="group_by_date" string="Date" context="{ 'group_by': 'date' }" />
2614 </search>
2615 `,
2616 });
2617
2618 checkLabels(view, ["xphone", "xpad"]);
2619 checkDatasets(view, "data", { data: [82, 157] });
2620
2621 def = new Deferred();
2622 await toggleSearchBarMenu();
2623 await toggleMenuItem("Color");
2624 await toggleMenuItem("Color");
2625 await toggleMenuItem("Date");
2626 await toggleMenuItemOption("Date", "Month");
2627
2628 checkLabels(view, ["xphone", "xpad"]);
2629 checkDatasets(view, "data", { data: [82, 157] });
2630
2631 def.resolve();
2632 await animationFrame();
2633
2634 checkLabels(view, ["January 2016", "March 2016", "April 2016", "May 2016", "None"]);
2635 checkDatasets(view, "data", { data: [56, 26, 48, 4, 105] });
2636 });
2637
2638 test("fill_temporal is true by default", async () => {
2639 expect.assertions(1);
2640
2641 onRpc("formatted_read_group", ({ kwargs }) => {
2642 expect(kwargs.context.fill_temporal).toBe(true, {
2643 message: "The observable state of fill_temporal should be true",
2644 });
2645 });
2646
2647 await mountView({ type: "graph", resModel: "foo" });
2648 });
2649
2650 test("fill_temporal can be changed throught the context", async () => {
2651 expect.assertions(1);
2652
2653 onRpc("formatted_read_group", ({ kwargs }) => {
2654 expect(kwargs.context.fill_temporal).toBe(false, {
2655 message: "The observable state of fill_temporal should be false",
2656 });
2657 });
2658
2659 await mountView({
2660 type: "graph",
2661 resModel: "foo",
2662 context: {
2663 fill_temporal: false,
2664 },
2665 });
2666 });
2667
2668 test("group by a non stored, sortable field", async () => {
2669 // When a field is non-stored but sortable it's inherited
2670 // from a stored field, so it can be sortable
2671 Foo._fields.date.store = false;
2672
2673 const view = await mountView({
2674 type: "graph",
2675 resModel: "foo",
2676 groupBy: ["date:month"],
2677 arch: /* xml */ `<graph type="line" />`,
2678 });
2679
2680 checkLabels(view, ["January 2016", "March 2016", "April 2016", "May 2016"]);
2681 });
2682
2683 test("graph_groupbys should be also used after first load", async () => {
2684 const view = await mountView({
2685 type: "graph",
2686 resModel: "foo",
2687 viewId: false,
2688 groupBy: ["date:quarter"],
2689 irFilters: [
2690 {
2691 user_ids: [2],
2692 name: "Favorite",
2693 id: 1,
2694 context: JSON.stringify({
2695 group_by: [],
2696 graph_measure: "revenue",
2697 graph_mode: "bar",
2698 graph_groupbys: ["color_id"],
2699 }),
2700 sort: "[]",
2701 domain: "",
2702 is_default: false,
2703 model_id: "foo",
2704 action_id: false,
2705 },
2706 ],
2707 });
2708
2709 checkModeIs(view, "bar");
2710 checkLabels(view, ["Q1 2016", "Q2 2016", "None"]);
2711 checkLegend(view, "Count");
2712
2713 await toggleSearchBarMenu();
2714 await toggleMenuItem("Favorite");
2715
2716 checkModeIs(view, "bar");
2717 checkLabels(view, ["red", "None"]);
2718 checkLegend(view, "Revenue");
2719 });
2720
2721 test("order='desc' on arch", async () => {
2722 const view = await mountView({
2723 type: "graph",
2724 resModel: "foo",
2725 arch: /* xml */ `
2726 <graph order="desc">
2727 <field name="date" />
2728 </graph>
2729 `,
2730 });
2731 checkDatasets(view, ["data", "label"], {
2732 data: [2, 2, 2, 1, 1],
2733 label: "Count",
2734 });
2735 });
2736
2737 test("order='asc' on arch", async () => {
2738 const view = await mountView({
2739 type: "graph",
2740 resModel: "foo",
2741 arch: /* xml */ `
2742 <graph order="asc">
2743 <field name="date" />
2744 </graph>
2745 `,
2746 });
2747 checkDatasets(view, ["data", "label"], {
2748 data: [1, 1, 2, 2, 2],
2749 label: "Count",
2750 });
2751 });
2752
2753 test("In the middle of a year, a graph view grouped by a date field with granularity 'year' should have a single group of SampleServer.MAIN_RECORDSET_SIZE records", async () => {
2754 mockDate("2023-06-15 08:00:00");
2755
2756 const view = await mountView({
2757 type: "graph",
2758 resModel: "foo",
2759 arch: /* xml */ `
2760 <graph sample="1">
2761 <field name="date" interval="year" />
2762 </graph>
2763 `,
2764 domain: Domain.FALSE.toList(),
2765 });
2766
2767 checkDatasets(view, ["data"], { data: [SampleServer.MAIN_RECORDSET_SIZE] });
2768 });
2769
2770 test("no class 'o_view_sample_data' when real data are presented", async () => {
2771 Foo._records = [];
2772
2773 const view = await mountView({
2774 type: "graph",
2775 resModel: "foo",
2776 arch: /* xml */ `
2777 <graph sample="1">
2778 <field name="date" />
2779 </graph>
2780 `,
2781 });
2782
2783 expect(".o_graph_view .o_view_sample_data").toHaveCount(1);
2784 expect(getChart(view).data.datasets.length).toBeGreaterThan(0);
2785
2786 await selectMode("line");
2787
2788 expect(".o_graph_view .o_view_sample_data").toHaveCount(1);
2789 expect(getChart(view).data.datasets.length).toBeGreaterThan(0);
2790
2791 await toggleMenu("Measures");
2792 await toggleMenuItem("Revenue");
2793
2794 expect(".o_graph_view .o_view_sample_data").toHaveCount(0);
2795 expect(".o_graph_canvas_container").toHaveCount(0);
2796 });
2797
2798 test("single chart rendering on search", async () => {
2799 patchWithCleanup(GraphRenderer.prototype, {
2800 setup() {
2801 super.setup();
2802
2803 onRendered(() => expect.step("rendering"));
2804 },
2805 });
2806
2807 await mountView({
2808 type: "graph",
2809 resModel: "foo",
2810 });
2811
2812 expect.verifySteps(["rendering"]);
2813
2814 await validateSearch();
2815
2816 expect.verifySteps(["rendering"]);
2817 });
2818
2819 test("missing property field definition is fetched", async function () {
2820 Foo._fields.properties_definition = fields.PropertiesDefinition();
2821 Foo._fields.parent_id = fields.Many2one({ relation: "foo" });
2822 Foo._fields.properties = fields.Properties({
2823 definition_record: "parent_id",
2824 definition_record_field: "properties_definition",
2825 });
2826 onRpc(({ method, kwargs }) => {
2827 if (method === "formatted_read_group" && kwargs.groupby?.includes("properties.my_char")) {
2828 expect.step(JSON.stringify(kwargs.groupby));
2829 return [
2830 {
2831 "properties.my_char": false,
2832 __extra_domain: [["properties.my_char", "=", false]],
2833 __count: 2,
2834 },
2835 {
2836 "properties.my_char": "aaa",
2837 __extra_domain: [["properties.my_char", "=", "aaa"]],
2838 __count: 1,
2839 },
2840 ];
2841 } else if (method === "get_property_definition") {
2842 return {
2843 name: "my_char",
2844 type: "char",
2845 };
2846 }
2847 });
2848 const view = await mountView({
2849 type: "graph",
2850 resModel: "foo",
2851 arch: `<graph/>`,
2852 irFilters: [
2853 {
2854 user_ids: [2],
2855 name: "My Filter",
2856 id: 5,
2857 context: `{"group_by": ['properties.my_char']}`,
2858 sort: "[]",
2859 domain: "[]",
2860 is_default: true,
2861 model_id: "foo",
2862 action_id: false,
2863 },
2864 ],
2865 });
2866 expect.verifySteps([`["properties.my_char"]`]);
2867 checkLabels(view, ["None", "aaa"]);
2868 checkDatasets(
2869 view,
2870 ["data", "label"],
2871 [
2872 {
2873 data: [2, 1],
2874 label: "Count",
2875 },
2876 ]
2877 );
2878 });
2879
2880 test("missing deleted property field definition is created", async function () {
2881 Foo._fields.properties_definition = fields.PropertiesDefinition();
2882 Foo._fields.parent_id = fields.Many2one({ relation: "foo" });
2883 Foo._fields.properties = fields.Properties({
2884 definition_record: "parent_id",
2885 definition_record_field: "properties_definition",
2886 });
2887 onRpc(({ method, kwargs }) => {
2888 if (method === "formatted_read_group" && kwargs.groupby?.includes("properties.my_char")) {
2889 expect.step(JSON.stringify(kwargs.groupby));
2890 return [
2891 {
2892 "properties.my_char": false,
2893 __extra_domain: [["properties.my_char", "=", false]],
2894 __count: 2,
2895 },
2896 {
2897 "properties.my_char": "aaa",
2898 __extra_domain: [["properties.my_char", "=", "aaa"]],
2899 __count: 1,
2900 },
2901 ];
2902 } else if (method === "get_property_definition") {
2903 return {};
2904 }
2905 });
2906 const view = await mountView({
2907 type: "graph",
2908 resModel: "foo",
2909 arch: `<graph/>`,
2910 irFilters: [
2911 {
2912 user_ids: [2],
2913 name: "My Filter",
2914 id: 5,
2915 context: `{"group_by": ['properties.my_char']}`,
2916 sort: "[]",
2917 domain: "[]",
2918 is_default: true,
2919 model_id: "foo",
2920 action_id: false,
2921 },
2922 ],
2923 });
2924 expect.verifySteps([`["properties.my_char"]`]);
2925 checkLabels(view, ["None", "aaa"]);
2926 checkDatasets(
2927 view,
2928 ["data", "label"],
2929 [
2930 {
2931 data: [2, 1],
2932 label: "Count",
2933 },
2934 ]
2935 );
2936 });
2937
2938 test("display '0' for false group, when grouped by int field", async () => {
2939 Foo._records[0].foo = false;
2940
2941 const view = await mountView({
2942 type: "graph",
2943 resModel: "foo",
2944 groupBy: ["foo"],
2945 arch: /* xml */ `<graph type="pie" />`,
2946 });
2947
2948 checkLabels(view, ["2", "4", "24", "42", "48", "53", "63", "0"]);
2949 });
2950
2951 test("display the field's falsy_value_label for false group, if defined", async () => {
2952 Foo._fields.product_id.falsy_value_label = "I'm the false group";
2953 Foo._records[0].product_id = false;
2954
2955 const view = await mountView({
2956 type: "graph",
2957 resModel: "foo",
2958 groupBy: ["product_id"],
2959 arch: /* xml */ `<graph type="bar" />`,
2960 });
2961
2962 checkLabels(view, ["xphone", "xpad", "I'm the false group"]);
2963 });
2964
2965 test("limit dataset amount", async () => {
2966 class Project extends models.Model {
2967 id = fields.Integer();
2968 name = fields.Char();
2969 }
2970 class Stage extends models.Model {
2971 id = fields.Integer();
2972 name = fields.Char();
2973 }
2974 class Task extends models.Model {
2975 id = fields.Integer();
2976 name = fields.Char();
2977 project_id = fields.Many2one({ relation: "project" });
2978 stage_id = fields.Many2one({ relation: "stage" });
2979 }
2980 defineModels([Project, Stage, Task]);
2981
2982 for (let i = 1; i <= 600; i++) {
2983 Project._records.push({
2984 id: i,
2985 name: `Project ${i}`,
2986 });
2987 Stage._records.push({
2988 id: i,
2989 name: `Stage ${i}`,
2990 });
2991 Task._records.push({
2992 id: i,
2993 project_id: i,
2994 stage_id: i,
2995 name: `Task ${i}`,
2996 });
2997 }
2998
2999 const view = await mountView({
3000 type: "graph",
3001 resModel: "task",
3002 arch: `
3003 <graph>
3004 <field name="project_id"/>
3005 <field name="stage_id"/>
3006 </graph>
3007 `,
3008 });
3009 const model = getGraphModel(view);
3010 expect(model.data.exceeds).toBe(true);
3011 expect(model.data.datasets).toHaveLength(80);
3012 expect(model.data.labels).toHaveLength(80);
3013 expect(`.o_graph_alert`).toHaveCount(1);
3014
3015 patchWithCleanup(GraphModel.prototype, {
3016 notify() {
3017 expect.step("rerender");
3018 },
3019 });
3020 await contains(`.o_graph_load_all_btn`).click();
3021 expect.verifySteps(["rerender"]);
3022 expect(model.data.exceeds).toBe(false);
3023 expect(model.data.datasets).toHaveLength(600);
3024 expect(model.data.labels).toHaveLength(600);
3025 });
3026
3027 test.tags("desktop");
3028 test("graph views make their control panel available directly", async () => {
3029 const def = new Deferred();
3030 onRpc("formatted_read_group", () => def);
3031 await mountView({
3032 type: "graph",
3033 resModel: "foo",
3034 arch: `<graph/>`,
3035 });
3036
3037 expect(".o_graph_view").toHaveCount(1);
3038 expect(".o_graph_view .o_control_panel .o_searchview").toHaveCount(1);
3039 expect(".o_graph_view .o_graph_renderer").toHaveCount(0);
3040
3041 def.resolve();
3042 await animationFrame();
3043 expect(".o_graph_view .o_graph_renderer").toHaveCount(1);
3044 });
3045
3046 test.tags("desktop");
3047 test("monetary chart rendering with multiple currencies", async () => {
3048 // simulate that the company currency is EUR
3049 serverState.companies[0].currency_id = 2;
3050
3051 onRpc("/web/domain/validate", () => true);
3052 Foo._fields.amount = fields.Monetary({ currency_field: "currency_id" });
3053 Foo._fields.currency_id = fields.Many2one({ relation: "res.currency", default: 1 });
3054 Foo._records[0].amount = 500;
3055 Foo._records[1].amount = 300;
3056 Foo._records[2].amount = 200;
3057 Foo._records[3].amount = 400;
3058 Foo._records[3].currency_id = 2;
3059 Foo._records[4].amount = 700;
3060 Foo._records[4].currency_id = 2;
3061 Foo._records[5].amount = 100;
3062 const view = await mountView({
3063 type: "graph",
3064 resModel: "foo",
3065 arch: /* xml */ `
3066 <graph>
3067 <field name="bar" />
3068 <field name="amount" type="measure" />
3069 </graph>
3070 `,
3071 });
3072
3073 expect(".o_graph_canvas_container canvas").toHaveCount(1);
3074 checkLabels(view, ["false", "true"]);
3075 checkDatasets(view, ["backgroundColor", "borderColor", "data", "label"], {
3076 backgroundColor: "#4EA7F2",
3077 borderColor: undefined,
3078 data: [1200, 1000],
3079 label: "Amount",
3080 });
3081 checkLegend(view, "Amount");
3082 // should display the sum in the company currency, i.e. EUR
3083 checkTooltip(view, { title: "Amount", lines: [{ label: "false", value: "1,200.00 €" }] }, 0);
3084 checkTooltip(view, { title: "Amount", lines: [{ label: "true", value: "1,000.00 €" }] }, 1);
3085 });