| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { check, click, press, uncheck } from "@odoo/hoot-dom"; |
| 3 |
import { animationFrame } from "@odoo/hoot-mock"; |
| 4 |
import { |
| 5 |
clickSave, |
| 6 |
defineModels, |
| 7 |
fields, |
| 8 |
models, |
| 9 |
mountView, |
| 10 |
onRpc, |
| 11 |
} from "@web/../tests/web_test_helpers"; |
| 12 |
|
| 13 |
class Partner extends models.Model { |
| 14 |
bar = fields.Boolean({ default: true }); |
| 15 |
|
| 16 |
_records = [ |
| 17 |
{ id: 1, bar: true }, |
| 18 |
{ id: 2, bar: true }, |
| 19 |
{ id: 3, bar: true }, |
| 20 |
{ id: 4, bar: true }, |
| 21 |
{ id: 5, bar: false }, |
| 22 |
]; |
| 23 |
} |
| 24 |
|
| 25 |
defineModels([Partner]); |
| 26 |
|
| 27 |
test("boolean field in form view", async () => { |
| 28 |
await mountView({ |
| 29 |
resModel: "partner", |
| 30 |
resId: 1, |
| 31 |
type: "form", |
| 32 |
arch: ` |
| 33 |
<form> |
| 34 |
<label for="bar" string="Awesome checkbox"/> |
| 35 |
<field name="bar"/> |
| 36 |
</form> |
| 37 |
`, |
| 38 |
}); |
| 39 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 40 |
expect(`.o_field_boolean input`).toBeEnabled(); |
| 41 |
|
| 42 |
await uncheck(`.o_field_boolean input`); |
| 43 |
await animationFrame(); |
| 44 |
expect(`.o_field_boolean input`).not.toBeChecked(); |
| 45 |
|
| 46 |
await clickSave(); |
| 47 |
expect(`.o_field_boolean input`).not.toBeChecked(); |
| 48 |
|
| 49 |
await check(`.o_field_boolean input`); |
| 50 |
await animationFrame(); |
| 51 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 52 |
|
| 53 |
await uncheck(`.o_field_boolean input`); |
| 54 |
await animationFrame(); |
| 55 |
expect(`.o_field_boolean input`).not.toBeChecked(); |
| 56 |
|
| 57 |
await click(`.o_form_view label:not(.form-check-label)`); |
| 58 |
await animationFrame(); |
| 59 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 60 |
|
| 61 |
await click(`.o_form_view label:not(.form-check-label)`); |
| 62 |
await animationFrame(); |
| 63 |
expect(`.o_field_boolean input`).not.toBeChecked(); |
| 64 |
|
| 65 |
await press("enter"); |
| 66 |
await animationFrame(); |
| 67 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 68 |
|
| 69 |
await press("enter"); |
| 70 |
await animationFrame(); |
| 71 |
expect(`.o_field_boolean input`).not.toBeChecked(); |
| 72 |
|
| 73 |
await press("enter"); |
| 74 |
await animationFrame(); |
| 75 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 76 |
|
| 77 |
await clickSave(); |
| 78 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 79 |
}); |
| 80 |
|
| 81 |
test("boolean field in editable list view", async () => { |
| 82 |
onRpc("has_group", () => true); |
| 83 |
|
| 84 |
await mountView({ |
| 85 |
resModel: "partner", |
| 86 |
type: "list", |
| 87 |
arch: `<list editable="bottom"><field name="bar"/></list>`, |
| 88 |
}); |
| 89 |
expect(`tbody td:not(.o_list_record_selector) .o-checkbox input`).toHaveCount(5); |
| 90 |
expect(`tbody td:not(.o_list_record_selector) .o-checkbox input:checked`).toHaveCount(4); |
| 91 |
|
| 92 |
// Edit a line |
| 93 |
const cell = `tr.o_data_row td:not(.o_list_record_selector):first`; |
| 94 |
expect(`${cell} .o-checkbox input:only`).toBeChecked(); |
| 95 |
expect(`${cell} .o-checkbox input:only`).not.toBeEnabled(); |
| 96 |
|
| 97 |
await click(`${cell} .o-checkbox`); |
| 98 |
await animationFrame(); |
| 99 |
expect(`tr.o_data_row:nth-child(1)`).toHaveClass("o_selected_row", { |
| 100 |
message: "the row is now selected, in edition", |
| 101 |
}); |
| 102 |
expect(`${cell} .o-checkbox input:only`).not.toBeChecked(); |
| 103 |
expect(`${cell} .o-checkbox input:only`).toBeEnabled(); |
| 104 |
|
| 105 |
await click(`${cell} .o-checkbox`); |
| 106 |
await click(cell); |
| 107 |
await animationFrame(); |
| 108 |
expect(`${cell} .o-checkbox input:only`).toBeChecked(); |
| 109 |
expect(`${cell} .o-checkbox input:only`).toBeEnabled(); |
| 110 |
|
| 111 |
await click(`${cell} .o-checkbox`); |
| 112 |
await animationFrame(); |
| 113 |
|
| 114 |
await click(`.o_list_button_save`); |
| 115 |
await animationFrame(); |
| 116 |
expect(`${cell} .o-checkbox input:only`).not.toBeChecked(); |
| 117 |
expect(`${cell} .o-checkbox input:only`).not.toBeEnabled(); |
| 118 |
expect(`tbody td:not(.o_list_record_selector) .o-checkbox input`).toHaveCount(5); |
| 119 |
expect(`tbody td:not(.o_list_record_selector) .o-checkbox input:checked`).toHaveCount(3); |
| 120 |
|
| 121 |
// Fake-check the checkbox |
| 122 |
await click(cell); |
| 123 |
await animationFrame(); |
| 124 |
await click(`${cell} .o-checkbox`); |
| 125 |
await animationFrame(); |
| 126 |
|
| 127 |
await click(`.o_list_button_save`); |
| 128 |
await animationFrame(); |
| 129 |
expect(`tbody td:not(.o_list_record_selector) .o-checkbox input`).toHaveCount(5); |
| 130 |
expect(`tbody td:not(.o_list_record_selector) .o-checkbox input:checked`).toHaveCount(3); |
| 131 |
}); |
| 132 |
|
| 133 |
test("readonly boolean field", async () => { |
| 134 |
await mountView({ |
| 135 |
resModel: "partner", |
| 136 |
resId: 1, |
| 137 |
type: "form", |
| 138 |
arch: `<form><field name="bar" readonly="1"/></form>`, |
| 139 |
}); |
| 140 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 141 |
expect(`.o_field_boolean input`).not.toBeEnabled(); |
| 142 |
|
| 143 |
await click(`.o_field_boolean .o-checkbox`); |
| 144 |
await animationFrame(); |
| 145 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 146 |
expect(`.o_field_boolean input`).not.toBeEnabled(); |
| 147 |
}); |
| 148 |
|
| 149 |
test("onchange return value before toggle checkbox", async () => { |
| 150 |
Partner._onChanges.bar = (record) => { |
| 151 |
record["bar"] = true; |
| 152 |
}; |
| 153 |
|
| 154 |
await mountView({ |
| 155 |
resModel: "partner", |
| 156 |
resId: 1, |
| 157 |
type: "form", |
| 158 |
arch: `<form><field name="bar"/></form>`, |
| 159 |
}); |
| 160 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 161 |
|
| 162 |
await click(`.o_field_boolean .o-checkbox`); |
| 163 |
await animationFrame(); |
| 164 |
await animationFrame(); |
| 165 |
expect(`.o_field_boolean input`).toBeChecked(); |
| 166 |
}); |