157 lines
5.6 KiB
JavaScript
157 lines
5.6 KiB
JavaScript
import './index.js';
|
|
|
|
describe('Component vnWorkerTimeControl', () => {
|
|
let $httpBackend;
|
|
let $scope;
|
|
let $element;
|
|
let controller;
|
|
|
|
beforeEach(ngModule('worker'));
|
|
|
|
beforeEach(inject(($componentController, $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 = Date.vnNew();
|
|
jest.spyOn(controller, 'fetchHours').mockReturnThis();
|
|
|
|
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 = Date.vnNew();
|
|
jest.spyOn(controller, 'fetchHours').mockReturnThis();
|
|
|
|
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 = Date.vnNew();
|
|
jest.spyOn(controller, 'fetchHours').mockReturnThis();
|
|
|
|
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');
|
|
});
|
|
});
|
|
|
|
describe('save() ', () => {
|
|
it(`should make a query an then call to the fetchHours() method`, () => {
|
|
controller.fetchHours = jest.fn();
|
|
controller.selectedRow = {id: 1, timed: Date.vnNew(), 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 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());
|
|
});
|
|
});
|
|
});
|
|
});
|