import './index.js'; describe('Ticket', () => { describe('Component vnTicketBasicDataStepOne', () => { let $state; let controller; let $httpBackend; beforeEach(angular.mock.module('ticket', $translateProvider => { $translateProvider.translations('en', {}); })); beforeEach(angular.mock.inject(($componentController, _$state_, _$httpBackend_) => { $state = _$state_; $httpBackend = _$httpBackend_; controller = $componentController('vnTicketBasicDataStepOne', {$state}); controller.ticket = { addressFk: 121, agencyModeFk: 7, warehouseFk: 1 }; })); describe('ticket() setter', () => { it('should set ticket property and call onChangeClient() method', () => { spyOn(controller, 'onChangeClient'); controller.ticket = {id: 1, clientFk: 101}; expect(controller.onChangeClient).toHaveBeenCalledWith(101); }); it(`should not call onChangeClient() method as the ticket doesn't have an ID`, () => { spyOn(controller, 'onChangeClient'); controller.ticket = {}; expect(controller.onChangeClient).not.toHaveBeenCalledWith(); }); }); describe('clientId() setter', () => { it('should set clientId property and call onChangeClient() method ', () => { spyOn(controller, 'onChangeClient'); controller.ticket = {id: 1, clientId: 101}; controller.clientId = 102; expect(controller.onChangeClient).toHaveBeenCalledWith(102); }); }); describe('shipped() setter', () => { it('should set shipped property and call getLanded() method ', () => { spyOn(controller, 'getLanded'); const shipped = new Date(); const spectedResult = { shipped: shipped, addressFk: 121, agencyModeFk: 7, warehouseFk: 1 }; controller.shipped = shipped; expect(controller.getLanded).toHaveBeenCalledWith(spectedResult); }); }); describe('landed() setter', () => { it('should set shipped property and call getShipped() method ', () => { spyOn(controller, 'getShipped'); const landed = new Date(); const spectedResult = { landed: landed, addressFk: 121, agencyModeFk: 7, warehouseFk: 1 }; controller.landed = landed; expect(controller.getShipped).toHaveBeenCalledWith(spectedResult); }); }); describe('agencyModeId() setter', () => { it('should set agencyModeId property and call onChangeAgencyMode() method', () => { spyOn(controller, 'getShipped'); const landed = new Date(); const agencyModeId = 8; const spectedResult = { landed: landed, addressFk: 121, agencyModeFk: agencyModeId, warehouseFk: 1 }; controller.ticket.landed = landed; controller.agencyModeId = 8; expect(controller.getShipped).toHaveBeenCalledWith(spectedResult); }); it('should do nothing if attempting to set the same agencyMode id', () => { spyOn(controller, 'getShipped'); const landed = new Date(); const agencyModeId = 7; const spectedResult = { landed: landed, addressFk: 121, agencyModeFk: agencyModeId, warehouseFk: 1 }; controller.ticket.landed = landed; controller.agencyModeId = 7; expect(controller.getShipped).not.toHaveBeenCalledWith(spectedResult); }); }); describe('zoneId() setter', () => { it('should set zoneId property and call onChangeZone() method ', () => { const zoneId = 5; spyOn(controller, 'onChangeZone'); controller.ticket = {id: 1}; controller.zoneId = 5; expect(controller.onChangeZone).toHaveBeenCalledWith(zoneId); }); it('should do nothing if attempting to set the same zone id', () => { const zoneId = 5; spyOn(controller, 'onChangeZone'); controller.ticket = {id: 1, zoneFk: zoneId}; controller.zoneId = zoneId; expect(controller.onChangeZone).not.toHaveBeenCalledWith(zoneId); }); }); describe('onChangeClient()', () => { it('should return a list of addresses from choosed client', async() => { const clientId = 102; let filter = { include: [ { relation: 'province', scope: { fields: ['name'] } }, { relation: 'agencyMode', scope: { fields: ['name'] } } ] }; filter = encodeURIComponent(JSON.stringify(filter)); $httpBackend.when('GET', `/api/Clients/${clientId}/addresses?filter=${filter}`).respond(200); $httpBackend.expect('GET', `/api/Clients/${clientId}/addresses?filter=${filter}`); controller.onChangeClient(clientId); $httpBackend.flush(); }); }); describe('onChangeZone()', () => { it('should return an available zone', async() => { const zoneId = 5; const landed = new Date(); controller._ticket = { id: 1, landed: landed, addressFk: 121, warehouseFk: 1 }; $httpBackend.when('GET', `/api/Zones/${zoneId}`).respond(200); $httpBackend.expect('GET', `/api/Zones/${zoneId}`); controller.onChangeZone(zoneId); $httpBackend.flush(); }); }); describe('isFormInvalid()', () => { it('should check if all form fields are valid', () => { controller.ticket = { clientFk: 1, addressFk: 121, zoneFk: 3, agencyModeFk: 1, companyFk: 442, warehouseFk: 1, shipped: new Date(), landed: new Date() }; expect(controller.isFormInvalid()).toBeFalsy(); }); }); describe('onStepChange()', () => { it('should call onStepChange method and return a NO_AGENCY_AVAILABLE signal error', async() => { let landed = new Date(); landed.setHours(0, 0, 0, 0); controller._ticket = { id: 1, clientFk: 1, addressFk: 121, zoneFk: 3, agencyModeFk: 1, companyFk: 442, warehouseFk: 1, shipped: new Date(), landed: landed }; let response = {error: new Error('NO_AGENCY_AVAILABLE')}; $httpBackend.whenPOST(`/api/tickets/1/priceDifference`).respond(400, response); $httpBackend.expectPOST(`/api/tickets/1/priceDifference`); controller.onStepChange(); $httpBackend.flush(); }); }); }); });