| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { contains, defineModels, fields, models, mountView } from "@web/../tests/web_test_helpers"; |
| 3 |
|
| 4 |
class Partner extends models.Model { |
| 5 |
foo = fields.Char({ default: "My little Foo Value" }); |
| 6 |
int_field = fields.Integer({ string: "int_field" }); |
| 7 |
qux = fields.Float({ digits: [16, 1] }); |
| 8 |
monetary = fields.Monetary({ currency_field: "" }); |
| 9 |
|
| 10 |
_records = [{ id: 1, foo: "yop", int_field: 10, qux: 0.44444, monetary: 9.999999 }]; |
| 11 |
} |
| 12 |
|
| 13 |
defineModels([Partner]); |
| 14 |
|
| 15 |
test("StatInfoField formats decimal precision", async () => { |
| 16 |
// sometimes the round method can return numbers such as 14.000001 |
| 17 |
// when asked to round a number to 2 decimals, as such is the behaviour of floats. |
| 18 |
// we check that even in that eventuality, only two decimals are displayed |
| 19 |
await mountView({ |
| 20 |
type: "form", |
| 21 |
resModel: "partner", |
| 22 |
resId: 1, |
| 23 |
arch: /* xml */ ` |
| 24 |
<form> |
| 25 |
<button class="oe_stat_button" name="items" icon="fa-gear"> |
| 26 |
<field name="qux" widget="statinfo" /> |
| 27 |
</button> |
| 28 |
<button class="oe_stat_button" name="money" icon="fa-money"> |
| 29 |
<field name="monetary" widget="statinfo" /> |
| 30 |
</button> |
| 31 |
</form> |
| 32 |
`, |
| 33 |
}); |
| 34 |
|
| 35 |
// formatFloat renders according to this.field.digits |
| 36 |
expect("button.oe_stat_button .o_field_widget .o_stat_value:eq(0)").toHaveText("0.4", { |
| 37 |
message: "Default precision should be [16,1]", |
| 38 |
}); |
| 39 |
expect("button.oe_stat_button .o_field_widget .o_stat_value:eq(1)").toHaveText("10.00", { |
| 40 |
message: "Currency decimal precision should be 2", |
| 41 |
}); |
| 42 |
}); |
| 43 |
|
| 44 |
test("StatInfoField widget on a chart fields", async () => { |
| 45 |
await mountView({ |
| 46 |
type: "form", |
| 47 |
resModel: "partner", |
| 48 |
resId: 1, |
| 49 |
arch: /* xml */ ` |
| 50 |
<form> |
| 51 |
<button class="oe_stat_button" name="items" icon="fa-gear"> |
| 52 |
<field name="foo" widget="statinfo" /> |
| 53 |
</button> |
| 54 |
</form> |
| 55 |
`, |
| 56 |
}); |
| 57 |
|
| 58 |
expect("button.oe_stat_button .o_field_widget .o_stat_value:eq(0)").toHaveText("yop"); |
| 59 |
}); |
| 60 |
|
| 61 |
test("StatInfoField widget on a char field (unset value)", async () => { |
| 62 |
Partner._records = [{ id: 1, foo: "" }]; |
| 63 |
await mountView({ |
| 64 |
type: "form", |
| 65 |
resModel: "partner", |
| 66 |
resId: 1, |
| 67 |
arch: /* xml */ ` |
| 68 |
<form> |
| 69 |
<button class="oe_stat_button" name="items" icon="fa-gear"> |
| 70 |
<field name="foo" widget="statinfo" /> |
| 71 |
</button> |
| 72 |
</form> |
| 73 |
`, |
| 74 |
}); |
| 75 |
|
| 76 |
expect("button.oe_stat_button .o_field_widget .o_stat_value:eq(0)").toHaveText(""); |
| 77 |
}); |
| 78 |
|
| 79 |
test("StatInfoField widget on a one2many field (one record)", async () => { |
| 80 |
Partner._fields.child_ids = fields.One2many({ string: "one2many field", relation: "partner" }); |
| 81 |
Partner._records.push({ id: 2, foo: "plop", child_ids: [1] }); |
| 82 |
await mountView({ |
| 83 |
type: "form", |
| 84 |
resModel: "partner", |
| 85 |
resId: 2, |
| 86 |
arch: /* xml */ ` |
| 87 |
<form> |
| 88 |
<button class="oe_stat_button" name="items" icon="fa-gear"> |
| 89 |
<field name="child_ids" widget="statinfo" /> |
| 90 |
</button> |
| 91 |
</form> |
| 92 |
`, |
| 93 |
}); |
| 94 |
|
| 95 |
expect("button.oe_stat_button .o_field_widget .o_stat_value:eq(0)").toHaveText("1 record"); |
| 96 |
}); |
| 97 |
|
| 98 |
test("StatInfoField widget on a one2many field (multiple records)", async () => { |
| 99 |
Partner._fields.child_ids = fields.One2many({ string: "one2many field", relation: "partner" }); |
| 100 |
Partner._records.push({ id: 3, foo: "plop3" }); |
| 101 |
Partner._records.push({ id: 4, foo: "plop4" }); |
| 102 |
Partner._records.push({ id: 2, foo: "plop", child_ids: [1, 3, 4] }); |
| 103 |
await mountView({ |
| 104 |
type: "form", |
| 105 |
resModel: "partner", |
| 106 |
resId: 2, |
| 107 |
arch: /* xml */ ` |
| 108 |
<form> |
| 109 |
<button class="oe_stat_button" name="items" icon="fa-gear"> |
| 110 |
<field name="child_ids" widget="statinfo" /> |
| 111 |
</button> |
| 112 |
</form> |
| 113 |
`, |
| 114 |
}); |
| 115 |
|
| 116 |
expect("button.oe_stat_button .o_field_widget .o_stat_value:eq(0)").toHaveText("3 records"); |
| 117 |
}); |
| 118 |
|
| 119 |
test("StatInfoField widget on a many2one field", async () => { |
| 120 |
Partner._fields.name = fields.Char(); |
| 121 |
Partner._fields.parent_id = fields.Many2one({ relation: "partner" }); |
| 122 |
Partner._records[0].name = "Parent"; |
| 123 |
Partner._records.push({ id: 2, name: "child", parent_id: 1 }); |
| 124 |
await mountView({ |
| 125 |
type: "form", |
| 126 |
resModel: "partner", |
| 127 |
resId: 2, |
| 128 |
arch: /* xml */ ` |
| 129 |
<form> |
| 130 |
<button class="oe_stat_button" name="items" icon="fa-gear"> |
| 131 |
<field name="parent_id" widget="statinfo" /> |
| 132 |
</button> |
| 133 |
</form> |
| 134 |
`, |
| 135 |
}); |
| 136 |
|
| 137 |
expect("button.oe_stat_button .o_field_widget .o_stat_value:eq(0)").toHaveText("Parent"); |
| 138 |
}); |
| 139 |
|
| 140 |
test.tags("desktop"); |
| 141 |
test("StatInfoField in form view on desktop", async () => { |
| 142 |
await mountView({ |
| 143 |
type: "form", |
| 144 |
resModel: "partner", |
| 145 |
resId: 1, |
| 146 |
arch: /* xml */ ` |
| 147 |
<form> |
| 148 |
<div class="oe_button_box" name="button_box"> |
| 149 |
<button class="oe_stat_button" name="items" type="object" icon="fa-gear"> |
| 150 |
<field name="int_field" widget="statinfo" /> |
| 151 |
</button> |
| 152 |
</div> |
| 153 |
</form> |
| 154 |
`, |
| 155 |
}); |
| 156 |
|
| 157 |
expect("button.oe_stat_button .o_field_widget .o_stat_info").toHaveCount(1, { |
| 158 |
message: "should have one stat button", |
| 159 |
}); |
| 160 |
expect("button.oe_stat_button .o_field_widget .o_stat_value").toHaveText("10", { |
| 161 |
message: "should have 10 as value", |
| 162 |
}); |
| 163 |
expect("button.oe_stat_button .o_field_widget .o_stat_text").toHaveText("int_field", { |
| 164 |
message: "should have 'int_field' as text", |
| 165 |
}); |
| 166 |
}); |
| 167 |
|
| 168 |
test.tags("mobile"); |
| 169 |
test("StatInfoField in form view on mobile", async () => { |
| 170 |
await mountView({ |
| 171 |
type: "form", |
| 172 |
resModel: "partner", |
| 173 |
resId: 1, |
| 174 |
arch: /* xml */ ` |
| 175 |
<form> |
| 176 |
<div class="oe_button_box" name="button_box"> |
| 177 |
<button class="oe_stat_button" name="items" type="object" icon="fa-gear"> |
| 178 |
<field name="int_field" widget="statinfo" /> |
| 179 |
</button> |
| 180 |
</div> |
| 181 |
</form> |
| 182 |
`, |
| 183 |
}); |
| 184 |
|
| 185 |
await contains(".o-form-buttonbox .o_button_more").click(); |
| 186 |
expect("button.oe_stat_button .o_field_widget .o_stat_info").toHaveCount(1, { |
| 187 |
message: "should have one stat button", |
| 188 |
}); |
| 189 |
expect("button.oe_stat_button .o_field_widget .o_stat_value").toHaveText("10", { |
| 190 |
message: "should have 10 as value", |
| 191 |
}); |
| 192 |
expect("button.oe_stat_button .o_field_widget .o_stat_text").toHaveText("int_field", { |
| 193 |
message: "should have 'int_field' as text", |
| 194 |
}); |
| 195 |
}); |
| 196 |
|
| 197 |
test.tags("desktop"); |
| 198 |
test("StatInfoField in form view with specific label_field on desktop", async () => { |
| 199 |
await mountView({ |
| 200 |
type: "form", |
| 201 |
resModel: "partner", |
| 202 |
resId: 1, |
| 203 |
arch: /* xml */ ` |
| 204 |
<form> |
| 205 |
<sheet> |
| 206 |
<div class="oe_button_box" name="button_box"> |
| 207 |
<button class="oe_stat_button" name="items" type="object" icon="fa-gear"> |
| 208 |
<field string="Useful stat button" name="int_field" widget="statinfo" options="{'label_field': 'foo'}" /> |
| 209 |
</button> |
| 210 |
</div> |
| 211 |
<group> |
| 212 |
<field name="foo" invisible="1" /> |
| 213 |
</group> |
| 214 |
</sheet> |
| 215 |
</form> |
| 216 |
`, |
| 217 |
}); |
| 218 |
|
| 219 |
expect("button.oe_stat_button .o_field_widget .o_stat_info").toHaveCount(1, { |
| 220 |
message: "should have one stat button", |
| 221 |
}); |
| 222 |
expect("button.oe_stat_button .o_field_widget .o_stat_value").toHaveText("10", { |
| 223 |
message: "should have 10 as value", |
| 224 |
}); |
| 225 |
expect("button.oe_stat_button .o_field_widget .o_stat_text").toHaveText("yop", { |
| 226 |
message: "should have 'yop' as text, since it is the value of field foo", |
| 227 |
}); |
| 228 |
}); |
| 229 |
|
| 230 |
test.tags("mobile"); |
| 231 |
test("StatInfoField in form view with specific label_field on mobile", async () => { |
| 232 |
await mountView({ |
| 233 |
type: "form", |
| 234 |
resModel: "partner", |
| 235 |
resId: 1, |
| 236 |
arch: /* xml */ ` |
| 237 |
<form> |
| 238 |
<sheet> |
| 239 |
<div class="oe_button_box" name="button_box"> |
| 240 |
<button class="oe_stat_button" name="items" type="object" icon="fa-gear"> |
| 241 |
<field string="Useful stat button" name="int_field" widget="statinfo" options="{'label_field': 'foo'}" /> |
| 242 |
</button> |
| 243 |
</div> |
| 244 |
<group> |
| 245 |
<field name="foo" invisible="1" /> |
| 246 |
</group> |
| 247 |
</sheet> |
| 248 |
</form> |
| 249 |
`, |
| 250 |
}); |
| 251 |
|
| 252 |
await contains(".o-form-buttonbox .o_button_more").click(); |
| 253 |
expect("button.oe_stat_button .o_field_widget .o_stat_info").toHaveCount(1, { |
| 254 |
message: "should have one stat button", |
| 255 |
}); |
| 256 |
expect("button.oe_stat_button .o_field_widget .o_stat_value").toHaveText("10", { |
| 257 |
message: "should have 10 as value", |
| 258 |
}); |
| 259 |
expect("button.oe_stat_button .o_field_widget .o_stat_text").toHaveText("yop", { |
| 260 |
message: "should have 'yop' as text, since it is the value of field foo", |
| 261 |
}); |
| 262 |
}); |
| 263 |
|
| 264 |
test.tags("desktop"); |
| 265 |
test("StatInfoField in form view with no label on desktop", async () => { |
| 266 |
await mountView({ |
| 267 |
type: "form", |
| 268 |
resModel: "partner", |
| 269 |
resId: 1, |
| 270 |
arch: /* xml */ ` |
| 271 |
<form> |
| 272 |
<sheet> |
| 273 |
<div class="oe_button_box" name="button_box"> |
| 274 |
<button class="oe_stat_button" name="items" type="object" icon="fa-gear"> |
| 275 |
<field string="Useful stat button" name="int_field" widget="statinfo" nolabel="1" /> |
| 276 |
</button> |
| 277 |
</div> |
| 278 |
</sheet> |
| 279 |
</form> |
| 280 |
`, |
| 281 |
}); |
| 282 |
expect("button.oe_stat_button .o_field_widget .o_stat_info").toHaveCount(1, { |
| 283 |
message: "should have one stat button", |
| 284 |
}); |
| 285 |
expect("button.oe_stat_button .o_field_widget .o_stat_value").toHaveText("10", { |
| 286 |
message: "should have 10 as value", |
| 287 |
}); |
| 288 |
expect("button.oe_stat_button .o_field_widget .o_stat_text").toHaveCount(0, { |
| 289 |
message: "should not have any label", |
| 290 |
}); |
| 291 |
}); |
| 292 |
|
| 293 |
test.tags("mobile"); |
| 294 |
test("StatInfoField in form view with no label on mobile", async () => { |
| 295 |
await mountView({ |
| 296 |
type: "form", |
| 297 |
resModel: "partner", |
| 298 |
resId: 1, |
| 299 |
arch: /* xml */ ` |
| 300 |
<form> |
| 301 |
<sheet> |
| 302 |
<div class="oe_button_box" name="button_box"> |
| 303 |
<button class="oe_stat_button" name="items" type="object" icon="fa-gear"> |
| 304 |
<field string="Useful stat button" name="int_field" widget="statinfo" nolabel="1" /> |
| 305 |
</button> |
| 306 |
</div> |
| 307 |
</sheet> |
| 308 |
</form> |
| 309 |
`, |
| 310 |
}); |
| 311 |
await contains(".o-form-buttonbox .o_button_more").click(); |
| 312 |
expect("button.oe_stat_button .o_field_widget .o_stat_info").toHaveCount(1, { |
| 313 |
message: "should have one stat button", |
| 314 |
}); |
| 315 |
expect("button.oe_stat_button .o_field_widget .o_stat_value").toHaveText("10", { |
| 316 |
message: "should have 10 as value", |
| 317 |
}); |
| 318 |
expect("button.oe_stat_button .o_field_widget .o_stat_text").toHaveCount(0, { |
| 319 |
message: "should not have any label", |
| 320 |
}); |
| 321 |
}); |