Debian Codegraph @

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

raw
1 import { defineModels, fields, models, mountView } from "@web/../tests/web_test_helpers";
2 import { test, expect } from "@odoo/hoot";
3 import { queryOne } from "@odoo/hoot-dom";
4
5 class Partner extends models.Model {
6 foo = fields.Char({
7 string: "Foo",
8 default: "My little Foo Value",
9 trim: true,
10 });
11 int_field = fields.Integer();
12 float_field = fields.Float();
13 _records = [
14 { id: 1, foo: "yop", int_field: 10 },
15 { id: 2, foo: "gnap", int_field: 80 },
16 { id: 3, foo: "blip", float_field: 33.3333 },
17 ];
18 }
19
20 defineModels([Partner]);
21
22 test("PercentPieField in form view with value < 50%", async () => {
23 await mountView({
24 type: "form",
25 resModel: "partner",
26 arch: /* xml */ `
27 <form>
28 <sheet>
29 <group>
30 <field name="int_field" widget="percentpie"/>
31 </group>
32 </sheet>
33 </form>`,
34 resId: 1,
35 });
36
37 expect(".o_field_percent_pie.o_field_widget .o_pie").toHaveCount(1);
38 expect(".o_field_percent_pie.o_field_widget .o_pie_info .o_pie_value").toHaveText("10%", {
39 message: "should have 10% as pie value since int_field=10",
40 });
41
42 expect(
43 queryOne(".o_field_percent_pie.o_field_widget .o_pie").style.background.replaceAll(
44 /\s+/g,
45 " "
46 )
47 ).toBe(
48 "conic-gradient( var(--PercentPieField-color-active) 0% 10%, var(--PercentPieField-color-static) 0% 100% )",
49 { message: "pie should have a background computed for its value of 10%" }
50 );
51 });
52
53 test("PercentPieField in form view with value > 50%", async () => {
54 await mountView({
55 type: "form",
56 resModel: "partner",
57 arch: /* xml */ `
58 <form>
59 <sheet>
60 <group>
61 <field name="int_field" widget="percentpie"/>
62 </group>
63 </sheet>
64 </form>`,
65 resId: 2,
66 });
67
68 expect(".o_field_percent_pie.o_field_widget .o_pie").toHaveCount(1);
69 expect(".o_field_percent_pie.o_field_widget .o_pie_info .o_pie_value").toHaveText("80%", {
70 message: "should have 80% as pie value since int_field=80",
71 });
72 expect(
73 queryOne(".o_field_percent_pie.o_field_widget .o_pie").style.background.replaceAll(
74 /\s+/g,
75 " "
76 )
77 ).toBe(
78 "conic-gradient( var(--PercentPieField-color-active) 0% 80%, var(--PercentPieField-color-static) 0% 100% )",
79 { message: "pie should have a background computed for its value of 80%" }
80 );
81 });
82
83 test("PercentPieField in form view with float value", async () => {
84 await mountView({
85 type: "form",
86 resModel: "partner",
87 arch: /* xml */ `
88 <form>
89 <sheet>
90 <group>
91 <field name="float_field" widget="percentpie"/>
92 </group>
93 </sheet>
94 </form>`,
95 resId: 3,
96 });
97
98 expect(".o_field_percent_pie.o_field_widget .o_pie").toHaveCount(1);
99 expect(".o_field_percent_pie.o_field_widget .o_pie_info .o_pie_value").toHaveText("33.33%", {
100 message:
101 "should have 33.33% as pie value since float_field=33.3333 and its value is rounded to 2 decimals",
102 });
103 expect(
104 queryOne(".o_field_percent_pie.o_field_widget .o_pie").style.background.replaceAll(
105 /\s+/g,
106 " "
107 )
108 ).toBe(
109 "conic-gradient( var(--PercentPieField-color-active) 0% 33.3333%, var(--PercentPieField-color-static) 0% 100% )",
110 { message: "pie should have a background computed for its value of 33.3333%" }
111 );
112 });
113
114 test("hide the string when the PercentPieField widget is used in the view", async () => {
115 await mountView({
116 type: "form",
117 resModel: "partner",
118 arch: /* xml */ `
119 <form>
120 <sheet>
121 <group>
122 <field name="int_field" widget="percentpie"/>
123 </group>
124 </sheet>
125 </form>`,
126 resId: 1,
127 });
128
129 expect(".o_field_percent_pie.o_field_widget .o_pie").toHaveCount(1);
130 expect(".o_field_percent_pie.o_field_widget .o_pie_info .o_pie_text").not.toBeVisible();
131 });
132
133 test.tags("desktop");
134 test("show the string when the PercentPieField widget is used in a button with the class oe_stat_button", async () => {
135 await mountView({
136 type: "form",
137 resModel: "partner",
138 arch: /* xml */ `
139 <form>
140 <div name="button_box" class="oe_button_box">
141 <button type="object" class="oe_stat_button">
142 <field name="int_field" widget="percentpie"/>
143 </button>
144 </div>
145 </form>`,
146 resId: 1,
147 });
148
149 expect(".o_field_percent_pie.o_field_widget .o_pie").toHaveCount(1);
150 expect(".o_field_percent_pie.o_field_widget .o_pie_info .o_pie_text").toBeVisible();
151 });