import './index'; describe('vnInvoiceOutDescriptorMenu', () => { let controller; let $httpBackend; let $httpParamSerializer; const invoiceOut = { id: 1, client: {id: 1101} }; beforeEach(ngModule('invoiceOut')); beforeEach(inject(($componentController, _$httpParamSerializer_, _$httpBackend_) => { $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; controller = $componentController('vnInvoiceOutDescriptorMenu', {$element: null}); })); describe('createPdfInvoice()', () => { it('should make a query to the createPdf() endpoint and show a success snackbar', () => { jest.spyOn(controller.vnApp, 'showSuccess'); controller.invoiceOut = invoiceOut; $httpBackend.whenGET(`InvoiceOuts/${invoiceOut.id}`).respond(); $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/createPdf`).respond(); controller.createPdfInvoice(); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); }); describe('showCsvInvoice()', () => { it('should make a query to the csv invoice download endpoint and show a message snackbar', () => { jest.spyOn(window, 'open').mockReturnThis(); controller.invoiceOut = invoiceOut; const expectedParams = { invoiceId: invoiceOut.id, recipientId: invoiceOut.client.id }; const serializedParams = $httpParamSerializer(expectedParams); const expectedPath = `api/csv/invoice/download?${serializedParams}`; controller.showCsvInvoice(); expect(window.open).toHaveBeenCalledWith(expectedPath); }); }); describe('deleteInvoiceOut()', () => { it(`should make a query and call showSuccess()`, () => { controller.invoiceOut = invoiceOut; controller.$state.reload = jest.fn(); jest.spyOn(controller.vnApp, 'showSuccess'); $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/delete`).respond(); controller.deleteInvoiceOut(); $httpBackend.flush(); expect(controller.$state.reload).toHaveBeenCalled(); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); it(`should make a query and call showSuccess() after state.go if the state wasn't in invoiceOut module`, () => { controller.invoiceOut = invoiceOut; jest.spyOn(controller.$state, 'go').mockReturnValue('ok'); jest.spyOn(controller.vnApp, 'showSuccess'); controller.$state.current.name = 'invoiceOut.card.something'; $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/delete`).respond(); controller.deleteInvoiceOut(); $httpBackend.flush(); expect(controller.$state.go).toHaveBeenCalledWith('invoiceOut.index'); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); }); describe('sendPdfInvoice()', () => { it('should make a query to the email invoice endpoint and show a message snackbar', () => { jest.spyOn(controller.vnApp, 'showMessage'); controller.invoiceOut = invoiceOut; const $data = {email: 'brucebanner@gothamcity.com'}; const expectedParams = { invoiceId: invoiceOut.id, recipient: $data.email, recipientId: invoiceOut.client.id }; const serializedParams = $httpParamSerializer(expectedParams); $httpBackend.expectGET(`email/invoice?${serializedParams}`).respond(); controller.sendPdfInvoice($data); $httpBackend.flush(); expect(controller.vnApp.showMessage).toHaveBeenCalled(); }); }); describe('sendCsvInvoice()', () => { it('should make a query to the csv invoice send endpoint and show a message snackbar', () => { jest.spyOn(controller.vnApp, 'showMessage'); controller.invoiceOut = invoiceOut; const $data = {email: 'brucebanner@gothamcity.com'}; const expectedParams = { invoiceId: invoiceOut.id, recipient: $data.email, recipientId: invoiceOut.client.id }; const serializedParams = $httpParamSerializer(expectedParams); $httpBackend.expectGET(`csv/invoice/send?${serializedParams}`).respond(); controller.sendCsvInvoice($data); $httpBackend.flush(); expect(controller.vnApp.showMessage).toHaveBeenCalled(); }); }); // #4084 review with Juan xdescribe('refundInvoiceOut()', () => { it('should make a query and go to ticket.card.sale', () => { controller.$state.go = jest.fn(); const invoiceOut = { id: 1, ref: 'T1111111' }; controller.invoiceOut = invoiceOut; const tickets = [{id: 1}]; const sales = [{id: 1}]; const services = [{id: 2}]; $httpBackend.expectGET(`Tickets`).respond(tickets); $httpBackend.expectGET(`Sales`).respond(sales); $httpBackend.expectGET(`TicketServices`).respond(services); const expectedParams = { sales: sales, services: services }; $httpBackend.expectPOST(`Sales/refund`, expectedParams).respond(); controller.refundInvoiceOut(); $httpBackend.flush(); expect(controller.$state.go).toHaveBeenCalledWith('ticket.card.sale', {id: undefined}); }); }); });