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

287 lines
11 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;
2023-03-08 11:06:31 +00:00
let $httpParamSerializer;
2019-11-18 15:37:33 +00:00
beforeEach(ngModule('worker'));
2023-03-08 11:06:31 +00:00
beforeEach(inject(($componentController, $rootScope, $stateParams, _$httpBackend_, _$httpParamSerializer_) => {
2019-11-18 15:37:33 +00:00
$stateParams.id = 1;
$httpBackend = _$httpBackend_;
2023-03-08 11:06:31 +00:00
$httpParamSerializer = _$httpParamSerializer_;
2019-11-18 15:37:33 +00:00
$scope = $rootScope.$new();
$element = angular.element('<vn-worker-time-control></vn-worker-time-control>');
controller = $componentController('vnWorkerTimeControl', {$element, $scope});
2023-07-14 11:01:31 +00:00
controller.card = {
hasWorkCenter: true
2023-06-22 07:48:14 +00:00
};
2019-11-18 15:37:33 +00:00
}));
describe('date() setter', () => {
2023-09-28 09:36:03 +00:00
it(`should set the weekDays and the date in the controller`, () => {
2023-01-16 14:18:24 +00:00
let today = Date.vnNew();
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);
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`, () => {
2023-01-16 14:18:24 +00:00
let today = Date.vnNew();
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', () => {
2023-01-16 14:18:24 +00:00
let today = Date.vnNew();
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
2023-03-08 08:51:21 +00:00
$httpBackend.whenRoute('GET', 'WorkerTimeControlMails')
.respond([]);
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);
$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`, () => {
2023-04-04 07:29:51 +00:00
const today = Date.vnNew();
jest.spyOn(controller, 'getWeekData').mockReturnThis();
jest.spyOn(controller, 'getMailStates').mockReturnThis();
controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())};
controller.date = today;
2021-06-18 06:03:55 +00:00
controller.fetchHours = jest.fn();
controller.selectedRow = {id: 1, timed: Date.vnNew(), direction: 'in', $orgRow: {direction: null}};
2021-06-18 06:03:55 +00:00
controller.$.editEntry = {
hide: () => {}
};
const expectedParams = {direction: 'in'};
$httpBackend.expect('POST', 'WorkerTimeControls/1/updateTimeEntry', expectedParams).respond(200);
controller.save();
$httpBackend.flush();
expect(controller.fetchHours).toHaveBeenCalledWith();
});
});
2023-03-08 11:06:31 +00:00
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();
2023-03-08 11:06:31 +00:00
expect(controller.date).toEqual(jasmine.any(Date));
});
it(`should set the controller date using the received timestamp`, () => {
const timestamp = 1;
const date = new Date(timestamp);
controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())};
controller.$.calendar = {};
controller.$params = {timestamp: timestamp};
controller.$postLink();
expect(controller.date.toDateString()).toEqual(date.toDateString());
});
});
describe('getWeekData() ', () => {
it(`should make a query an then update the state and reason`, () => {
const today = Date.vnNew();
const response = [
{
state: 'SENDED',
reason: null
}
];
controller._date = today;
$httpBackend.whenRoute('GET', 'WorkerTimeControlMails')
.respond(response);
controller.getWeekData();
$httpBackend.flush();
expect(controller.state).toBe('SENDED');
expect(controller.reason).toBe(null);
});
});
2023-03-08 11:06:31 +00:00
describe('isSatisfied() ', () => {
it(`should make a query an then call three methods`, () => {
const today = Date.vnNew();
jest.spyOn(controller, 'getWeekData').mockReturnThis();
jest.spyOn(controller, 'getMailStates').mockReturnThis();
jest.spyOn(controller.vnApp, 'showSuccess');
2023-03-08 11:06:31 +00:00
controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())};
controller.worker = {id: 1};
controller.date = today;
controller.weekNumber = 1;
$httpBackend.expect('POST', 'WorkerTimeControls/1/updateMailState').respond();
2023-03-08 11:06:31 +00:00
controller.isSatisfied();
$httpBackend.flush();
2023-03-08 11:06:31 +00:00
expect(controller.getMailStates).toHaveBeenCalledWith(controller.date);
expect(controller.getWeekData).toHaveBeenCalled();
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
});
});
describe('isUnsatisfied() ', () => {
it(`should throw an error is reason is empty`, () => {
let error;
try {
controller.isUnsatisfied();
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.message).toBe(`You must indicate a reason`);
});
it(`should make a query an then call three methods`, () => {
const today = Date.vnNew();
jest.spyOn(controller, 'getWeekData').mockReturnThis();
jest.spyOn(controller, 'getMailStates').mockReturnThis();
jest.spyOn(controller.vnApp, 'showSuccess');
controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())};
controller.worker = {id: 1};
controller.date = today;
controller.weekNumber = 1;
controller.reason = 'reason';
$httpBackend.expect('POST', 'WorkerTimeControls/1/updateMailState').respond();
2023-03-08 11:06:31 +00:00
controller.isSatisfied();
$httpBackend.flush();
expect(controller.getMailStates).toHaveBeenCalledWith(controller.date);
expect(controller.getWeekData).toHaveBeenCalled();
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
});
});
describe('resendEmail() ', () => {
it(`should make a query an then call showSuccess method`, () => {
const today = Date.vnNew();
2023-04-04 07:29:51 +00:00
2023-03-08 11:06:31 +00:00
jest.spyOn(controller, 'getWeekData').mockReturnThis();
2023-04-04 07:29:51 +00:00
jest.spyOn(controller, 'getMailStates').mockReturnThis();
2023-03-08 11:06:31 +00:00
jest.spyOn(controller.vnApp, 'showSuccess');
controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())};
controller.worker = {id: 1};
controller.worker = {user: {emailUser: {email: 'employee@verdnatura.es'}}};
controller.date = today;
controller.weekNumber = 1;
2024-06-06 15:26:23 +00:00
$httpBackend.expect('POST', 'WorkerTimeControls/weekly-hour-record-email').respond();
2023-03-08 11:06:31 +00:00
controller.resendEmail();
$httpBackend.flush();
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
});
});
describe('getMailStates() ', () => {
it(`should make a query an then call showSuccess method`, () => {
const today = Date.vnNew();
jest.spyOn(controller, 'repaint').mockReturnThis();
controller.$params = {id: 1};
$httpBackend.expect('GET', `WorkerTimeControls/1/getMailStates?month=1&year=2001`).respond();
controller.getMailStates(today);
$httpBackend.flush();
expect(controller.repaint).toHaveBeenCalled();
});
});
2019-05-17 11:27:51 +00:00
});
});