From bda7913d480f83e101cab300c27c55c5e508eb10 Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Thu, 29 Sep 2022 17:06:42 +0200 Subject: [PATCH 1/6] [MOV] hr_timesheet: move legacy tests into a separated folder task-3001470 X-original-commit: e1a9678b8dadb78fa4fecc72edf9a0d462b1b101 --- .../static/tests/{ => legacy}/timesheet_uom_common.js | 0 .../hr_timesheet/static/tests/{ => legacy}/timesheet_uom_tests.js | 0 .../static/tests/{ => legacy}/timesheet_uom_widgets_tests.js | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename addons/hr_timesheet/static/tests/{ => legacy}/timesheet_uom_common.js (100%) rename addons/hr_timesheet/static/tests/{ => legacy}/timesheet_uom_tests.js (100%) rename addons/hr_timesheet/static/tests/{ => legacy}/timesheet_uom_widgets_tests.js (100%) diff --git a/addons/hr_timesheet/static/tests/timesheet_uom_common.js b/addons/hr_timesheet/static/tests/legacy/timesheet_uom_common.js similarity index 100% rename from addons/hr_timesheet/static/tests/timesheet_uom_common.js rename to addons/hr_timesheet/static/tests/legacy/timesheet_uom_common.js diff --git a/addons/hr_timesheet/static/tests/timesheet_uom_tests.js b/addons/hr_timesheet/static/tests/legacy/timesheet_uom_tests.js similarity index 100% rename from addons/hr_timesheet/static/tests/timesheet_uom_tests.js rename to addons/hr_timesheet/static/tests/legacy/timesheet_uom_tests.js diff --git a/addons/hr_timesheet/static/tests/timesheet_uom_widgets_tests.js b/addons/hr_timesheet/static/tests/legacy/timesheet_uom_widgets_tests.js similarity index 100% rename from addons/hr_timesheet/static/tests/timesheet_uom_widgets_tests.js rename to addons/hr_timesheet/static/tests/legacy/timesheet_uom_widgets_tests.js From 9b483a6d8954342a5f59fc64ef3ed4f56293f3c6 Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Thu, 29 Sep 2022 17:17:49 +0200 Subject: [PATCH 2/6] [REF] hr_timesheet: rename tests task-3001470 X-original-commit: d7881b85a66558ac31123e8ad30795342bbbf64a --- addons/hr_timesheet/static/tests/timesheet_graph_tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/hr_timesheet/static/tests/timesheet_graph_tests.js b/addons/hr_timesheet/static/tests/timesheet_graph_tests.js index 972294c7715..91092343897 100644 --- a/addons/hr_timesheet/static/tests/timesheet_graph_tests.js +++ b/addons/hr_timesheet/static/tests/timesheet_graph_tests.js @@ -11,7 +11,7 @@ import { setupControlPanelServiceRegistry } from "@web/../tests/search/helpers"; const serviceRegistry = registry.category("services"); -QUnit.module('Views', function (hooks) { +QUnit.module('hr_timesheet', function (hooks) { let serverData; hooks.beforeEach(() => { serverData = { @@ -40,7 +40,7 @@ QUnit.module('Views', function (hooks) { serviceRegistry.add("dialog", dialogService); }); - QUnit.module("hrTimesheetGraphView"); + QUnit.module("hr_timesheet_graphview"); QUnit.test('the timesheet graph view data are not multiplied by a factor that is company related (factor = 1)', async function (assert) { assert.expect(1); From d0987e0b254be303f87a4c56db30ac93eab97a9e Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Tue, 4 Oct 2022 08:23:38 +0200 Subject: [PATCH 3/6] [REF] web: allow overriding Many2OneField activeActions Prior to this commit it was not possible to override `computeActiveActions`, which prevents acting when onWillUpdateProps is triggered as both are private to the setup function. task-3001470 X-original-commit: c785327fedd4fa94884af692c21985663a79c454 --- .../views/fields/many2one/many2one_field.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/addons/web/static/src/views/fields/many2one/many2one_field.js b/addons/web/static/src/views/fields/many2one/many2one_field.js index 3cb54051d29..57406b8a44d 100644 --- a/addons/web/static/src/views/fields/many2one/many2one_field.js +++ b/addons/web/static/src/views/fields/many2one/many2one_field.js @@ -51,18 +51,10 @@ export class Many2OneField extends Component { this.autocompleteContainerRef.el.querySelector("input").focus(); }; - const computeActiveActions = (props) => { - this.state.activeActions = { - create: props.canCreate, - createEdit: props.canCreateEdit, - write: props.canWrite, - }; - }; - this.state = useState({ isFloating: !this.props.value, }); - computeActiveActions(this.props); + this.computeActiveActions(this.props); this.openMany2X = useOpenMany2XRecord({ resModel: this.relation, @@ -102,7 +94,7 @@ export class Many2OneField extends Component { onWillUpdateProps(async (nextProps) => { this.state.isFloating = !nextProps.value; - computeActiveActions(nextProps); + this.computeActiveActions(nextProps); }); } @@ -151,6 +143,13 @@ export class Many2OneField extends Component { autocomplete_container: this.autocompleteContainerRef, }; } + computeActiveActions(props) { + this.state.activeActions = { + create: props.canCreate, + createEdit: props.canCreateEdit, + write: props.canWrite, + }; + } getDomain() { return this.domain.toList(this.context); } From 63f95986a72ad1e578b9223faa5f3d7edb0e047d Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Tue, 4 Oct 2022 08:25:14 +0200 Subject: [PATCH 4/6] [FIX] hr_timesheet: ensure task_with_hours widget works as expected Prior to this commit, altought the widget was working from the UI, tests have detected that `activeActions` from the state was not updated when props where updated. task-3001470 X-original-commit: 0fb1ff059c40b2489e315fe630a3b39625c468ec --- .../task_with_hours/task_with_hours.js | 18 +-- .../static/tests/hr_timesheet_common_tests.js | 130 ++++++++++++++++++ .../static/tests/task_with_hours_tests.js | 67 +++++++++ 3 files changed, 207 insertions(+), 8 deletions(-) create mode 100644 addons/hr_timesheet/static/tests/hr_timesheet_common_tests.js create mode 100644 addons/hr_timesheet/static/tests/task_with_hours_tests.js diff --git a/addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js b/addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js index 22e1464d48b..ba82247cede 100644 --- a/addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js +++ b/addons/hr_timesheet/static/src/components/task_with_hours/task_with_hours.js @@ -6,14 +6,6 @@ import { Many2OneField } from "@web/views/fields/many2one/many2one_field"; class TaskWithHours extends Many2OneField { - setup() { - super.setup(); - const activeActions = this.state.activeActions; - activeActions.canCreate = activeActions.canCreate && this.canCreate; - activeActions.canQuickCreate = activeActions.canQuickCreate || this.canCreate; - activeActions.canCreateEdit = activeActions.canCreate; - } - get canCreate() { return Boolean(this.context.default_project_id); } @@ -44,6 +36,16 @@ class TaskWithHours extends Many2OneField { return props; } + /** + * @override + */ + computeActiveActions(props) { + super.computeActiveActions(props); + const activeActions = this.state.activeActions; + activeActions.create = activeActions.create && this.canCreate; + activeActions.createEdit = activeActions.create; + } + } registry.category("fields").add("task_with_hours", TaskWithHours); diff --git a/addons/hr_timesheet/static/tests/hr_timesheet_common_tests.js b/addons/hr_timesheet/static/tests/hr_timesheet_common_tests.js new file mode 100644 index 00000000000..2973e580364 --- /dev/null +++ b/addons/hr_timesheet/static/tests/hr_timesheet_common_tests.js @@ -0,0 +1,130 @@ +/** @odoo-module */ + +import { registry } from "@web/core/registry"; +import { session } from "@web/session"; +import { companyService } from "@web/webclient/company_service"; + +import { patchWithCleanup } from "@web/../tests/helpers/utils"; +import { setupViewRegistries } from "@web/../tests/views/helpers"; + + +export const getServerData = () => JSON.parse(JSON.stringify({ + models: { + 'account.analytic.line': { + fields: { + project_id: { string: "Project", type: "many2one", relation: "project.project" }, + task_id: { string: "Task", type: "many2one", relation: "project.task" }, + unit_amount: { string: "Unit Amount", type: "integer" }, + }, + records: [ + { id: 1, project_id: 1, task_id: 3, unit_amount: 1 }, + { id: 2, project_id: 1, task_id: false, unit_amount: 1 }, + { id: 3, project_id: false, task_id: false, unit_amount: 1 }, + ], + }, + 'project.project': { + fields: { + name: { string: "Name", type: "string" }, + }, + records: [ + { id: 1, name: "Project 1" }, + ], + }, + 'project.task': { + fields: { + name: { string: "Name", type: "string" }, + project_id: { string: "Project", type: "many2one", relation: "project.project" }, + }, + records: [ + { id: 1, name: "Task 1\u00A0AdditionalInfo", project_id: 1 }, + { id: 2, name: "Task 2\u00A0AdditionalInfo", project_id: 1 }, + { id: 3, name: "Task 3\u00A0AdditionalInfo", project_id: 1 }, + ], + }, + }, + views: { + "account.analytic.line,false,form": ` +
+ + + + + `, + "account.analytic.line,false,list": ` + + + + + + `, + }, +})); + +export function updateArch(serverData, fieldNameWidgetNameMapping = {}, fieldNameContextMapping = {}) { + for (const viewKey in serverData.views) { + for (const [fieldName, widgetName] of Object.entries(fieldNameWidgetNameMapping)) { + serverData.views[viewKey] = serverData.views[viewKey].replace( + `name="${fieldName}"`, + `name="${fieldName}" widget="${widgetName}"` + ); + } + for (const [fieldName, context] of Object.entries(fieldNameContextMapping)) { + serverData.views[viewKey] = serverData.views[viewKey].replace( + `name="${fieldName}"`, + `name="${fieldName}" context="${context}"` + ); + } + } +} + +export function addFieldsInArch(serverData, fields, beforeField) { + let fieldsArch = ""; + for (const field of fields) { + fieldsArch += ` + `; + } + for (const viewKey in serverData.views) { + serverData.views[viewKey] = serverData.views[viewKey].replace( + ` { + let target; + let serverData; + hooks.beforeEach(async function (assert) { + setupTestEnv(); + serverData = getServerData(); + updateArch( + serverData, + { task_id: "task_with_hours" }, + { task_id: "{ 'default_project_id': project_id }" }); + target = getFixture(); + }); + + QUnit.module("task_with_hours"); + + async function _testCreateAndEdit(target, visible, assert) { + await click(target, ".o_list_many2one[name=task_id]"); + await click(target, ".o_list_many2one[name=task_id] input"); + await editInput(target, ".o_list_many2one[name=task_id] input", "NonExistingTask"); + await click(target, ".o_list_many2one[name=task_id] input"); + await clickDropdown(target, "task_id"); + const testFunction = visible ? assert.containsOnce : assert.containsNone; + testFunction(target, '.o_list_many2one[name=task_id] .dropdown ul li:contains("Create and edit...")'); + } + + QUnit.test("quick create is enabled when project_id is set", async function (assert) { + await makeView({ + serverData, + type: "list", + resModel: "account.analytic.line", + }); + const secondRow = target.querySelector(".o_list_table .o_data_row:nth-of-type(2)"); + await _testCreateAndEdit(secondRow, true, assert); + }); + + QUnit.test("quick create is no enabled when project_id is not set", async function (assert) { + await makeView({ + serverData, + type: "list", + resModel: "account.analytic.line", + }); + const thirdRow = target.querySelector(".o_list_table .o_data_row:nth-of-type(3)"); + await _testCreateAndEdit(thirdRow, false, assert); + }); + + QUnit.test("the text of the task includes hours in the drop down but not in the line", async function (assert) { + await makeView({ + serverData, + type: "list", + resModel: "account.analytic.line", + }); + const firstRow = target.querySelector(".o_list_table .o_data_row:first-of-type"); + assert.containsNone(firstRow, '.o_list_many2one[name=task_id]:contains("AdditionalInfo")'); + await click(firstRow, ".o_list_many2one[name=task_id]"); + await clickDropdown(firstRow, "task_id"); + assert.containsN(firstRow, '.o_list_many2one[name=task_id] .dropdown ul li:contains("AdditionalInfo")', 3); + }); + +}); From b085183eba6c6384a9ce4fea711a79d7c847bef0 Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Tue, 4 Oct 2022 13:00:50 +0200 Subject: [PATCH 5/6] [IMP] hr_timesheet: add tests for timesheet_uom widget task-3001470 X-original-commit: 5698eaa78f8db4e0c016674f46dc4747a03e259b --- .../static/tests/timesheet_uom_tests.js | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 addons/hr_timesheet/static/tests/timesheet_uom_tests.js diff --git a/addons/hr_timesheet/static/tests/timesheet_uom_tests.js b/addons/hr_timesheet/static/tests/timesheet_uom_tests.js new file mode 100644 index 00000000000..0e3f37fad7d --- /dev/null +++ b/addons/hr_timesheet/static/tests/timesheet_uom_tests.js @@ -0,0 +1,94 @@ +/** @odoo-module */ + +import { session } from "@web/session"; + +import { makeView } from "@web/../tests/views/helpers"; +import { getFixture, patchWithCleanup } from "@web/../tests/helpers/utils"; + +import { getServerData, updateArch, setupTestEnv } from "./hr_timesheet_common_tests"; + + +QUnit.module("hr_timesheet", (hooks) => { + let target; + let serverData; + hooks.beforeEach(async function (assert) { + setupTestEnv(); + serverData = getServerData(); + updateArch(serverData, { unit_amount: "timesheet_uom" }); + target = getFixture(); + }); + + QUnit.module("timesheet_uom"); + + QUnit.test("FloatTimeField is used when current company uom uses float_time widget", async function (assert) { + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.equal(unitAmountInput.value, "01:00", "unit_amount is displayed as time"); + }); + + QUnit.test("FloatTimeField is not dependent of timesheet_uom_factor of the current company when current company uom uses float_time widget", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_factor: 2 }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.equal(unitAmountInput.value, "01:00", "timesheet_uom_factor is not taken into account"); + }); + + QUnit.test("FloatToggleField is used when current company uom uses float_toggle widget", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_id: 2 }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + assert.containsOnce(target, 'div[name="unit_amount"] .o_field_float_toggle', "unit_amount is displayed as float toggle"); + }); + + QUnit.test("FloatToggleField is dependent of timesheet_uom_factor of the current company when current company uom uses float_toggle widget", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_id: 2, timesheet_uom_factor: 2 }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + assert.containsOnce(target, 'div[name="unit_amount"] .o_field_float_toggle:contains("2.00")', "timesheet_uom_factor is taken into account"); + }); + + QUnit.test("FloatFactorField is used when the current_company uom is not part of the session uom", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_id: 'dummy' }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.containsOnce(target, 'div[name="unit_amount"] input[inputmode="numeric"]', "unit_amount is displayed as float"); + assert.equal(unitAmountInput.value, "1.00", "unit_amount is not displayed as float and not as time"); + assert.containsNone(target, 'div[name="unit_amount"].o_field_float_toggle', "unit_amount is not displayed as float toggle"); + }); + + QUnit.test("FloatFactorField is dependent of timesheet_uom_factor of the current company when current company uom uses float_toggle widget", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_id: 'dummy', timesheet_uom_factor: 2 }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.equal(unitAmountInput.value, "2.00", "timesheet_uom_factor is taken into account"); + }); + +}); From 56623a9712cb95fcee25ce2260c5a57dade526ae Mon Sep 17 00:00:00 2001 From: "Laurent Stukkens (LTU)" Date: Tue, 4 Oct 2022 13:09:01 +0200 Subject: [PATCH 6/6] [IMP] hr_timesheet: add tests for timesheet_uom_no_toggle widget task-3001470 X-original-commit: 4004a760a577247ae9508b8cecfc4502faf98a58 --- .../tests/timesheet_uom_no_toggle_tests.js | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 addons/hr_timesheet/static/tests/timesheet_uom_no_toggle_tests.js diff --git a/addons/hr_timesheet/static/tests/timesheet_uom_no_toggle_tests.js b/addons/hr_timesheet/static/tests/timesheet_uom_no_toggle_tests.js new file mode 100644 index 00000000000..98d0b20d8d6 --- /dev/null +++ b/addons/hr_timesheet/static/tests/timesheet_uom_no_toggle_tests.js @@ -0,0 +1,86 @@ +/** @odoo-module */ + +import { session } from "@web/session"; + +import { makeView } from "@web/../tests/views/helpers"; +import { getFixture, patchWithCleanup } from "@web/../tests/helpers/utils"; + +import { getServerData, updateArch, setupTestEnv } from "./hr_timesheet_common_tests"; + + +QUnit.module("hr_timesheet", (hooks) => { + let target; + let serverData; + hooks.beforeEach(async function (assert) { + setupTestEnv(); + serverData = getServerData(); + updateArch(serverData, { unit_amount: "timesheet_uom_no_toggle" }); + target = getFixture(); + }); + + QUnit.module("timesheet_uom_no_toggle"); + + QUnit.test("FloatTimeField is used when current company uom uses float_time widget", async function (assert) { + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.equal(unitAmountInput.value, "01:00", "unit_amount is displayed as time"); + }); + + QUnit.test("FloatTimeField is not dependent of timesheet_uom_factor of the current company when current company uom uses float_time widget", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_factor: 2 }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.equal(unitAmountInput.value, "01:00", "timesheet_uom_factor is not taken into account"); + }); + + QUnit.test("FloatToggleField is not used when current company uom uses float_toggle widget, FloatFactorField is used instead", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_id: 2 }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + assert.containsNone(target, 'div[name="unit_amount"] .o_field_float_toggle', "unit_amount is not displayed as float toggle"); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.containsOnce(target, 'div[name="unit_amount"] input[inputmode="numeric"]', "unit_amount is displayed as float"); + assert.equal(unitAmountInput.value, "1.00", "unit_amount is not displayed as float and not as time"); + }); + + QUnit.test("FloatFactorField is used when the current_company uom is not part of the session uom", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_id: 'dummy' }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.containsOnce(target, 'div[name="unit_amount"] input[inputmode="numeric"]', "unit_amount is displayed as float"); + assert.equal(unitAmountInput.value, "1.00", "unit_amount is not displayed as float and not as time"); + assert.containsNone(target, 'div[name="unit_amount"].o_field_float_toggle', "unit_amount is not displayed as float toggle"); + }); + + QUnit.test("FloatFactorField is dependent of timesheet_uom_factor of the current company when current company uom uses float_toggle widget", async function (assert) { + patchWithCleanup(session.user_companies.allowed_companies[1], { timesheet_uom_id: 'dummy', timesheet_uom_factor: 2 }); + await makeView({ + serverData, + type: "form", + resModel: "account.analytic.line", + resId: 1, + }); + const unitAmountInput = target.querySelector('div[name="unit_amount"] input'); + assert.equal(unitAmountInput.value, "2.00", "timesheet_uom_factor is taken into account"); + }); + +});