| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { runAllTimers } from "@odoo/hoot-mock"; |
| 3 |
import { |
| 4 |
clickSave, |
| 5 |
contains, |
| 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 |
int_field = fields.Integer({ sortable: true }); |
| 15 |
json_checkboxes_field = fields.Json({ string: "Json Checkboxes Field" }); |
| 16 |
_records = [ |
| 17 |
{ |
| 18 |
id: 1, |
| 19 |
int_field: 10, |
| 20 |
json_checkboxes_field: { |
| 21 |
key1: { checked: true, label: "First Key" }, |
| 22 |
key2: { checked: false, label: "Second Key" }, |
| 23 |
}, |
| 24 |
}, |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
defineModels([Partner]); |
| 29 |
|
| 30 |
test("JsonCheckBoxesField", async () => { |
| 31 |
const commands = [ |
| 32 |
{ |
| 33 |
key1: { checked: true, label: "First Key" }, |
| 34 |
key2: { checked: true, label: "Second Key" }, |
| 35 |
}, |
| 36 |
{ |
| 37 |
key1: { checked: false, label: "First Key" }, |
| 38 |
key2: { checked: true, label: "Second Key" }, |
| 39 |
}, |
| 40 |
]; |
| 41 |
onRpc("web_save", (args) => { |
| 42 |
expect.step("web_save"); |
| 43 |
expect(args.args[1].json_checkboxes_field).toEqual(commands.shift()); |
| 44 |
}); |
| 45 |
await mountView({ |
| 46 |
type: "form", |
| 47 |
resModel: "partner", |
| 48 |
resId: 1, |
| 49 |
arch: ` |
| 50 |
<form> |
| 51 |
<group> |
| 52 |
<field name="json_checkboxes_field" widget="json_checkboxes" /> |
| 53 |
</group> |
| 54 |
</form>`, |
| 55 |
}); |
| 56 |
|
| 57 |
expect("div.o_field_widget div.form-check").toHaveCount(2); |
| 58 |
|
| 59 |
expect("div.o_field_widget div.form-check input:eq(0)").toBeChecked(); |
| 60 |
expect("div.o_field_widget div.form-check input:eq(1)").not.toBeChecked(); |
| 61 |
|
| 62 |
expect("div.o_field_widget div.form-check input:disabled").toHaveCount(0); |
| 63 |
|
| 64 |
// check a value by clicking on input |
| 65 |
await contains("div.o_field_widget div.form-check input:eq(1)").click(); |
| 66 |
await runAllTimers(); |
| 67 |
await clickSave(); |
| 68 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(2); |
| 69 |
|
| 70 |
// uncheck a value by clicking on label |
| 71 |
await contains("div.o_field_widget div.form-check > label").click(); |
| 72 |
await runAllTimers(); |
| 73 |
await clickSave(); |
| 74 |
expect("div.o_field_widget div.form-check input:eq(0)").not.toBeChecked(); |
| 75 |
expect("div.o_field_widget div.form-check input:eq(1)").toBeChecked(); |
| 76 |
|
| 77 |
expect.verifySteps(["web_save", "web_save"]); |
| 78 |
}); |
| 79 |
|
| 80 |
test("JsonCheckBoxesField (readonly field)", async () => { |
| 81 |
await mountView({ |
| 82 |
type: "form", |
| 83 |
resModel: "partner", |
| 84 |
resId: 1, |
| 85 |
arch: ` |
| 86 |
<form> |
| 87 |
<group> |
| 88 |
<field name="json_checkboxes_field" widget="json_checkboxes" readonly="True" /> |
| 89 |
</group> |
| 90 |
</form>`, |
| 91 |
}); |
| 92 |
|
| 93 |
expect("div.o_field_widget div.form-check").toHaveCount(2, { |
| 94 |
message: "should have fetched and displayed the 2 values of the many2many", |
| 95 |
}); |
| 96 |
expect("div.o_field_widget div.form-check input:disabled").toHaveCount(2, { |
| 97 |
message: "the checkboxes should be disabled", |
| 98 |
}); |
| 99 |
|
| 100 |
await contains("div.o_field_widget div.form-check > label:eq(1)").click(); |
| 101 |
|
| 102 |
expect("div.o_field_widget div.form-check input:eq(0)").toBeChecked(); |
| 103 |
expect("div.o_field_widget div.form-check input:eq(1)").not.toBeChecked(); |
| 104 |
}); |
| 105 |
|
| 106 |
test("JsonCheckBoxesField (some readonly)", async () => { |
| 107 |
Partner._records[0].json_checkboxes_field = { |
| 108 |
key1: { checked: true, label: "First Key" }, |
| 109 |
key2: { checked: false, readonly: true, label: "Second Key" }, |
| 110 |
}; |
| 111 |
await mountView({ |
| 112 |
type: "form", |
| 113 |
resModel: "partner", |
| 114 |
resId: 1, |
| 115 |
arch: ` |
| 116 |
<form> |
| 117 |
<group> |
| 118 |
<field name="json_checkboxes_field" widget="json_checkboxes" /> |
| 119 |
</group> |
| 120 |
</form>`, |
| 121 |
}); |
| 122 |
|
| 123 |
expect("div.o_field_widget div.form-check").toHaveCount(2, { |
| 124 |
message: "should have fetched and displayed the 2 values of the many2many", |
| 125 |
}); |
| 126 |
expect("div.o_field_widget div.form-check input:eq(0):enabled").toHaveCount(1, { |
| 127 |
message: "first checkbox should be enabled", |
| 128 |
}); |
| 129 |
expect("div.o_field_widget div.form-check input:eq(1):disabled").toHaveCount(1, { |
| 130 |
message: "second checkbox should be disabled", |
| 131 |
}); |
| 132 |
|
| 133 |
await contains("div.o_field_widget div.form-check > label:eq(1)").click(); |
| 134 |
|
| 135 |
expect("div.o_field_widget div.form-check input:eq(0)").toBeChecked(); |
| 136 |
expect("div.o_field_widget div.form-check input:eq(1)").not.toBeChecked(); |
| 137 |
}); |
| 138 |
|
| 139 |
test("JsonCheckBoxesField (question circle)", async () => { |
| 140 |
Partner._records[0].json_checkboxes_field = { |
| 141 |
key1: { checked: true, label: "First Key" }, |
| 142 |
key2: { checked: false, label: "Second Key", question_circle: "Some info about this" }, |
| 143 |
}; |
| 144 |
await mountView({ |
| 145 |
type: "form", |
| 146 |
resModel: "partner", |
| 147 |
resId: 1, |
| 148 |
arch: ` |
| 149 |
<form> |
| 150 |
<group> |
| 151 |
<field name="json_checkboxes_field" widget="json_checkboxes" /> |
| 152 |
</group> |
| 153 |
</form>`, |
| 154 |
}); |
| 155 |
|
| 156 |
expect("div.o_field_widget div.form-check:eq(0) ~ i.fa-question-circle").toHaveCount(0, { |
| 157 |
message: "first checkbox should not have a question circle", |
| 158 |
}); |
| 159 |
expect( |
| 160 |
"div.o_field_widget div.form-check:eq(1) ~ i.fa-question-circle[title='Some info about this']" |
| 161 |
).toHaveCount(1, { |
| 162 |
message: "second checkbox should have a question circle", |
| 163 |
}); |
| 164 |
}); |
| 165 |
|
| 166 |
test("JsonCheckBoxesField (implicit inline mode)", async () => { |
| 167 |
await mountView({ |
| 168 |
type: "form", |
| 169 |
resModel: "partner", |
| 170 |
resId: 1, |
| 171 |
arch: ` |
| 172 |
<form> |
| 173 |
<group> |
| 174 |
<field name="json_checkboxes_field" widget="json_checkboxes" /> |
| 175 |
</group> |
| 176 |
</form>`, |
| 177 |
}); |
| 178 |
|
| 179 |
expect("div.o_field_widget .d-inline-block div.form-check").toHaveCount(2, { |
| 180 |
message: "should show the checkboxes in inlined mode", |
| 181 |
}); |
| 182 |
}); |
| 183 |
|
| 184 |
test("JsonCheckBoxesField (explicit inline mode)", async () => { |
| 185 |
await mountView({ |
| 186 |
type: "form", |
| 187 |
resModel: "partner", |
| 188 |
resId: 1, |
| 189 |
arch: ` |
| 190 |
<form> |
| 191 |
<group> |
| 192 |
<field name="json_checkboxes_field" widget="json_checkboxes" options="{'stacked': 0}" /> |
| 193 |
</group> |
| 194 |
</form>`, |
| 195 |
}); |
| 196 |
|
| 197 |
expect("div.o_field_widget .d-inline-block div.form-check").toHaveCount(2, { |
| 198 |
message: "should show the checkboxes in inlined mode", |
| 199 |
}); |
| 200 |
}); |
| 201 |
|
| 202 |
test("JsonCheckBoxesField (stacked mode)", async () => { |
| 203 |
await mountView({ |
| 204 |
type: "form", |
| 205 |
resModel: "partner", |
| 206 |
resId: 1, |
| 207 |
arch: ` |
| 208 |
<form> |
| 209 |
<group> |
| 210 |
<field name="json_checkboxes_field" widget="json_checkboxes" options="{'stacked': 1}" /> |
| 211 |
</group> |
| 212 |
</form>`, |
| 213 |
}); |
| 214 |
|
| 215 |
expect("div.o_field_widget .d-block div.form-check").toHaveCount(2, { |
| 216 |
message: "should show the checkboxes in stacked mode", |
| 217 |
}); |
| 218 |
}); |