salix/modules/worker/back/methods/worker-calendar/specs/absences.spec.js

160 lines
5.1 KiB
JavaScript

const app = require('vn-loopback/server/server');
describe('Worker absences()', () => {
afterAll(async done => {
const hiredWorker = await app.models.WorkerLabour.findById(106);
const endedDate = new Date();
endedDate.setFullYear(endedDate.getFullYear() + 1);
endedDate.setHours(0, 0, 0, 0);
endedDate.setMonth(0);
endedDate.setDate(1);
await hiredWorker.updateAttributes({ended: endedDate});
done();
});
it('should get the absence calendar for the given dates then evaluate the type of absences', async() => {
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
let absences = result[1];
expect(calendar.totalHolidays).toEqual(27.5);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
expect(firstType).toEqual('Leave of absence');
expect(sixthType).toEqual('Holidays');
});
it(`should fire the worker 106 on Juny and see he/she has 14`, async() => {
let workerFk = 106;
const firedWorker = await app.models.WorkerLabour.findById(workerFk);
const endedDate = new Date();
endedDate.setDate(30);
endedDate.setMonth(5);
endedDate.setHours(0, 0, 0, 0);
await firedWorker.updateAttributes({ended: endedDate});
let ctx = {req: {accessToken: {userId: 9}}};
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
let absences = result[1];
expect(calendar.totalHolidays).toEqual(13.5);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
expect(firstType).toEqual('Leave of absence');
expect(sixthType).toEqual('Holidays');
});
it(`should fire the worker 106 on March and see he/she has 7`, async() => {
const firedWorker = await app.models.WorkerLabour.findById(106);
const endedDate = new Date();
endedDate.setHours(0, 0, 0, 0);
endedDate.setMonth(2);
endedDate.setDate(31);
await firedWorker.updateAttributes({ended: endedDate});
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
let absences = result[1];
expect(calendar.totalHolidays).toEqual(7);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
expect(firstType).toEqual('Leave of absence');
expect(sixthType).toEqual('Holidays');
});
it(`should fire the worker 106 on january and see he/she has x`, async() => {
const firedWorker = await app.models.WorkerLabour.findById(106);
const endedDate = new Date();
endedDate.setHours(0, 0, 0, 0);
endedDate.setMonth(0);
endedDate.setDate(28);
await firedWorker.updateAttributes({ended: endedDate});
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
let absences = result[1];
expect(calendar.totalHolidays).toEqual(2);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
expect(firstType).toEqual('Leave of absence');
expect(sixthType).toEqual('Holidays');
});
});