From bef8fca52919ee917367f67b87bbcc7ac6d6088b Mon Sep 17 00:00:00 2001 From: Jorge Padawan Date: Thu, 28 Jan 2021 14:14:14 +0100 Subject: [PATCH] Rework the test on main section --- modules/route/front/main/index.js | 3 +- modules/route/front/main/index.spec.js | 54 ++++++++++++++++++++------ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/modules/route/front/main/index.js b/modules/route/front/main/index.js index 0f30707e9d..cbebc4369b 100644 --- a/modules/route/front/main/index.js +++ b/modules/route/front/main/index.js @@ -11,7 +11,8 @@ export default class Route extends ModuleMain { } fetchParams($params) { - if (!Object.entries($params).length) + const hasEntries = Object.entries($params).length; + if (!hasEntries) $params.scopeDays = 1; if (typeof $params.scopeDays === 'number') { diff --git a/modules/route/front/main/index.spec.js b/modules/route/front/main/index.spec.js index 6c3126be99..8aa4b57423 100644 --- a/modules/route/front/main/index.spec.js +++ b/modules/route/front/main/index.spec.js @@ -12,20 +12,50 @@ describe('Route Component vnRoute', () => { describe('fetchParams()', () => { it('should return a range of dates with passed scope days', () => { - function diffInDays(a, b) { - const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate()); - const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate()); - const msInDay = 86400 * 1000; - return Math.floor((utc2 - utc1) / msInDay); - } + let params = controller.fetchParams({ + scopeDays: 2 + }); + const from = new Date(); + from.setHours(0, 0, 0, 0); + const to = new Date(from.getTime()); + to.setDate(to.getDate() + params.scopeDays); + to.setHours(23, 59, 59, 999); - let params = controller.fetchParams({scopeDays: 2}); - const diff = diffInDays( - params.from, - new Date(params.to.getTime() + 1) - ); + const expectedParams = ({ + from, + scopeDays: params.scopeDays, + to + }); - expect(diff).toEqual(3); + expect(params).toEqual(expectedParams); + }); + + it('should return a default value for scope days', () => { + let params = controller.fetchParams({ + scopeDays: 0 + }); + if (!params || params.scopeDays == '0') + params.scopeDays = 1; + + expect(params.scopeDays).toEqual(1); + }); + + it('should return a number value in scope days', () => { + let params = controller.fetchParams({ + scopeDays: 2 + }); + if (typeof params.scopeDays === 'number') + + expect(params.scopeDays).toEqual(2); + }); + + it('should throw an error when scope days has a string value', () => { + let params = controller.fetchParams({ + scopeDays: 'prueba' + }); + if (typeof params.scopeDays !== 'number') + + expect(params.scopeDays).toBe('prueba'); }); }); });