| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { check, click, queryRect } 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 |
int_field = fields.Integer(); |
| 16 |
trululu = fields.Many2one({ relation: "partner" }); |
| 17 |
product_id = fields.Many2one({ relation: "product" }); |
| 18 |
color = fields.Selection({ |
| 19 |
selection: [ |
| 20 |
["red", "Red"], |
| 21 |
["black", "Black"], |
| 22 |
], |
| 23 |
default: "red", |
| 24 |
}); |
| 25 |
_records = [ |
| 26 |
{ |
| 27 |
id: 1, |
| 28 |
display_name: "first record", |
| 29 |
bar: true, |
| 30 |
int_field: 10, |
| 31 |
}, |
| 32 |
{ |
| 33 |
id: 2, |
| 34 |
display_name: "second record", |
| 35 |
}, |
| 36 |
{ |
| 37 |
id: 3, |
| 38 |
display_name: "third record", |
| 39 |
}, |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
class Product extends models.Model { |
| 44 |
display_name = fields.Char(); |
| 45 |
_records = [ |
| 46 |
{ |
| 47 |
id: 37, |
| 48 |
display_name: "xphone", |
| 49 |
}, |
| 50 |
{ |
| 51 |
id: 41, |
| 52 |
display_name: "xpad", |
| 53 |
}, |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
defineModels([Partner, Product]); |
| 58 |
|
| 59 |
test("radio field on a many2one in a new record", async () => { |
| 60 |
await mountView({ |
| 61 |
type: "form", |
| 62 |
resModel: "partner", |
| 63 |
arch: /* xml */ `<form><field name="product_id" widget="radio"/></form>`, |
| 64 |
}); |
| 65 |
|
| 66 |
expect("div.o_radio_item").toHaveCount(2); |
| 67 |
expect("input.o_radio_input").toHaveCount(2); |
| 68 |
expect(".o_field_radio:first").toHaveText("xphone\nxpad"); |
| 69 |
expect("input.o_radio_input:checked").toHaveCount(0); |
| 70 |
}); |
| 71 |
|
| 72 |
test("required radio field on a many2one", async () => { |
| 73 |
await mountView({ |
| 74 |
type: "form", |
| 75 |
resModel: "partner", |
| 76 |
arch: /* xml */ `<form><field name="product_id" widget="radio" required="1"/></form>`, |
| 77 |
}); |
| 78 |
|
| 79 |
expect(".o_field_radio input:checked").toHaveCount(0); |
| 80 |
await clickSave(); |
| 81 |
expect(".o_notification_content:first").toHaveText("Missing required fields"); |
| 82 |
expect(".o_notification_bar:first").toHaveClass("bg-danger"); |
| 83 |
}); |
| 84 |
|
| 85 |
test("radio field change value by onchange", async () => { |
| 86 |
Partner._fields.bar = fields.Boolean({ |
| 87 |
default: true, |
| 88 |
onChange: (obj) => { |
| 89 |
obj.product_id = obj.bar ? [41] : [37]; |
| 90 |
obj.color = obj.bar ? "red" : "black"; |
| 91 |
}, |
| 92 |
}); |
| 93 |
|
| 94 |
await mountView({ |
| 95 |
type: "form", |
| 96 |
resModel: "partner", |
| 97 |
arch: /* xml */ ` |
| 98 |
<form> |
| 99 |
<field name="bar" /> |
| 100 |
<field name="product_id" widget="radio" /> |
| 101 |
<field name="color" widget="radio" /> |
| 102 |
</form> |
| 103 |
`, |
| 104 |
}); |
| 105 |
|
| 106 |
await click(".o_field_boolean input[type='checkbox']"); |
| 107 |
await animationFrame(); |
| 108 |
expect("input.o_radio_input[data-value='37']").toBeChecked(); |
| 109 |
expect("input.o_radio_input[data-value='black']").toBeChecked(); |
| 110 |
|
| 111 |
await click(".o_field_boolean input[type='checkbox']"); |
| 112 |
await animationFrame(); |
| 113 |
expect("input.o_radio_input[data-value='41']").toBeChecked(); |
| 114 |
expect("input.o_radio_input[data-value='red']").toBeChecked(); |
| 115 |
}); |
| 116 |
|
| 117 |
test("radio field on a selection in a new record", async () => { |
| 118 |
await mountView({ |
| 119 |
type: "form", |
| 120 |
resModel: "partner", |
| 121 |
arch: /* xml */ `<form><field name="color" widget="radio"/></form>`, |
| 122 |
}); |
| 123 |
|
| 124 |
expect("div.o_radio_item").toHaveCount(2); |
| 125 |
expect("input.o_radio_input").toHaveCount(2, { message: "should have 2 possible choices" }); |
| 126 |
expect(".o_field_radio").toHaveText("Red\nBlack"); |
| 127 |
|
| 128 |
// click on 2nd option |
| 129 |
await click("input.o_radio_input:eq(1)"); |
| 130 |
await animationFrame(); |
| 131 |
|
| 132 |
await clickSave(); |
| 133 |
|
| 134 |
expect("input.o_radio_input[data-value=black]").toBeChecked({ |
| 135 |
message: "should have saved record with correct value", |
| 136 |
}); |
| 137 |
}); |
| 138 |
|
| 139 |
test("two radio field with same selection", async () => { |
| 140 |
Partner._fields.color_2 = { ...Partner._fields.color }; |
| 141 |
Partner._records[0].color = "black"; |
| 142 |
Partner._records[0].color_2 = "black"; |
| 143 |
|
| 144 |
await mountView({ |
| 145 |
type: "form", |
| 146 |
resModel: "partner", |
| 147 |
resId: 1, |
| 148 |
arch: /* xml */ ` |
| 149 |
<form> |
| 150 |
<group> |
| 151 |
<field name="color" widget="radio"/> |
| 152 |
</group> |
| 153 |
<group> |
| 154 |
<field name="color_2" widget="radio"/> |
| 155 |
</group> |
| 156 |
</form> |
| 157 |
`, |
| 158 |
}); |
| 159 |
|
| 160 |
expect("[name='color'] input.o_radio_input[data-value=black]").toBeChecked(); |
| 161 |
expect("[name='color_2'] input.o_radio_input[data-value=black]").toBeChecked(); |
| 162 |
|
| 163 |
// click on Red |
| 164 |
await click("[name='color_2'] label"); |
| 165 |
await animationFrame(); |
| 166 |
|
| 167 |
expect("[name='color'] input.o_radio_input[data-value=black]").toBeChecked(); |
| 168 |
expect("[name='color_2'] input.o_radio_input[data-value=red]").toBeChecked(); |
| 169 |
}); |
| 170 |
|
| 171 |
test("radio field has o_horizontal or o_vertical class", async () => { |
| 172 |
Partner._fields.color2 = Partner._fields.color; |
| 173 |
|
| 174 |
await mountView({ |
| 175 |
type: "form", |
| 176 |
resModel: "partner", |
| 177 |
arch: /* xml */ ` |
| 178 |
<form> |
| 179 |
<group> |
| 180 |
<field name="color" widget="radio" /> |
| 181 |
<field name="color2" widget="radio" options="{'horizontal': True}" /> |
| 182 |
</group> |
| 183 |
</form> |
| 184 |
`, |
| 185 |
}); |
| 186 |
|
| 187 |
expect(".o_field_radio > div.o_vertical").toHaveCount(1, { |
| 188 |
message: "should have o_vertical class", |
| 189 |
}); |
| 190 |
|
| 191 |
const verticalRadio = ".o_field_radio > div.o_vertical:first"; |
| 192 |
expect(`${verticalRadio} .o_radio_item:first`).toHaveRect({ |
| 193 |
right: queryRect(`${verticalRadio} .o_radio_item:last`).right, |
| 194 |
}); |
| 195 |
expect(".o_field_radio > div.o_horizontal").toHaveCount(1, { |
| 196 |
message: "should have o_horizontal class", |
| 197 |
}); |
| 198 |
const horizontalRadio = ".o_field_radio > div.o_horizontal:first"; |
| 199 |
expect(`${horizontalRadio} .o_radio_item:first`).toHaveRect({ |
| 200 |
top: queryRect(`${horizontalRadio} .o_radio_item:last`).top, |
| 201 |
}); |
| 202 |
}); |
| 203 |
|
| 204 |
test("radio field with numerical keys encoded as strings", async () => { |
| 205 |
Partner._fields.selection = fields.Selection({ |
| 206 |
selection: [ |
| 207 |
["0", "Red"], |
| 208 |
["1", "Black"], |
| 209 |
], |
| 210 |
}); |
| 211 |
|
| 212 |
onRpc("partner", "web_save", ({ args }) => expect.step(args[1].selection)); |
| 213 |
|
| 214 |
await mountView({ |
| 215 |
type: "form", |
| 216 |
resModel: "partner", |
| 217 |
resId: 1, |
| 218 |
arch: /* xml */ `<form><field name="selection" widget="radio"/></form>`, |
| 219 |
}); |
| 220 |
expect(".o_field_widget").toHaveText("Red\nBlack"); |
| 221 |
expect(".o_radio_input:checked").toHaveCount(0); |
| 222 |
|
| 223 |
await check("input.o_radio_input:last"); |
| 224 |
await animationFrame(); |
| 225 |
await clickSave(); |
| 226 |
|
| 227 |
expect(".o_field_widget").toHaveText("Red\nBlack"); |
| 228 |
expect(".o_radio_input[data-value='1']").toBeChecked(); |
| 229 |
|
| 230 |
expect.verifySteps(["1"]); |
| 231 |
}); |
| 232 |
|
| 233 |
test("radio field is empty", async () => { |
| 234 |
await mountView({ |
| 235 |
type: "form", |
| 236 |
resModel: "partner", |
| 237 |
resId: 2, |
| 238 |
arch: /* xml */ ` |
| 239 |
<form edit="0"> |
| 240 |
<field name="trululu" widget="radio" /> |
| 241 |
</form> |
| 242 |
`, |
| 243 |
}); |
| 244 |
|
| 245 |
expect(".o_field_widget[name=trululu]").toHaveClass("o_field_empty"); |
| 246 |
expect(".o_radio_input").toHaveCount(3); |
| 247 |
expect(".o_radio_input:disabled").toHaveCount(3); |
| 248 |
expect(".o_radio_input:checked").toHaveCount(0); |
| 249 |
}); |