import { after, beforeEach, describe, expect, getFixture, test } from "@odoo/hoot"; import { queryAllProperties, queryAllTexts, queryOne, queryRect, resize } from "@odoo/hoot-dom"; import { animationFrame, runAllTimers } from "@odoo/hoot-mock"; import { Component, xml } from "@odoo/owl"; import { contains, defineModels, defineParams, fields, models, mountView, pagerNext, removeFacet, serverState, toggleMenuItem, toggleSearchBarMenu, webModels, } from "@web/../tests/web_test_helpers"; import { registry } from "@web/core/registry"; import { resetDateFieldWidths } from "@web/views/list/column_width_hook"; describe.current.tags("desktop"); const { ResCompany, ResPartner, ResUsers } = webModels; class Foo extends models.Model { foo = fields.Char(); bar = fields.Boolean(); date = fields.Date(); datetime = fields.Datetime(); int_field = fields.Integer(); qux = fields.Float(); m2o = fields.Many2one({ relation: "bar" }); o2m = fields.One2many({ relation: "bar" }); foo_o2m = fields.One2many({ relation: "foo" }); m2m = fields.Many2many({ relation: "bar" }); text = fields.Text(); amount = fields.Monetary({ currency_field: "currency_id" }); currency_id = fields.Many2one({ relation: "res.currency", default: 1 }); reference = fields.Reference({ selection: [ ["bar", "Bar"], ["res.currency", "Currency"], ], }); _records = [ { id: 1, foo: "yop", bar: true, date: "2017-01-25", datetime: "2016-12-12 10:55:05", int_field: 10, qux: 0.4, m2o: 1, m2m: [1, 2], amount: 1200, currency_id: 2, reference: "bar,1", }, { id: 2, foo: "blip", bar: true, int_field: 9, qux: 13, m2o: 2, m2m: [1, 2, 3], amount: 500, reference: "res.currency,1", }, { id: 3, foo: "gnap", bar: true, int_field: 17, qux: -3, m2o: 1, m2m: [], amount: 300, reference: "res.currency,2", }, { id: 4, foo: "blip", bar: false, int_field: -4, qux: 9, m2o: 1, m2m: [1], amount: 0, }, ]; } class Bar extends models.Model { name = fields.Char(); _records = [ { id: 1, name: "Value 1" }, { id: 2, name: "Value 2" }, { id: 3, name: "Value 3" }, ]; } class Currency extends models.Model { _name = "res.currency"; name = fields.Char(); symbol = fields.Char(); position = fields.Selection({ selection: [ ["after", "A"], ["before", "B"], ], }); inverse_rate = fields.Float(); _records = [ { id: 1, name: "USD", symbol: "$", position: "before", inverse_rate: 1 }, { id: 2, name: "EUR", symbol: "€", position: "after", inverse_rate: 0.5 }, ]; } defineModels([Foo, Bar, Currency, ResCompany, ResPartner, ResUsers]); beforeEach(() => { resize({ width: 800 }); document.body.style.fontFamily = "sans-serif"; }); function getColumnWidths() { return queryAllProperties(".o_list_table thead th", "offsetWidth"); } function expectedColumnWidthsToBeCloseTo(expectedColumnWidths) { getColumnWidths().forEach((width, index) => expect(width).toBeCloseTo(expectedColumnWidths[index], { margin: 3 }) ); } // width computation test(`width computation: no record, lot of fields`, async () => { Foo._records = []; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expectedColumnWidthsToBeCloseTo([40, 29, 89, 80, 89, 102, 99, 188, 114, 100]); }); test(`width computation: no record, few fields`, async () => { Foo._records = []; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 109, 559, 91]); }); test(`width computation: no record, all fields with a max width`, async () => { Foo._records = []; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 262, 233, 266]); }); test(`width computation: no record, sample data`, async () => { Foo._records = []; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 109, 186, 186, 186, 91]); }); test(`width computation: with records, lot of fields`, async () => { await mountView({ type: "list", resModel: "foo", arch: ` `, }); expectedColumnWidthsToBeCloseTo([40, 29, 89, 80, 89, 102, 99, 188, 114, 100]); }); test(`width computation: with records, lot of fields, grouped`, async () => { await mountView({ resModel: "foo", type: "list", arch: ` `, groupBy: ["int_field"], }); expect(`.o_resize`).toHaveCount(9); expectedColumnWidthsToBeCloseTo([40, 29, 89, 80, 89, 102, 99, 188, 114, 34, 32]); }); test(`width computation: with records, few fields`, async () => { await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 109, 559, 91]); }); test(`width computation: with records, no relative fields`, async () => { await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 199, 170, 192, 200]); }); test(`width computation: with records, very long text field`, async () => { Foo._records[0].text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum " + "dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, " + "sunt in culpa qui officia deserunt mollit anim id est laborum"; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 29, 618, 113]); }); test(`width computation: with records, lot of fields, long texts`, async () => { Foo._records[0].text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt"; Foo._records[1].foo = "Duis aute irure dolor in reprehenderit in voluptate velit esse cillumt"; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expectedColumnWidthsToBeCloseTo([40, 29, 89, 80, 102, 99, 89, 188, 114, 100]); }); test(`width computation: editable list, overflowing table`, async () => { class Abc extends models.Model { titi = fields.Char(); grosminet = fields.Char(); _records = [ { id: 1, titi: "Tiny text", grosminet: // Just want to make sure that the table is overflowed `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec est massa, gravida eget dapibus ac, eleifend eget libero. Suspendisse feugiat sed massa eleifend vestibulum. Sed tincidunt velit sed lacinia lacinia. Nunc in fermentum nunc. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nullam ut nisi a est ornare molestie non vulputate orci. Nunc pharetra porta semper. Mauris dictum eu nulla a pulvinar. Duis eleifend odio id ligula congue sollicitudin. Curabitur quis aliquet nunc, ut aliquet enim. Suspendisse malesuada felis non metus efficitur aliquet.`, }, ]; } defineModels([Abc]); await mountView({ resModel: "abc", type: "list", arch: ` `, }); expect(`table`).toHaveRect(queryRect`.o_list_renderer`, { message: "Table should not be stretched by its content", }); expect(getColumnWidths()).toEqual([40, 89, 671]); }); test(`width computation: with records, few fields, long texts`, async () => { Foo._records[0].text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt. Duis aute irure dolor in reprehenderit in voluptate velit esse cillumt"; Foo._records[1].foo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt. Duis aute irure dolor in reprehenderit in voluptate velit esse cillumt"; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 29, 354, 377]); }); test(`width computation: list with handle field`, async () => { await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 29, 731]); }); test(`width computation: editable list, no record, with handle field`, async () => { Foo._records = []; await mountView({ resModel: "foo", type: "list", arch: ` `, }); expect(`thead th`).toHaveCount(4, { message: "there should be 4 th" }); expect(`thead th:eq(0)`).toHaveClass("o_list_record_selector"); expect(`thead th:eq(1)`).toHaveClass("o_handle_cell"); expect(`thead th:eq(0)`).toHaveText("", { message: "the handle field shouldn't have a header description", }); expect(getColumnWidths()).toEqual([40, 29, 360, 371]); }); test(`width computation: widget with listViewWidth in its definition`, async () => { class MyWidget extends Component { static template = xml`My custom widget`; static props = ["*"]; } const myWidget = { listViewWidth: 171, component: MyWidget, }; registry.category("view_widgets").add("my_widget", myWidget); await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 180, 580]); }); test(`width computation: list with width attribute in arch`, async () => { await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(getColumnWidths()).toEqual([40, 61, 72, 102, 524]); }); test(`width computation: datetime in numeric, am/pm format`, async () => { defineParams({ lang_parameters: { date_format: "%m/%d/%Y", time_format: "%I:%M:%S %p", }, }); resetDateFieldWidths(); after(resetDateFieldWidths); await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(queryAllTexts(".o_data_row:eq(0) .o_data_cell")).toEqual([ "yop", "01/25/2017", "12/12/2016 11:55:05 AM", ]); expect(getColumnWidths()).toEqual([40, 494, 83, 182]); }); test(`width computation: width attribute in arch and overflowing table`, async () => { Foo._records[0].text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum " + "dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, " + "sunt in culpa qui officia deserunt mollit anim id est laborum"; await mountView({ resModel: "foo", type: "list", arch: ` `, }); expectedColumnWidthsToBeCloseTo([40, 188, 210, 362]); }); test(`width computation: no record, nameless and stringless buttons`, async () => { Foo._records = []; await mountView({ resModel: "foo", type: "list", arch: `