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

157 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-05-17 11:27:51 +00:00
import './index.js';
2019-11-18 15:37:33 +00:00
describe('Component vnWorkerTimeControl', () => {
let $httpBackend;
let $scope;
let $element;
let controller;
beforeEach(ngModule('worker'));
2020-07-23 14:46:16 +00:00
beforeEach(inject(($componentController, $rootScope, $stateParams, _$httpBackend_) => {
2019-11-18 15:37:33 +00:00
$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();
2020-02-27 06:19:42 +00:00
jest.spyOn(controller, 'fetchHours').mockReturnThis();
2019-11-18 15:37:33 +00:00
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();
2019-05-17 11:27:51 +00:00
});
2019-11-18 15:37:33 +00:00
});
2019-05-17 11:27:51 +00:00
2019-11-18 15:37:33 +00:00
describe('hours() setter', () => {
it(`should set hours data at it's corresponding week day`, () => {
let today = new Date();
2020-02-27 06:19:42 +00:00
jest.spyOn(controller, 'fetchHours').mockReturnThis();
2019-11-18 15:37:33 +00:00
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);
2019-05-17 11:27:51 +00:00
});
2019-11-18 15:37:33 +00:00
});
2019-05-17 11:27:51 +00:00
2019-11-18 15:37:33 +00:00
describe('getWorkedHours() ', () => {
2020-02-26 12:22:52 +00:00
it('should set the weekdays expected and worked hours plus the total worked hours', () => {
2019-11-18 15:37:33 +00:00
let today = new Date();
2020-02-27 06:19:42 +00:00
jest.spyOn(controller, 'fetchHours').mockReturnThis();
2019-11-18 15:37:33 +00:00
controller.date = today;
2019-11-18 15:37:33 +00:00
let sixHoursInSeconds = 6 * 60 * 60;
let tenHoursInSeconds = 10 * 60 * 60;
let response = [
{
dated: today,
expectedHours: sixHoursInSeconds,
workedHours: tenHoursInSeconds,
2019-05-17 11:27:51 +00:00
2019-11-18 15:37:33 +00:00
},
];
$httpBackend.whenRoute('GET', 'Workers/:id/getWorkedHours')
.respond(response);
2019-05-17 11:27:51 +00:00
2019-11-18 15:37:33 +00:00
today.setHours(0, 0, 0, 0);
2019-11-18 15:37:33 +00:00
let weekOffset = today.getDay() - 1;
if (weekOffset < 0) weekOffset = 6;
2019-11-18 15:37:33 +00:00
let started = new Date(today.getTime());
started.setDate(started.getDate() - weekOffset);
controller.started = started;
2019-11-18 15:37:33 +00:00
let ended = new Date(started.getTime());
ended.setHours(23, 59, 59, 59);
ended.setDate(ended.getDate() + 6);
controller.ended = ended;
2019-05-17 11:27:51 +00:00
2019-11-18 15:37:33 +00:00
controller.getWorkedHours(controller.started, controller.ended);
2019-05-17 11:27:51 +00:00
2019-11-18 15:37:33 +00:00
$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');
2019-05-17 11:27:51 +00:00
});
describe('formatHours() ', () => {
it(`should format a passed timestamp to hours and minutes`, () => {
2019-11-18 15:37:33 +00:00
const result = controller.formatHours(3600);
2019-05-17 11:27:51 +00:00
expect(result).toEqual('01:00');
});
});
2021-06-18 06:03:55 +00:00
describe('save() ', () => {
it(`should make a query an then call to the fetchHours() method`, () => {
controller.fetchHours = jest.fn();
controller.selectedRow = {id: 1, timed: new Date(), direction: 'in'};
controller.$.editEntry = {
hide: () => {}
};
const expectedParams = {direction: 'in'};
$httpBackend.expect('POST', 'WorkerTimeControls/1/updateTimeEntry', expectedParams).respond(200);
controller.save();
$httpBackend.flush();
expect(controller.fetchHours).toHaveBeenCalledWith();
});
});
describe('$postLink() ', () => {
it(`should set the controller date as today if no timestamp is defined`, () => {
controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())};
controller.$params = {timestamp: undefined};
controller.$postLink();
expect(controller.date).toEqual(jasmine.any(Date));
});
it(`should set the controller date using the received timestamp`, () => {
const date = 'Wed, 31 Dec 1969 23:00:00 GMT';
const timestamp = 1;
controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())};
controller.$.calendar = {};
controller.$params = {timestamp: timestamp};
controller.$postLink();
expect(controller.date.toUTCString()).toEqual(date);
});
});
2019-05-17 11:27:51 +00:00
});
});