From c5e953f922bc7e8ba128058e7377eb50f769a193 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 22 Mar 2019 12:51:44 +0100 Subject: [PATCH] worker calendar front & back unit tests #1248 --- db/install/dump/fixtures.sql | 33 +++++ .../worker-calendar/specs/absences.spec.js | 30 +++++ modules/worker/front/calendar/index.js | 14 ++- modules/worker/front/calendar/index.spec.js | 117 ++++++++++++++++++ 4 files changed, 189 insertions(+), 5 deletions(-) create mode 100644 modules/worker/back/methods/worker-calendar/specs/absences.spec.js create mode 100644 modules/worker/front/calendar/index.spec.js diff --git a/db/install/dump/fixtures.sql b/db/install/dump/fixtures.sql index d451b94a3..5fbe8f863 100644 --- a/db/install/dump/fixtures.sql +++ b/db/install/dump/fixtures.sql @@ -1216,3 +1216,36 @@ INSERT INTO `postgresql`.`business_labour`(`business_id`, `notes`, `department_i (1, NULL, 22, 4, 0, 1, 1, 1, 1), (2, 'From las friday worker ownes the company 1 hour', 23, 1, 0, 1, 0, 1, 1); +INSERT INTO `vn`.`workCenter` (`id`, `name`, `warehouseFk`) + VALUES + ('1', 'Silla', '1'), + ('5', 'Madrid', '5'); + +INSERT INTO `vn2008`.`workcenter_holiday` (`workcenter_id`, `day`, `year`) + VALUES + ('1', '27.5', YEAR(CURDATE())), + ('5', '22', YEAR(CURDATE())), + ('1', '24.5', YEAR(DATE_ADD(CURDATE(), INTERVAL -1 YEAR))), + ('5', '23', YEAR(DATE_ADD(CURDATE(), INTERVAL -1 YEAR))); + +INSERT INTO `postgresql`.`calendar_state` (`calendar_state_id`, `type`, `rgb`) + VALUES + ('1', 'Holidays', '#FF4444'), + ('2', 'Leave of absence', '#C71585'); + +INSERT INTO `postgresql`.`calendar_employee` (`business_id`, `calendar_state_id`, `date`) + VALUES + ('1', '1', DATE_ADD(CURDATE(), INTERVAL 10 DAY)), + ('1', '1', DATE_ADD(CURDATE(), INTERVAL 11 DAY)), + ('1', '1', DATE_ADD(CURDATE(), INTERVAL 12 DAY)), + ('1', '1', DATE_ADD(CURDATE(), INTERVAL 20 DAY)), + ('1', '2', DATE_ADD(CURDATE(), INTERVAL -10 DAY)), + ('1', '1', DATE_ADD(CURDATE(), INTERVAL -12 DAY)), + ('1', '2', DATE_ADD(CURDATE(), INTERVAL -20 DAY)), + ('2', '1', DATE_ADD(CURDATE(), INTERVAL 15 DAY)), + ('2', '1', DATE_ADD(CURDATE(), INTERVAL 16 DAY)), + ('2', '1', DATE_ADD(CURDATE(), INTERVAL 20 DAY)), + ('2', '1', DATE_ADD(CURDATE(), INTERVAL 30 DAY)), + ('2', '2', DATE_ADD(CURDATE(), INTERVAL -10 DAY)), + ('2', '1', DATE_ADD(CURDATE(), INTERVAL -12 DAY)), + ('2', '2', DATE_ADD(CURDATE(), INTERVAL -20 DAY)); \ No newline at end of file diff --git a/modules/worker/back/methods/worker-calendar/specs/absences.spec.js b/modules/worker/back/methods/worker-calendar/specs/absences.spec.js new file mode 100644 index 000000000..625e20dc7 --- /dev/null +++ b/modules/worker/back/methods/worker-calendar/specs/absences.spec.js @@ -0,0 +1,30 @@ +const app = require('vn-loopback/server/server'); + +describe('Worker absences()', () => { + it('should import sales to a claim actions from an specific ticket', 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); + // Last day of previous month (January) + 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(15); + expect(calendar.holidaysEnjoyed).toEqual(5); + + expect(absences[0].absenceType().id).toEqual(2); + expect(absences[5].absenceType().id).toEqual(1); + }); +}); diff --git a/modules/worker/front/calendar/index.js b/modules/worker/front/calendar/index.js index e871de1cc..c97e1b138 100644 --- a/modules/worker/front/calendar/index.js +++ b/modules/worker/front/calendar/index.js @@ -37,9 +37,10 @@ class Controller { } setHolidays(data) { + if (!data.holidays) return; + const holidays = data.holidays; const events = []; - holidays.forEach(holiday => { events.push({ date: holiday.dated, @@ -52,6 +53,8 @@ class Controller { } setWorkerCalendar(data) { + if (!data.absences) return; + const absences = data.absences; const events = []; absences.forEach(absence => { @@ -91,11 +94,12 @@ class Controller { let months = new Array(12); for (let i = 0; i < months.length; i++) { - const currentDate = new Date(); - currentDate.setMonth(i); - currentDate.setDate(1); + const now = new Date(); + now.setHours(0, 0, 0, 0); + now.setMonth(i); + now.setDate(1); - months[i] = currentDate; + months[i] = now; } return months; diff --git a/modules/worker/front/calendar/index.spec.js b/modules/worker/front/calendar/index.spec.js new file mode 100644 index 000000000..d8f42f762 --- /dev/null +++ b/modules/worker/front/calendar/index.spec.js @@ -0,0 +1,117 @@ +import './index'; + +describe('Worker', () => { + describe('Component vnWorkerCalendar', () => { + let $componentController; + let $httpParamSerializer; + let $httpBackend; + let $scope; + let controller; + + beforeEach(ngModule('worker')); + + 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 `, () => { + 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].title).toEqual('New year'); + expect(controller.events[0].isRemovable).toEqual(false); + }); + }); + + describe('setWorkerCalendar()', () => { + it(`should `, () => { + 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].title).toEqual('Holiday'); + expect(controller.events[0].style).toBeDefined(); + expect(controller.events[1].title).toEqual('Leave'); + expect(controller.events[1].style).toBeDefined(); + }); + }); + }); +});