#1852 worker.time-control

This commit is contained in:
Carlos Jimenez Ruiz 2019-11-18 16:37:33 +01:00
parent eeead2cc86
commit f998a04808
1 changed files with 85 additions and 92 deletions

View File

@ -1,122 +1,115 @@
import './index.js'; import './index.js';
describe('Worker', () => {
describe('Component vnWorkerTimeControl', () => {
let $httpBackend;
let $scope;
let $element;
let controller;
beforeEach(ngModule('worker')); describe('Component vnWorkerTimeControl', () => {
let $httpBackend;
let $scope;
let $element;
let controller;
beforeEach(angular.mock.inject(($compile, $rootScope, $stateParams, _$httpBackend_) => { beforeEach(ngModule('worker'));
$stateParams.id = 1;
$httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
$element = $compile('<vn-worker-time-control></vn-worker-time-control>')($scope);
controller = $element.controller('vnWorkerTimeControl');
}));
afterEach(() => { beforeEach(angular.mock.inject(($componentController, $compile, $rootScope, $stateParams, _$httpBackend_) => {
$scope.$destroy(); $stateParams.id = 1;
$element.remove(); $httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
$element = angular.element('<vn-worker-time-control></vn-worker-time-control>');
controller = $componentController('vnWorkerTimeControl', {$element, $scope});
}));
describe('date() setter', () => {
it(`should set the weekDays, the date in the controller and call fetchHours`, () => {
let today = new Date();
spyOn(controller, 'fetchHours');
controller.date = today;
expect(controller._date).toEqual(today);
expect(controller.started).toBeDefined();
expect(controller.ended).toBeDefined();
expect(controller.weekDays.length).toEqual(7);
expect(controller.fetchHours).toHaveBeenCalledWith();
}); });
});
describe('hours() setter', () => { describe('hours() setter', () => {
it(`should set hours data at it's corresponding week day`, () => { it(`should set hours data at it's corresponding week day`, () => {
let today = new Date(); let today = new Date();
spyOn(controller, 'fetchHours');
controller.date = today; controller.date = today;
let hours = [ let hours = [
{ {
id: 1, id: 1,
timed: controller.started.toJSON(), timed: controller.started.toJSON(),
userFk: 1 userFk: 1
}, { }, {
id: 2, id: 2,
timed: today.toJSON(), timed: controller.ended.toJSON(),
userFk: 1 userFk: 1
}, { }, {
id: 3, id: 3,
timed: today.toJSON(), timed: controller.ended.toJSON(),
userFk: 1 userFk: 1
} }
]; ];
controller.hours = hours; controller.hours = hours;
let todayInWeek = today.getDay() - 1; expect(controller.weekDays.length).toEqual(7);
expect(controller.weekDays[0].hours.length).toEqual(1);
expect(controller.weekDays.length).toEqual(7); expect(controller.weekDays[6].hours.length).toEqual(2);
expect(controller.weekDays[0].hours.length).toEqual(1);
expect(controller.weekDays[todayInWeek].hours.length).toEqual(2);
});
}); });
});
describe('getWorkedHours() ', () => { describe('getWorkedHours() ', () => {
fit(`should set the week days and the worked hours in today`, () => { it(`should `, () => {
let today = new Date(); let today = new Date();
spyOn(controller, 'fetchHours');
$httpBackend.whenRoute('GET', 'WorkerTimeControls/filter').respond({}); controller.date = today;
$httpBackend.whenRoute('GET', 'WorkerCalendars/absences').respond({});
$httpBackend.whenRoute('GET', 'Workers/:id/getWorkedHours')
.respond([
{dated: today},
]);
today.setHours(0, 0, 0, 0); let sixHoursInSeconds = 6 * 60 * 60;
let tenHoursInSeconds = 10 * 60 * 60;
let response = [
{
dated: today,
expectedHours: sixHoursInSeconds,
workedHours: tenHoursInSeconds,
let weekOffset = today.getDay() - 1; },
if (weekOffset < 0) weekOffset = 6; ];
$httpBackend.whenRoute('GET', 'Workers/:id/getWorkedHours')
.respond(response);
let started = new Date(today.getTime()); today.setHours(0, 0, 0, 0);
started.setDate(started.getDate() - weekOffset);
controller.started = started;
let ended = new Date(started.getTime()); let weekOffset = today.getDay() - 1;
ended.setHours(23, 59, 59, 59); if (weekOffset < 0) weekOffset = 6;
ended.setDate(ended.getDate() + 6);
controller.ended = ended;
controller.getWorkedHours(controller.started, controller.ended); let started = new Date(today.getTime());
started.setDate(started.getDate() - weekOffset);
controller.started = started;
$httpBackend.flush(); let ended = new Date(started.getTime());
ended.setHours(23, 59, 59, 59);
ended.setDate(ended.getDate() + 6);
controller.ended = ended;
expect(controller.started).toEqual(started); controller.getWorkedHours(controller.started, controller.ended);
});
});
describe('weekTotalHours() ', () => { $httpBackend.flush();
it(`should return a total worked hours from a week`, () => {
const hourOne = new Date();
hourOne.setHours(7, 0, 0, 0);
const hourTwo = new Date();
hourTwo.setHours(10, 0, 0, 0);
const hourThree = new Date();
hourThree.setHours(10, 20, 0, 0);
const hourFour = new Date();
hourFour.setHours(15, 0, 0, 0);
const weekday = {hours: [ expect(controller.weekDays.length).toEqual(7);
{id: 1, timed: hourOne}, expect(controller.weekDays[weekOffset].expectedHours).toEqual(response[0].expectedHours);
{id: 2, timed: hourTwo}, expect(controller.weekDays[weekOffset].workedHours).toEqual(response[0].workedHours);
{id: 3, timed: hourThree}, expect(controller.weekTotalHours).toEqual('10:00');
{id: 4, timed: hourFour}
]};
controller.weekDays = [weekday];
const weekdayHours = controller.getWeekdayTotalHours(weekday);
const weekHours = controller.weekTotalHours;
expect(weekdayHours).toEqual('08:00');
expect(weekHours).toEqual('08:00');
});
}); });
describe('formatHours() ', () => { describe('formatHours() ', () => {
it(`should format a passed timestamp to hours and minutes`, () => { it(`should format a passed timestamp to hours and minutes`, () => {
const result = controller.formatHours(3600000); const result = controller.formatHours(3600);
expect(result).toEqual('01:00'); expect(result).toEqual('01:00');
}); });