| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { 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 |
foo = fields.Char(); |
| 8 |
selection = fields.Selection({ |
| 9 |
selection: [ |
| 10 |
["normal", "Normal"], |
| 11 |
["blocked", "Blocked"], |
| 12 |
["done", "Done"], |
| 13 |
], |
| 14 |
}); |
| 15 |
_records = [ |
| 16 |
{ |
| 17 |
foo: "yop", |
| 18 |
selection: "blocked", |
| 19 |
}, |
| 20 |
{ |
| 21 |
foo: "blip", |
| 22 |
selection: "normal", |
| 23 |
}, |
| 24 |
{ |
| 25 |
foo: "abc", |
| 26 |
selection: "done", |
| 27 |
}, |
| 28 |
]; |
| 29 |
} |
| 30 |
defineModels([Partner]); |
| 31 |
|
| 32 |
test("LabelSelectionField in form view", async () => { |
| 33 |
await mountView({ |
| 34 |
type: "form", |
| 35 |
resModel: "partner", |
| 36 |
arch: /* xml */ ` |
| 37 |
<form> |
| 38 |
<sheet> |
| 39 |
<group> |
| 40 |
<field name="selection" widget="label_selection" |
| 41 |
options="{'classes': {'normal': 'secondary', 'blocked': 'warning','done': 'success'}}"/> |
| 42 |
</group> |
| 43 |
</sheet> |
| 44 |
</form>`, |
| 45 |
resId: 1, |
| 46 |
}); |
| 47 |
|
| 48 |
expect(".o_field_widget .badge.text-bg-warning").toHaveCount(1, { |
| 49 |
message: "should have a warning status label since selection is the second, blocked state", |
| 50 |
}); |
| 51 |
expect(".o_field_widget .badge.text-bg-secondary").toHaveCount(0, { |
| 52 |
message: "should not have a default status since selection is the second, blocked state", |
| 53 |
}); |
| 54 |
expect(".o_field_widget .badge.text-bg-success").toHaveCount(0, { |
| 55 |
message: "should not have a success status since selection is the second, blocked state", |
| 56 |
}); |
| 57 |
expect(".o_field_widget .badge.text-bg-warning").toHaveText("Blocked", { |
| 58 |
message: "the label should say 'Blocked' since this is the label value for that state", |
| 59 |
}); |
| 60 |
}); |
| 61 |
|
| 62 |
test("LabelSelectionField in editable list view", async () => { |
| 63 |
onRpc("has_group", () => true); |
| 64 |
|
| 65 |
await mountView({ |
| 66 |
type: "list", |
| 67 |
resModel: "partner", |
| 68 |
arch: /* xml */ ` |
| 69 |
<list editable="bottom"> |
| 70 |
<field name="foo"/> |
| 71 |
<field name="selection" widget="label_selection" |
| 72 |
options="{'classes': {'normal': 'secondary', 'blocked': 'warning','done': 'success'}}"/> |
| 73 |
</list>`, |
| 74 |
}); |
| 75 |
|
| 76 |
expect(".o_field_widget .badge:not(:empty)").toHaveCount(3, { |
| 77 |
message: "should have three visible status labels", |
| 78 |
}); |
| 79 |
expect(".o_field_widget .badge.text-bg-warning").toHaveCount(1, { |
| 80 |
message: "should have one warning status label", |
| 81 |
}); |
| 82 |
expect(".o_field_widget .badge.text-bg-warning").toHaveText("Blocked", { |
| 83 |
message: "the warning label should read 'Blocked'", |
| 84 |
}); |
| 85 |
expect(".o_field_widget .badge.text-bg-secondary").toHaveCount(1, { |
| 86 |
message: "should have one default status label", |
| 87 |
}); |
| 88 |
expect(".o_field_widget .badge.text-bg-secondary").toHaveText("Normal", { |
| 89 |
message: "the default label should read 'Normal'", |
| 90 |
}); |
| 91 |
expect(".o_field_widget .badge.text-bg-success").toHaveCount(1, { |
| 92 |
message: "should have one success status label", |
| 93 |
}); |
| 94 |
expect(".o_field_widget .badge.text-bg-success").toHaveText("Done", { |
| 95 |
message: "the success label should read 'Done'", |
| 96 |
}); |
| 97 |
|
| 98 |
// switch to edit mode and check the result |
| 99 |
await click("tbody td:not(.o_list_record_selector)"); |
| 100 |
await animationFrame(); |
| 101 |
|
| 102 |
expect(".o_field_widget .badge:not(:empty)").toHaveCount(3, { |
| 103 |
message: "should have three visible status labels", |
| 104 |
}); |
| 105 |
expect(".o_field_widget .badge.text-bg-warning").toHaveCount(1, { |
| 106 |
message: "should have one warning status label", |
| 107 |
}); |
| 108 |
expect(".o_field_widget .badge.text-bg-warning").toHaveText("Blocked", { |
| 109 |
message: "the warning label should read 'Blocked'", |
| 110 |
}); |
| 111 |
expect(".o_field_widget .badge.text-bg-secondary").toHaveCount(1, { |
| 112 |
message: "should have one default status label", |
| 113 |
}); |
| 114 |
expect(".o_field_widget .badge.text-bg-secondary").toHaveText("Normal", { |
| 115 |
message: "the default label should read 'Normal'", |
| 116 |
}); |
| 117 |
expect(".o_field_widget .badge.text-bg-success").toHaveCount(1, { |
| 118 |
message: "should have one success status label", |
| 119 |
}); |
| 120 |
expect(".o_field_widget .badge.text-bg-success").toHaveText("Done", { |
| 121 |
message: "the success label should read 'Done'", |
| 122 |
}); |
| 123 |
|
| 124 |
// save and check the result |
| 125 |
await click(".o_control_panel_main_buttons .o_list_button_save"); |
| 126 |
await animationFrame(); |
| 127 |
expect(".o_field_widget .badge:not(:empty)").toHaveCount(3, { |
| 128 |
message: "should have three visible status labels", |
| 129 |
}); |
| 130 |
expect(".o_field_widget .badge.text-bg-warning").toHaveCount(1, { |
| 131 |
message: "should have one warning status label", |
| 132 |
}); |
| 133 |
expect(".o_field_widget .badge.text-bg-warning").toHaveText("Blocked", { |
| 134 |
message: "the warning label should read 'Blocked'", |
| 135 |
}); |
| 136 |
expect(".o_field_widget .badge.text-bg-secondary").toHaveCount(1, { |
| 137 |
message: "should have one default status label", |
| 138 |
}); |
| 139 |
expect(".o_field_widget .badge.text-bg-secondary").toHaveText("Normal", { |
| 140 |
message: "the default label should read 'Normal'", |
| 141 |
}); |
| 142 |
expect(".o_field_widget .badge.text-bg-success").toHaveCount(1, { |
| 143 |
message: "should have one success status label", |
| 144 |
}); |
| 145 |
expect(".o_field_widget .badge.text-bg-success").toHaveText("Done", { |
| 146 |
message: "the success label should read 'Done'", |
| 147 |
}); |
| 148 |
}); |