salix/modules/worker/front/time-control/index.spec.js

119 lines
4.0 KiB
JavaScript

import './index.js';
describe('Component vnWorkerTimeControl', () => {
let $httpBackend;
let $scope;
let $element;
let controller;
beforeEach(ngModule('worker'));
beforeEach(angular.mock.inject(($componentController, $compile, $rootScope, $stateParams, _$httpBackend_) => {
$stateParams.id = 1;
$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();
jest.spyOn(controller, 'fetchHours').mockReturnValue('ok!');
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', () => {
it(`should set hours data at it's corresponding week day`, () => {
let today = new Date();
jest.spyOn(controller, 'fetchHours').mockReturnValue('ok!');
controller.date = today;
let hours = [
{
id: 1,
timed: controller.started.toJSON(),
userFk: 1
}, {
id: 2,
timed: controller.ended.toJSON(),
userFk: 1
}, {
id: 3,
timed: controller.ended.toJSON(),
userFk: 1
}
];
controller.hours = hours;
expect(controller.weekDays.length).toEqual(7);
expect(controller.weekDays[0].hours.length).toEqual(1);
expect(controller.weekDays[6].hours.length).toEqual(2);
});
});
describe('getWorkedHours() ', () => {
it('should set the weekdays expected and worked hours plus the total worked hours', () => {
let today = new Date();
jest.spyOn(controller, 'fetchHours').mockReturnValue('ok!');
controller.date = today;
let sixHoursInSeconds = 6 * 60 * 60;
let tenHoursInSeconds = 10 * 60 * 60;
let response = [
{
dated: today,
expectedHours: sixHoursInSeconds,
workedHours: tenHoursInSeconds,
},
];
$httpBackend.whenRoute('GET', 'Workers/:id/getWorkedHours')
.respond(response);
today.setHours(0, 0, 0, 0);
let weekOffset = today.getDay() - 1;
if (weekOffset < 0) weekOffset = 6;
let started = new Date(today.getTime());
started.setDate(started.getDate() - weekOffset);
controller.started = started;
let ended = new Date(started.getTime());
ended.setHours(23, 59, 59, 59);
ended.setDate(ended.getDate() + 6);
controller.ended = ended;
controller.getWorkedHours(controller.started, controller.ended);
$httpBackend.flush();
expect(controller.weekDays.length).toEqual(7);
expect(controller.weekDays[weekOffset].expectedHours).toEqual(response[0].expectedHours);
expect(controller.weekDays[weekOffset].workedHours).toEqual(response[0].workedHours);
expect(controller.weekTotalHours).toEqual('10:00');
});
describe('formatHours() ', () => {
it(`should format a passed timestamp to hours and minutes`, () => {
const result = controller.formatHours(3600);
expect(result).toEqual('01:00');
});
});
});
});