Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, press } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 defineModels,
6 fields,
7 findComponent,
8 models,
9 mountView,
10 } from "@web/../tests/web_test_helpers";
11 import { KanbanController } from "@web/views/kanban/kanban_controller";
12
13 const graph_values = [
14 { value: 300, label: "5-11 Dec" },
15 { value: 500, label: "12-18 Dec" },
16 { value: 100, label: "19-25 Dec" },
17 ];
18
19 class Partner extends models.Model {
20 int_field = fields.Integer({
21 string: "int_field",
22 sortable: true,
23 searchable: true,
24 });
25 selection = fields.Selection({
26 string: "Selection",
27 searchable: true,
28 selection: [
29 ["normal", "Normal"],
30 ["blocked", "Blocked"],
31 ["done", "Done"],
32 ],
33 });
34 graph_data = fields.Text({ string: "Graph Data" });
35 graph_type = fields.Selection({
36 string: "Graph Type",
37 selection: [
38 ["line", "Line"],
39 ["bar", "Bar"],
40 ],
41 });
42
43 _records = [
44 {
45 id: 1,
46 int_field: 10,
47 selection: "blocked",
48 graph_type: "bar",
49 graph_data: JSON.stringify([
50 {
51 color: "blue",
52 title: "Partner 1",
53 values: graph_values,
54 key: "A key",
55 area: true,
56 },
57 ]),
58 },
59 {
60 id: 2,
61 display_name: "second record",
62 int_field: 0,
63 selection: "normal",
64 graph_type: "line",
65 graph_data: JSON.stringify([
66 {
67 color: "red",
68 title: "Partner 0",
69 values: graph_values,
70 key: "A key",
71 area: true,
72 },
73 ]),
74 },
75 ];
76 }
77 class User extends models.Model {
78 _name = "res.users";
79 has_group() {
80 return true;
81 }
82 }
83 defineModels([Partner, User]);
84
85 // Kanban
86 // WOWL remove this helper and user the control panel instead
87 const reload = async (kanban, params = {}) => {
88 kanban.env.searchModel.reload(params);
89 kanban.env.searchModel.search();
90 await animationFrame();
91 };
92
93 test.tags("desktop");
94 test("JournalDashboardGraphField is rendered correctly", async () => {
95 await mountView({
96 type: "kanban",
97 resModel: "partner",
98 arch: /* xml */ `
99 <kanban>
100 <field name="graph_type"/>
101 <templates>
102 <t t-name="card">
103 <field name="graph_data" t-att-graph_type="record.graph_type.raw_value" widget="dashboard_graph"/>
104 </t>
105 </templates>
106 </kanban>`,
107 domain: [["id", "in", [1, 2]]],
108 });
109 expect(".o_dashboard_graph canvas").toHaveCount(2, {
110 message: "there should be two graphs rendered",
111 });
112 expect(".o_kanban_record:nth-child(1) .o_graph_barchart").toHaveCount(1, {
113 message: "graph of first record should be a barchart",
114 });
115 expect(".o_kanban_record:nth-child(2) .o_graph_linechart").toHaveCount(1, {
116 message: "graph of second record should be a linechart",
117 });
118
119 // reload kanban
120 await click("input.o_searchview_input");
121 await press("Enter");
122 await animationFrame();
123
124 expect(".o_dashboard_graph canvas").toHaveCount(2, {
125 message: "there should be two graphs rendered",
126 });
127 });
128
129 test("rendering of a JournalDashboardGraphField in an updated grouped kanban view", async () => {
130 const view = await mountView({
131 type: "kanban",
132 resModel: "partner",
133 arch: /* xml */ `
134 <kanban>
135 <field name="graph_type"/>
136 <templates>
137 <t t-name="card">
138 <field name="graph_data" t-att-graph_type="record.graph_type.raw_value" widget="dashboard_graph"/>
139 </t>
140 </templates>
141 </kanban>`,
142 domain: [["id", "in", [1, 2]]],
143 });
144 const kanban = findComponent(view, (component) => component instanceof KanbanController);
145 expect(".o_dashboard_graph canvas").toHaveCount(2, {
146 message: "there should be two graph rendered",
147 });
148 await reload(kanban, { groupBy: ["selection"], domain: [["int_field", "=", 10]] });
149
150 expect(".o_dashboard_graph canvas").toHaveCount(1, {
151 message: "there should be one graph rendered",
152 });
153 });