import { expect, test } from "@odoo/hoot"; import { click, edit, press, queryAllTexts, queryOne, scroll } from "@odoo/hoot-dom"; import { animationFrame, mockDate, mockTimeZone } from "@odoo/hoot-mock"; import { assertDateTimePicker, getPickerCell, zoomOut, } from "@web/../tests/core/datetime/datetime_test_helpers"; import { clickSave, contains, defineModels, defineParams, fieldInput, fields, models, mountView, onRpc, serverState, } from "@web/../tests/web_test_helpers"; class Partner extends models.Model { _name = "res.partner"; date = fields.Date(); char_field = fields.Char({ string: "Char" }); _records = [ { id: 1, date: "2017-02-03", char_field: "first char field", }, ]; _views = { form: /* xml */ `
`, }; } defineModels([Partner]); test("toggle datepicker", async () => { await mountView({ type: "form", resModel: "res.partner", resId: 1 }); expect(".o_datetime_picker").toHaveCount(0); await contains(".o_field_date input").click(); await animationFrame(); expect(".o_datetime_picker").toHaveCount(1); await fieldInput("char_field").click(); expect(".o_datetime_picker").toHaveCount(0); }); test.tags("desktop"); test("open datepicker on Control+Enter", async () => { defineParams({ lang_parameters: { date_format: "%d/%m/%Y", time_format: "%H:%M:%S", }, }); await mountView({ resModel: "res.partner", type: "form", arch: `
`, }); expect(".o_field_date input").toHaveCount(1); await press(["ctrl", "enter"]); await animationFrame(); expect(".o_datetime_picker").toHaveCount(1); //edit the input and open the datepicker again with ctrl+enter await contains(".o_field_date .o_input").click(); await edit("09/01/1997"); await press(["ctrl", "enter"]); await animationFrame(); assertDateTimePicker({ title: "January 1997", date: [ { cells: [ [0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, [9], 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 31, 0], ], daysOfWeek: ["#", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], weekNumbers: [1, 2, 3, 4, 5], }, ], }); }); test("toggle datepicker far in the future", async () => { Partner._records[0].date = "9999-12-31"; await mountView({ type: "form", resModel: "res.partner", resId: 1 }); expect(".o_datetime_picker").toHaveCount(0); await contains(".o_field_date input").click(); expect(".o_datetime_picker").toHaveCount(1); // focus another field await fieldInput("char_field").click(); expect(".o_datetime_picker").toHaveCount(0); }); test("date field is empty if no date is set", async () => { Partner._records[0].date = false; await mountView({ type: "form", resModel: "res.partner", resId: 1 }); expect(".o_field_date input").toHaveCount(1); expect(".o_field_date input").toHaveValue(""); }); test("set an invalid date when the field is already set", async () => { await mountView({ type: "form", resModel: "res.partner", resId: 1 }); expect(".o_field_widget[name='date'] input").toHaveValue("02/03/2017"); await fieldInput("date").edit("invalid date"); expect(".o_field_widget[name='date'] input").toHaveValue("02/03/2017", { message: "Should have been reset to the original value", }); }); test("set an invalid date when the field is not set yet", async () => { Partner._records[0].date = false; await mountView({ type: "form", resModel: "res.partner", resId: 1 }); expect(".o_field_widget[name='date'] input").toHaveValue(""); await fieldInput("date").edit("invalid date"); expect(".o_field_widget[name='date'] input").toHaveValue(""); }); test("value should not set on first click", async () => { Partner._records[0].date = false; await mountView({ type: "form", resModel: "res.partner", resId: 1 }); await contains(".o_field_date input").click(); expect(".o_field_widget[name='date'] input").toHaveValue(""); await contains(getPickerCell(22)).click(); await contains(".o_field_date input").click(); expect(".o_date_item_cell.o_selected").toHaveText("22"); }); test("date field in form view (with positive time zone offset)", async () => { mockTimeZone(2); // should be ignored by date fields await mountView({ type: "form", resModel: "res.partner", resId: 1 }); onRpc("web_save", ({ args }) => { expect.step(args[1].date); }); expect(".o_field_date input").toHaveValue("02/03/2017"); // open datepicker and select another value await contains(".o_field_date input").click(); expect(".o_datetime_picker").toHaveCount(1); expect(".o_date_item_cell.o_selected").toHaveCount(1); // select 22 Feb 2017 await zoomOut(); await zoomOut(); await contains(getPickerCell("2017")).click(); await contains(getPickerCell("Feb")).click(); await contains(getPickerCell("22")).click(); expect(".o_datetime_picker").toHaveCount(0); expect(".o_field_date input").toHaveValue("02/22/2017"); await clickSave(); expect.verifySteps(["2017-02-22"]); expect(".o_field_date input").toHaveValue("02/22/2017"); }); test("date field in form view (with negative time zone offset)", async () => { mockTimeZone(-2); // should be ignored by date fields await mountView({ type: "form", resModel: "res.partner", resId: 1 }); expect(".o_field_date input").toHaveValue("02/03/2017"); }); test("date field dropdown doesn't dissapear on scroll", async () => { await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: `
`, }); await contains(".o_field_date input").click(); expect(".o_datetime_picker").toHaveCount(1); await scroll(".scrollable", { top: 50 }); expect(".scrollable").toHaveProperty("scrollTop", 50); expect(".o_datetime_picker").toHaveCount(1); }); test("date field with label opens datepicker on click", async () => { await mountView({ type: "form", resModel: "res.partner", resId: 1, arch: `