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

159 lines
6.0 KiB
JavaScript
Raw Normal View History

import './index';
describe('Worker', () => {
describe('Component vnWorkerCalendar', () => {
let $httpBackend;
let $scope;
let controller;
2019-10-23 15:38:35 +00:00
let year = new Date().getFullYear();
beforeEach(ngModule('worker'));
2019-10-23 15:38:35 +00:00
beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => {
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
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;
}));
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-15 09:35:34 +00:00
jest.spyOn(controller, 'getIsSubordinate').mockReturnThis();
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);
$httpBackend.whenRoute('GET', 'WorkerCalendars/absences')
2019-10-23 15:38:35 +00:00
.respond({
holidays: [
{dated: today, detail: {description: 'New year'}},
{dated: tomorrow, detail: {description: 'Easter'}}
],
absences: [
{dated: today, absenceType: {name: 'Holiday', rgb: '#aaa'}},
{dated: yesterday, absenceType: {name: 'Leave', rgb: '#bbb'}}
]
});
controller.worker = {id: 1};
$httpBackend.flush();
2019-10-23 15:38:35 +00:00
let events = controller.events;
2019-10-23 15:38:35 +00:00
expect(events[today.getTime()].name).toEqual('Holiday');
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();
$httpBackend.whenRoute('GET', 'WorkerCalendars/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 = [];
controller.onSelection($event, $days);
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu');
});
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);
});
});
});
});