| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { check, click } from "@odoo/hoot-dom"; |
| 3 |
import { animationFrame } from "@odoo/hoot-mock"; |
| 4 |
import { defineModels, fields, models, mountView, onRpc } from "@web/../tests/web_test_helpers"; |
| 5 |
|
| 6 |
class Partner extends models.Model { |
| 7 |
bar = fields.Boolean({ default: true }); |
| 8 |
|
| 9 |
_records = [{ id: 1, bar: false }]; |
| 10 |
} |
| 11 |
|
| 12 |
defineModels([Partner]); |
| 13 |
|
| 14 |
test("use BooleanToggleField in form view", async () => { |
| 15 |
await mountView({ |
| 16 |
resModel: "partner", |
| 17 |
resId: 1, |
| 18 |
type: "form", |
| 19 |
arch: `<form><field name="bar" widget="boolean_toggle"/></form>`, |
| 20 |
}); |
| 21 |
expect(`.form-check.o_boolean_toggle`).toHaveCount(1); |
| 22 |
expect(`.o_boolean_toggle input`).toBeEnabled(); |
| 23 |
expect(`.o_boolean_toggle input`).not.toBeChecked(); |
| 24 |
|
| 25 |
await check(`.o_field_widget[name='bar'] input`); |
| 26 |
await animationFrame(); |
| 27 |
expect(`.o_boolean_toggle input`).toBeEnabled(); |
| 28 |
expect(`.o_boolean_toggle input`).toBeChecked(); |
| 29 |
}); |
| 30 |
|
| 31 |
test("BooleanToggleField is disabled with a readonly attribute", async () => { |
| 32 |
await mountView({ |
| 33 |
resModel: "partner", |
| 34 |
resId: 1, |
| 35 |
type: "form", |
| 36 |
arch: `<form><field name="bar" widget="boolean_toggle" readonly="1"/></form>`, |
| 37 |
}); |
| 38 |
expect(`.form-check.o_boolean_toggle`).toHaveCount(1); |
| 39 |
expect(`.o_boolean_toggle input`).not.toBeEnabled(); |
| 40 |
}); |
| 41 |
|
| 42 |
test("BooleanToggleField is disabled if readonly in editable list", async () => { |
| 43 |
Partner._fields.bar.readonly = true; |
| 44 |
|
| 45 |
onRpc("has_group", () => true); |
| 46 |
await mountView({ |
| 47 |
resModel: "partner", |
| 48 |
type: "list", |
| 49 |
arch: ` |
| 50 |
<list editable="bottom"> |
| 51 |
<field name="bar" widget="boolean_toggle"/> |
| 52 |
</list> |
| 53 |
`, |
| 54 |
}); |
| 55 |
expect(`.o_boolean_toggle input`).not.toBeEnabled(); |
| 56 |
expect(`.o_boolean_toggle input`).not.toBeChecked(); |
| 57 |
|
| 58 |
await click(`.o_boolean_toggle`); |
| 59 |
await animationFrame(); |
| 60 |
expect(`.o_boolean_toggle input`).not.toBeEnabled(); |
| 61 |
expect(`.o_boolean_toggle input`).not.toBeChecked(); |
| 62 |
}); |
| 63 |
|
| 64 |
test("BooleanToggleField - auto save record when field toggled", async () => { |
| 65 |
onRpc("web_save", () => expect.step("web_save")); |
| 66 |
await mountView({ |
| 67 |
resModel: "partner", |
| 68 |
resId: 1, |
| 69 |
type: "form", |
| 70 |
arch: `<form><field name="bar" widget="boolean_toggle"/></form>`, |
| 71 |
}); |
| 72 |
await click(`.o_field_widget[name='bar'] input`); |
| 73 |
await animationFrame(); |
| 74 |
expect.verifySteps(["web_save"]); |
| 75 |
}); |
| 76 |
|
| 77 |
test("BooleanToggleField - autosave option set to false", async () => { |
| 78 |
onRpc("web_save", () => expect.step("web_save")); |
| 79 |
await mountView({ |
| 80 |
resModel: "partner", |
| 81 |
resId: 1, |
| 82 |
type: "form", |
| 83 |
arch: `<form><field name="bar" widget="boolean_toggle" options="{'autosave': false}"/></form>`, |
| 84 |
}); |
| 85 |
await click(`.o_field_widget[name='bar'] input`); |
| 86 |
await animationFrame(); |
| 87 |
expect.verifySteps([]); |
| 88 |
}); |