#1889 absences() unit test fixed + refactor
gitea/salix/dev This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2019-11-20 14:22:44 +01:00
parent 37618efb58
commit 6929e3d497
2 changed files with 96 additions and 96 deletions

View File

@ -134,11 +134,11 @@ module.exports = Self => {
});
if (currentContract) {
const maxDays = currentContract.holidays().days;
calendar.totalHolidays = maxDays;
const maxHolidays = currentContract.holidays().days;
calendar.totalHolidays = maxHolidays;
if (workedDays < 365)
calendar.totalHolidays = Math.round(2 * maxDays * (workedDays + 1) / 365) / 2;
calendar.totalHolidays = Math.round(2 * maxHolidays * (workedDays) / 365) / 2;
}
return [calendar, absences, holidays];

View File

@ -1,20 +1,7 @@
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() => {
it('should get the absence calendar for a full year contract', async() => {
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
@ -43,16 +30,11 @@ describe('Worker absences()', () => {
expect(sixthType).toEqual('Holidays');
});
it(`should see he/she has 26.5`, async() => {
it('should get the absence calendar for a permanent contract', async() => {
let workerFk = 106;
const firedWorker = await app.models.WorkerLabour.findById(workerFk);
const endedDate = new Date();
endedDate.setDate(endedDate.getDate() + 1);
endedDate.setMonth(endedDate.getMonth() + 1);
endedDate.setHours(0, 0, 0, 0);
await firedWorker.updateAttributes({ended: endedDate});
let worker = await app.models.WorkerLabour.findById(workerFk);
let endedDate = worker.ended;
await worker.updateAttributes({ended: null});
let ctx = {req: {accessToken: {userId: 9}}};
@ -71,76 +53,6 @@ describe('Worker absences()', () => {
let calendar = result[0];
let absences = result[1];
expect(calendar.totalHolidays).toEqual(26.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 see he/she has 26.5`, async() => {
const firedWorker = await app.models.WorkerLabour.findById(106);
const endedDate = new Date();
endedDate.setDate(endedDate.getDate() + 1);
endedDate.setMonth(endedDate.getMonth() + 1);
endedDate.setHours(0, 0, 0, 0);
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(26.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 see he/she has 27.5`, async() => {
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
const firedWorker = await app.models.WorkerLabour.findById(106);
await firedWorker.updateAttributes({ended: null});
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);
@ -149,5 +61,93 @@ describe('Worker absences()', () => {
expect(firstType).toEqual('Leave of absence');
expect(sixthType).toEqual('Holidays');
// restores the contract end date
await worker.updateAttributes({ended: endedDate});
});
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
const today = new Date();
// getting how many days in a year
const yearStart = new Date();
yearStart.setHours(0, 0, 0, 0);
yearStart.setMonth(0);
yearStart.setDate(1);
const yearEnd = new Date();
yearEnd.setHours(23, 59, 59, 59);
yearEnd.setMonth(11);
yearEnd.setDate(31);
const startedTime = yearStart.getTime();
const endedTime = yearEnd.getTime();
const dayTimestamp = 1000 * 60 * 60 * 24;
const daysInYear = Math.floor((endedTime - startedTime) / dayTimestamp);
// sets the holidays per year to the amount of days in the current year
let holidaysConfig = await app.models.WorkCenterHoliday.findOne({
where: {
workCenterFk: 1,
year: today.getFullYear()
}});
let originalHolidaysValue = holidaysConfig.days;
await app.models.WorkCenterHoliday.updateAll(
{
workCenterFk: 1,
year: today.getFullYear()
},
{
days: daysInYear
}
);
// normal test begins
const contract = await app.models.WorkerLabour.findById(106);
const contractStartDate = contract.started;
const startingContract = new Date();
startingContract.setHours(0, 0, 0, 0);
startingContract.setMonth(today.getMonth());
startingContract.setDate(1);
await contract.updateAttributes({started: startingContract});
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, yearStart, yearEnd);
let calendar = result[0];
let absences = result[1];
let remainingDays = 0;
for (let i = today.getMonth(); i < 12; i++) {
today.setMonth(i + 1);
today.setDate(0);
remainingDays += today.getDate();
}
expect(calendar.totalHolidays).toEqual(remainingDays);
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');
// resets the holidays per year with originalHolidaysValue and the contract starting date
await app.models.WorkCenterHoliday.updateAll(
{
workCenterFk: 1,
year: today.getFullYear()
},
{
days: originalHolidaysValue
}
);
await contract.updateAttributes({started: contractStartDate});
});
});