salix/modules/worker/front/calendar/index.spec.js

369 lines
14 KiB
JavaScript
Raw Normal View History

import './index';
describe('Worker', () => {
describe('Component vnWorkerCalendar', () => {
let $httpBackend;
2020-07-16 08:04:16 +00:00
let $httpParamSerializer;
let $scope;
let controller;
2019-10-23 15:38:35 +00:00
let year = new Date().getFullYear();
beforeEach(ngModule('worker'));
2020-07-23 14:46:16 +00:00
beforeEach(inject(($componentController, $rootScope, _$httpParamSerializer_, _$httpBackend_) => {
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
2020-07-16 08:04:16 +00:00
$httpParamSerializer = _$httpParamSerializer_;
2020-07-15 09:35:34 +00:00
const $element = angular.element('<vn-worker-calendar></vn-worker-calendar>');
2019-10-23 15:38:35 +00:00
controller = $componentController('vnWorkerCalendar', {$element, $scope});
2020-07-15 09:35:34 +00:00
controller.isSubordinate = true;
2020-07-16 08:04:16 +00:00
controller.absenceType = {id: 1, name: 'Holiday', code: 'holiday', rgb: 'red'};
controller.$params.id = 106;
controller._worker = {id: 106};
}));
2020-08-11 10:16:19 +00:00
describe('year() getter', () => {
it(`should return the year number of the calendar date`, () => {
expect(controller.year).toEqual(year);
});
});
describe('year() setter', () => {
it(`should set the year of the calendar date`, () => {
2020-08-18 12:14:34 +00:00
jest.spyOn(controller, 'refresh').mockReturnValue(Promise.resolve());
2020-08-18 11:50:08 +00:00
2020-08-11 10:16:19 +00:00
const previousYear = year - 1;
controller.year = previousYear;
expect(controller.year).toEqual(previousYear);
expect(controller.date.getFullYear()).toEqual(previousYear);
2020-08-18 11:50:08 +00:00
expect(controller.refresh).toHaveBeenCalledWith();
2020-08-11 10:16:19 +00:00
});
});
2019-10-23 15:38:35 +00:00
describe('started property', () => {
it(`should return first day and month of current year`, () => {
let started = new Date(year, 0, 1);
expect(controller.started).toEqual(started);
});
});
2019-10-23 15:38:35 +00:00
describe('ended property', () => {
it(`should return last day and month of current year`, () => {
2019-10-23 15:38:35 +00:00
let ended = new Date(year, 11, 31);
expect(controller.ended).toEqual(ended);
});
});
2019-10-23 15:38:35 +00:00
describe('months property', () => {
it(`should return an array of twelve months length`, () => {
2019-10-23 15:38:35 +00:00
const ended = new Date(year, 11, 1);
expect(controller.months.length).toEqual(12);
expect(controller.months[0]).toEqual(controller.started);
expect(controller.months[11]).toEqual(ended);
});
});
describe('worker() setter', () => {
2019-10-23 15:38:35 +00:00
it(`should perform a get query and set the reponse data on the model`, () => {
2020-07-16 09:59:16 +00:00
jest.spyOn(controller, 'getIsSubordinate').mockReturnValue(true);
2020-07-15 09:35:34 +00:00
2019-10-23 15:38:35 +00:00
let today = new Date();
let tomorrow = new Date(today.getTime());
tomorrow.setDate(tomorrow.getDate() + 1);
let yesterday = new Date(today.getTime());
yesterday.setDate(yesterday.getDate() - 1);
2020-08-10 12:29:25 +00:00
$httpBackend.whenRoute('GET', 'Calendars/absences')
2019-10-23 15:38:35 +00:00
.respond({
holidays: [
2020-09-07 06:53:47 +00:00
{dated: today, detail: {name: 'New year'}},
{dated: tomorrow, detail: {name: 'Easter'}}
2019-10-23 15:38:35 +00:00
],
absences: [
{dated: today, absenceType: {name: 'Holiday', rgb: '#aaa'}},
{dated: yesterday, absenceType: {name: 'Leave', rgb: '#bbb'}}
]
});
2020-07-16 09:59:16 +00:00
controller.worker = {id: 107};
$httpBackend.flush();
2019-10-23 15:38:35 +00:00
let events = controller.events;
2020-09-24 08:14:53 +00:00
expect(events[today.getTime()].name).toEqual('New year, Holiday');
2019-10-23 15:38:35 +00:00
expect(events[tomorrow.getTime()].name).toEqual('Easter');
expect(events[yesterday.getTime()].name).toEqual('Leave');
expect(events[yesterday.getTime()].color).toEqual('#bbb');
2020-07-15 09:35:34 +00:00
expect(controller.getIsSubordinate).toHaveBeenCalledWith();
});
});
2019-10-23 15:38:35 +00:00
describe('formatDay()', () => {
it(`should set the day element style`, () => {
2020-07-15 09:35:34 +00:00
jest.spyOn(controller, 'getIsSubordinate').mockReturnThis();
2019-10-23 15:38:35 +00:00
let today = new Date();
2020-08-10 12:29:25 +00:00
$httpBackend.whenRoute('GET', 'Calendars/absences')
2019-10-23 15:38:35 +00:00
.respond({
absences: [
{dated: today, absenceType: {name: 'Holiday', rgb: '#000'}}
]
});
2019-10-23 15:38:35 +00:00
controller.worker = {id: 1};
$httpBackend.flush();
2019-10-23 15:38:35 +00:00
let dayElement = angular.element('<div><section></section></div>')[0];
let dayNumber = dayElement.firstElementChild;
2019-10-23 15:38:35 +00:00
controller.formatDay(today, dayElement);
2019-10-23 15:38:35 +00:00
expect(dayNumber.title).toEqual('Holiday');
expect(dayNumber.style.backgroundColor).toEqual('rgb(0, 0, 0)');
});
});
2020-07-15 09:35:34 +00:00
describe('pick()', () => {
it(`should set the absenceType property to null if they match with the current one`, () => {
const absenceType = {id: 1, name: 'Holiday'};
controller.absenceType = absenceType;
controller.pick(absenceType);
expect(controller.absenceType).toBeNull();
});
it(`should set the absenceType property`, () => {
const absenceType = {id: 1, name: 'Holiday'};
const expectedAbsence = {id: 2, name: 'Leave of absence'};
controller.absenceType = absenceType;
controller.pick(expectedAbsence);
expect(controller.absenceType).toEqual(expectedAbsence);
});
});
describe('onSelection()', () => {
it(`should show an snackbar message if no absence type is selected`, () => {
jest.spyOn(controller.vnApp, 'showMessage').mockReturnThis();
const $event = {};
const $days = [];
2020-07-16 08:04:16 +00:00
controller.absenceType = null;
2020-07-15 09:35:34 +00:00
controller.onSelection($event, $days);
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu');
});
2020-08-18 11:50:08 +00:00
it(`should show an snackbar message if the selected day is not within the current year`, () => {
jest.spyOn(controller.vnApp, 'showMessage').mockReturnThis();
const selectedDay = new Date();
const $event = {
target: {
closest: () => {
return {$ctrl: {}};
}
}
};
const $days = [selectedDay];
const pastYear = new Date();
pastYear.setFullYear(pastYear.getFullYear() - 1);
controller.date = pastYear;
controller.absenceType = {id: 1};
controller.onSelection($event, $days);
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('You can just add absences within the current year');
});
2020-07-15 09:35:34 +00:00
it(`should call to the create() method`, () => {
jest.spyOn(controller, 'create').mockReturnThis();
const selectedDay = new Date();
const $event = {
target: {
closest: () => {
return {$ctrl: {}};
}
}
};
const $days = [selectedDay];
controller.absenceType = {id: 1};
controller.onSelection($event, $days);
expect(controller.create).toHaveBeenCalledWith(jasmine.any(Object), selectedDay);
});
2020-07-16 08:04:16 +00:00
it(`should call to the delete() method`, () => {
jest.spyOn(controller, 'delete').mockReturnThis();
const selectedDay = new Date();
const expectedEvent = {
dated: selectedDay,
type: 'holiday'
};
const $event = {
target: {
closest: () => {
return {$ctrl: {}};
}
}
};
const $days = [selectedDay];
controller.events[selectedDay.getTime()] = expectedEvent;
controller.absenceType = {id: 1, code: 'holiday'};
controller.onSelection($event, $days);
expect(controller.delete).toHaveBeenCalledWith(jasmine.any(Object), selectedDay, expectedEvent);
});
it(`should call to the edit() method`, () => {
jest.spyOn(controller, 'edit').mockReturnThis();
const selectedDay = new Date();
const expectedEvent = {
dated: selectedDay,
type: 'leaveOfAbsence'
};
const $event = {
target: {
closest: () => {
return {$ctrl: {}};
}
}
};
const $days = [selectedDay];
controller.events[selectedDay.getTime()] = expectedEvent;
controller.absenceType = {id: 1, code: 'holiday'};
controller.onSelection($event, $days);
expect(controller.edit).toHaveBeenCalledWith(jasmine.any(Object), expectedEvent);
});
});
describe('create()', () => {
it(`should make a HTTP POST query and then call to the repaintCanceller() method`, () => {
jest.spyOn(controller, 'repaintCanceller').mockReturnThis();
const dated = new Date();
const calendarElement = {};
const expectedResponse = {id: 10};
$httpBackend.expect('POST', `Workers/106/createAbsence`).respond(200, expectedResponse);
controller.create(calendarElement, dated);
$httpBackend.flush();
const createdEvent = controller.events[dated.getTime()];
const absenceType = controller.absenceType;
expect(createdEvent.absenceId).toEqual(expectedResponse.id);
expect(createdEvent.color).toEqual(absenceType.rgb);
expect(controller.repaintCanceller).toHaveBeenCalled();
});
});
describe('edit()', () => {
it(`should make a HTTP PATCH query and then call to the repaintCanceller() method`, () => {
jest.spyOn(controller, 'repaintCanceller').mockReturnThis();
const event = {absenceId: 10};
const calendarElement = {};
const newAbsenceType = {
id: 2,
name: 'Leave of absence',
code: 'leaveOfAbsence',
rgb: 'purple'
};
controller.absenceType = newAbsenceType;
const expectedParams = {absenceId: 10, absenceTypeId: 2};
$httpBackend.expect('PATCH', `Workers/106/updateAbsence`, expectedParams).respond(200);
controller.edit(calendarElement, event);
$httpBackend.flush();
expect(event.name).toEqual(newAbsenceType.name);
expect(event.color).toEqual(newAbsenceType.rgb);
expect(event.type).toEqual(newAbsenceType.code);
expect(controller.repaintCanceller).toHaveBeenCalled();
});
});
describe('delete()', () => {
it(`should make a HTTP DELETE query and then call to the repaintCanceller() method`, () => {
jest.spyOn(controller, 'repaintCanceller').mockReturnThis();
const expectedParams = {absenceId: 10};
const calendarElement = {};
const selectedDay = new Date();
const expectedEvent = {
dated: selectedDay,
type: 'leaveOfAbsence',
absenceId: 10
};
controller.events[selectedDay.getTime()] = expectedEvent;
const serializedParams = $httpParamSerializer(expectedParams);
$httpBackend.expect('DELETE', `Workers/106/deleteAbsence?${serializedParams}`).respond(200);
controller.delete(calendarElement, selectedDay, expectedEvent);
$httpBackend.flush();
const event = controller.events[selectedDay.getTime()];
expect(event).toBeUndefined();
expect(controller.repaintCanceller).toHaveBeenCalled();
});
});
describe('repaintCanceller()', () => {
it(`should cancell the callback execution timer`, () => {
jest.spyOn(window, 'clearTimeout');
jest.spyOn(window, 'setTimeout');
const timeoutId = 90;
controller.canceller = timeoutId;
controller.repaintCanceller(() => {
return 'My callback';
});
expect(window.clearTimeout).toHaveBeenCalledWith(timeoutId);
expect(window.setTimeout).toHaveBeenCalledWith(jasmine.any(Function), 500);
});
});
describe('refresh()', () => {
it(`should make a HTTP GET query and then call to the onData() method`, () => {
jest.spyOn(controller, 'onData').mockReturnThis();
const dated = controller.date;
const started = new Date(dated.getTime());
started.setMonth(0);
started.setDate(1);
const ended = new Date(dated.getTime());
ended.setMonth(12);
ended.setDate(0);
controller.started = started;
controller.ended = ended;
const expecteResponse = [{id: 1}];
const expectedParams = {workerFk: 106, started: started, ended: ended};
const serializedParams = $httpParamSerializer(expectedParams);
2020-08-10 12:29:25 +00:00
$httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse);
2020-07-16 08:04:16 +00:00
controller.refresh();
$httpBackend.flush();
expect(controller.onData).toHaveBeenCalledWith(expecteResponse);
});
2020-07-15 09:35:34 +00:00
});
});
});