Rework the test on main section

This commit is contained in:
Jorge Padawan 2021-01-28 14:14:14 +01:00
parent e205ebba72
commit bef8fca529
2 changed files with 44 additions and 13 deletions

View File

@ -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') {

View File

@ -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');
});
});
});