| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { queryText } from "@odoo/hoot-dom"; |
| 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 |
float_field = fields.Float({ string: "Float field" }); |
| 15 |
|
| 16 |
_records = [{ id: 1, float_field: 0.44444 }]; |
| 17 |
} |
| 18 |
|
| 19 |
class User extends models.Model { |
| 20 |
_name = "res.users"; |
| 21 |
has_group() { |
| 22 |
return true; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
defineModels([Partner, User]); |
| 27 |
|
| 28 |
test("basic flow in form view", async () => { |
| 29 |
onRpc("partner", "web_save", ({ args }) => { |
| 30 |
// 1.000 / 0.125 = 8 |
| 31 |
expect.step(args[1].float_field.toString()); |
| 32 |
}); |
| 33 |
await mountView({ |
| 34 |
type: "form", |
| 35 |
resModel: "partner", |
| 36 |
resId: 1, |
| 37 |
arch: ` |
| 38 |
<form> |
| 39 |
<field name="float_field" widget="float_toggle" options="{'factor': 0.125, 'range': [0, 1, 0.75, 0.5, 0.25]}" digits="[5,3]"/> |
| 40 |
</form>`, |
| 41 |
}); |
| 42 |
|
| 43 |
expect(".o_field_widget").toHaveText("0.056", { |
| 44 |
message: "The formatted time value should be displayed properly.", |
| 45 |
}); |
| 46 |
expect("button.o_field_float_toggle").toHaveText("0.056", { |
| 47 |
message: "The value should be rendered correctly on the button.", |
| 48 |
}); |
| 49 |
|
| 50 |
await contains("button.o_field_float_toggle").click(); |
| 51 |
|
| 52 |
expect("button.o_field_float_toggle").toHaveText("0.000", { |
| 53 |
message: "The value should be rendered correctly on the button.", |
| 54 |
}); |
| 55 |
|
| 56 |
// note, 0 will not be written, it's kept in the _changes of the datapoint. |
| 57 |
// because save has not been clicked. |
| 58 |
|
| 59 |
await contains("button.o_field_float_toggle").click(); |
| 60 |
|
| 61 |
expect("button.o_field_float_toggle").toHaveText("1.000", { |
| 62 |
message: "The value should be rendered correctly on the button.", |
| 63 |
}); |
| 64 |
|
| 65 |
await clickSave(); |
| 66 |
|
| 67 |
expect(".o_field_widget").toHaveText("1.000", { |
| 68 |
message: "The new value should be saved and displayed properly.", |
| 69 |
}); |
| 70 |
|
| 71 |
expect.verifySteps(["8"]); |
| 72 |
}); |
| 73 |
|
| 74 |
test("kanban view (readonly) with option force_button", async () => { |
| 75 |
await mountView({ |
| 76 |
type: "kanban", |
| 77 |
resModel: "partner", |
| 78 |
arch: ` |
| 79 |
<kanban> |
| 80 |
<templates> |
| 81 |
<t t-name="card"> |
| 82 |
<field name="float_field" widget="float_toggle" options="{'force_button': true}"/> |
| 83 |
</t> |
| 84 |
</templates> |
| 85 |
</kanban>`, |
| 86 |
}); |
| 87 |
|
| 88 |
expect("button.o_field_float_toggle").toHaveCount(1, { |
| 89 |
message: "should have rendered toggle button", |
| 90 |
}); |
| 91 |
|
| 92 |
const value = queryText("button.o_field_float_toggle"); |
| 93 |
await contains("button.o_field_float_toggle").click(); |
| 94 |
expect("button.o_field_float_toggle").not.toHaveText(value, { |
| 95 |
message: "float_field field value should be changed", |
| 96 |
}); |
| 97 |
}); |