From 00e3cb74c44565b2106dba6befac2b6f8512eede Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Mon, 19 Jul 2021 09:11:08 +0000 Subject: [PATCH] [REF] web: migrate from moment to luxon Migrate the code of parse and format dates of mock server from moment to luxon. Co-authored-by: Samuel Degueldre --- .../web/static/tests/helpers/mock_server.js | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/addons/web/static/tests/helpers/mock_server.js b/addons/web/static/tests/helpers/mock_server.js index 5148ccb94a7..d247637e682 100644 --- a/addons/web/static/tests/helpers/mock_server.js +++ b/addons/web/static/tests/helpers/mock_server.js @@ -7,6 +7,7 @@ import { registry } from "@web/core/registry"; import * as utils from "@web/core/utils/arrays"; import { makeFakeRPCService, makeMockFetch } from "./mock_services"; import { patchWithCleanup } from "./utils"; +import { parseDate } from "@web/core/l10n/dates"; const { DateTime } = luxon; const serviceRegistry = registry.category("services"); @@ -242,7 +243,10 @@ export class MockServer { // TODO // const pyevalContext = window.py.dict.fromJSON(context || {}); // var v = pyUtils.py_eval(mod, {context: pyevalContext}) ? true : false; - console.info("MockServer: naive parse of modifier value in", QUnit.config.current.testName); + console.info( + "MockServer: naive parse of modifier value in", + QUnit.config.current.testName + ); const v = JSON.parse(mod); if (inTreeView && !inListHeader && attr === "invisible") { modifiers.column_invisible = v; @@ -509,7 +513,9 @@ export class MockServer { if (!action) { // when the action doesn't exist, the real server doesn't crash, it // simply returns false - console.warn(`No action found for ID ${kwargs.action_id} during test ${QUnit.config.current.testName}`); + console.warn( + `No action found for ID ${kwargs.action_id} during test ${QUnit.config.current.testName}` + ); } return action || false; } @@ -769,7 +775,7 @@ export class MockServer { if (aggregateFunction === "day") { return date.toFormat("yyyy-MM-dd"); } else if (aggregateFunction === "week") { - return `W${date.toFormat("W yyyy")}`; + return `W${date.toFormat("WW yyyy")}`; } else if (aggregateFunction === "quarter") { return `Q${date.toFormat("q yyyy")}`; } else if (aggregateFunction === "year") { @@ -819,24 +825,26 @@ export class MockServer { res[groupByField] = val; } if (field.type === "date" && val) { - console.info("Mock Server: read group not fully implemented (moment stuff) in", QUnit.config.current.testName); - // const aggregateFunction = groupByField.split(':')[1]; - // let startDate; - // let endDate; - // if (aggregateFunction === 'day') { - // startDate = moment(val, 'YYYY-MM-DD'); - // endDate = startDate.clone().add(1, 'days'); - // } else if (aggregateFunction === 'week') { - // startDate = moment(val, 'ww YYYY'); - // endDate = startDate.clone().add(1, 'weeks'); - // } else if (aggregateFunction === 'year') { - // startDate = moment(val, 'Y'); - // endDate = startDate.clone().add(1, 'years'); - // } else { - // startDate = moment(val, 'MMMM YYYY'); - // endDate = startDate.clone().add(1, 'months'); - // } - // res.__domain = [[fieldName, '>=', startDate.format('YYYY-MM-DD')], [fieldName, '<', endDate.format('YYYY-MM-DD')]].concat(res.__domain); + const aggregateFunction = groupByField.split(":")[1]; + let startDate; + let endDate; + if (aggregateFunction === "day") { + startDate = parseDate(val, { format: "yyyy-MM-dd" }); + endDate = startDate.plus({ days: 1 }); + } else if (aggregateFunction === "week") { + startDate = parseDate(val, { format: "WW kkkk" }); + endDate = startDate.plus({ weeks: 1 }); + } else if (aggregateFunction === "year") { + startDate = parseDate(val, { format: "y" }); + endDate = startDate.plus({ years: 1 }); + } else { + startDate = parseDate(val, { format: "MMMM yyyy" }); + endDate = startDate.plus({ months: 1 }); + } + res.__domain = [ + [fieldName, ">=", startDate.toFormat("yyyy-MM-dd")], + [fieldName, "<", endDate.toFormat("yyyy-MM-dd")], + ].concat(res.__domain); } else { res.__domain = Domain.combine( [[[fieldName, "=", val]], res.__domain],