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

150 lines
5.6 KiB
JavaScript
Raw Normal View History

import './index';
describe('Worker', () => {
describe('Component vnWorkerCalendar', () => {
let $componentController;
let $httpParamSerializer;
let $httpBackend;
let $scope;
let controller;
2019-09-13 14:09:14 +00:00
beforeEach(angular.mock.module('worker', $translateProvider => {
$translateProvider.translations('en', {});
}));
beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
$componentController = _$componentController_;
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
$httpParamSerializer = _$httpParamSerializer_;
controller = $componentController('vnWorkerCalendar', {$scope, $httpBackend});
}));
describe('started() getter', () => {
it(`should return first day and month of current year`, () => {
let started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
expect(controller.started).toEqual(started);
});
});
describe('ended() getter', () => {
it(`should return last day and month of current year`, () => {
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
// Last day of previous month (January)
ended.setDate(0);
expect(controller.ended).toEqual(ended);
});
});
describe('monthsOfYear()', () => {
it(`should return an array of twelve months length`, () => {
const months = controller.monthsOfYear();
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(11);
ended.setDate(1);
expect(months.length).toEqual(12);
expect(months[0]).toEqual(controller.started);
expect(months[11]).toEqual(ended);
});
});
describe('worker() setter', () => {
it(`should perform a get query and call setHolidays() and setWorkerCalendar() methods`, () => {
const worker = {id: 106};
spyOn(controller, 'setHolidays');
spyOn(controller, 'setWorkerCalendar');
const expectedData = {
calendar: {},
absences: {}
};
let params = $httpParamSerializer({
workerFk: worker.id,
started: controller.started,
ended: controller.ended
});
$httpBackend.when('GET', `/worker/api/WorkerCalendars/absences?${params}`).respond(expectedData);
$httpBackend.expect('GET', `/worker/api/WorkerCalendars/absences?${params}`);
controller.worker = worker;
$httpBackend.flush();
expect(controller.setHolidays).toHaveBeenCalledWith(expectedData);
expect(controller.setHolidays).toHaveBeenCalledWith(expectedData);
});
});
describe('setHolidays()', () => {
it(`should set holidays`, () => {
const data = {holidays: [
{dated: new Date(), detail: {description: 'New year'}},
{dated: new Date(), detail: {description: 'Easter'}}
]};
controller.setHolidays(data);
expect(controller.events.length).toEqual(2);
expect(controller.events[0].name).toEqual('New year');
expect(controller.events[0].isRemovable).toEqual(false);
});
});
describe('setWorkerCalendar()', () => {
it(`should set absences of differente types`, () => {
const data = {absences: [
{dated: new Date(), absenceType: {name: 'Holiday', rgb: '#000'}},
{dated: new Date(), absenceType: {name: 'Leave', rgb: '#000'}}
]};
controller.setWorkerCalendar(data);
expect(controller.events.length).toEqual(2);
expect(controller.events[0].name).toEqual('Holiday');
expect(controller.events[0].style).toBeDefined();
expect(controller.events[1].name).toEqual('Leave');
expect(controller.events[1].style).toBeDefined();
});
});
describe('absenceTypes() setter', () => {
it(`should set the absence types in the controller`, () => {
const absenceTypes = [
{id: 1, name: 'Holiday', rgb: '#000'},
{id: 2, name: 'Leave', rgb: '#000'}
];
expect(controller._absenceTypes).not.toBeDefined();
controller.absenceTypes = absenceTypes;
expect(controller._absenceTypes.length).toEqual(2);
});
it(`should set the absence types in the controller as formated legends`, () => {
const absenceTypes = [
{id: 1, name: 'Holiday', rgb: '#000'},
{id: 2, name: 'Leave', rgb: '#000'}
];
expect(controller.legends).not.toBeDefined();
controller.absenceTypes = absenceTypes;
expect(controller.legends.length).toEqual(2);
expect(controller.legends[0].color).toBeDefined();
expect(controller.legends[1].color).toBeDefined();
});
});
});
});