import './index.js'; describe('Travel Component vnTravelIndex', () => { let $componentController; let controller; let $window; let travels = [{ id: 1, warehouseInFk: 1, totalEntries: 3, isDelivered: false }, { id: 2, warehouseInFk: 1, total: 4, isDelivered: true }, { id: 3, warehouseInFk: 1, total: 2, isDelivered: true }]; beforeEach(angular.mock.module('travel', $translateProvider => { $translateProvider.translations('en', {}); })); beforeEach(angular.mock.inject(_$componentController_ => { $componentController = _$componentController_; const $element = angular.element(''); controller = $componentController('vnTravelIndex', {$element}); controller.$.summary = {show: jasmine.createSpy('show')}; })); describe('preview()', () => { it('should show the dialog summary', () => { let event = new MouseEvent('click', { view: $window, bubbles: true, cancelable: true }); controller.preview(event, travels[0]); expect(controller.$.summary.show).toHaveBeenCalledWith(); }); }); describe('getScopeDates()', () => { it('should return a range of dates', () => { let days = 2; // never put 1 or anything higher than 2 let result = controller.getScopeDates(days); let from = new Date(result.shippedFrom).getTime(); let to = new Date(result.shippedTo).getTime(); let range = to - from; const dayInMilliseconds = 24 * 60 * 60 * 1000; let millisecondsPerAddedDay = dayInMilliseconds - 1; expect(range - dayInMilliseconds).toEqual(dayInMilliseconds + millisecondsPerAddedDay); }); }); describe('onCloneAccept()', () => { it('should do nothing if response is not accept', () => { jest.spyOn(controller.$state, 'go'); let response = 'ERROR!'; controller.travelSelected = 'check me'; controller.onCloneAccept(response); expect(controller.$state.go).not.toHaveBeenCalledWith(); expect(controller.travelSelected).toEqual('check me'); }); it('should do nothing if response is accept but travelSelected is not defined in the controller', () => { jest.spyOn(controller.$state, 'go'); let response = 'accept'; controller.travelSelected = undefined; controller.onCloneAccept(response); expect(controller.$state.go).not.toHaveBeenCalledWith(); expect(controller.travelSelected).toBeUndefined(); }); it('should call go() then update travelSelected in the controller', () => { jest.spyOn(controller.$state, 'go'); let response = 'accept'; controller.travelSelected = { ref: 1, agencyFk: 1}; const travel = { ref: controller.travelSelected.ref, agencyModeFk: controller.travelSelected.agencyFk }; const queryParams = JSON.stringify(travel); controller.onCloneAccept(response); expect(controller.$state.go).toHaveBeenCalledWith('travel.create', {q: queryParams}); expect(controller.travelSelected).toBeNull(); }); }); });