From 2f87ac8c96378e89ae0844268f1dbe6d01962db2 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 3 Mar 2022 14:42:12 +0100 Subject: [PATCH] add test --- .../methods/agency-term/createInvoiceIn.js | 4 +- .../agency-term/specs/createInvoiceIn.spec.js | 61 ++++++++++ .../methods/agency-term/specs/filter.spec.js | 26 +++++ .../agency-term/createInvoiceIn/index.js | 4 +- .../agency-term/createInvoiceIn/index.spec.js | 109 ++++++++++++------ .../route/front/agency-term/index/index.js | 5 +- .../front/agency-term/index/index.spec.js | 96 ++++++++++++--- modules/route/front/agency-term/locale/es.yml | 3 +- modules/route/front/routes.json | 2 +- 9 files changed, 247 insertions(+), 63 deletions(-) create mode 100644 modules/route/back/methods/agency-term/specs/createInvoiceIn.spec.js create mode 100644 modules/route/back/methods/agency-term/specs/filter.spec.js diff --git a/modules/route/back/methods/agency-term/createInvoiceIn.js b/modules/route/back/methods/agency-term/createInvoiceIn.js index 82e4437c0..c686f3ad1 100644 --- a/modules/route/back/methods/agency-term/createInvoiceIn.js +++ b/modules/route/back/methods/agency-term/createInvoiceIn.js @@ -1,6 +1,6 @@ module.exports = Self => { Self.remoteMethod('createInvoiceIn', { - description: 'create a invoce in from a agency terms', + description: 'create a invoce in from one or more agency terms', accessType: 'WRITE', accepts: [{ arg: 'rows', @@ -12,7 +12,7 @@ module.exports = Self => { arg: 'dms', type: ['object'], required: true, - description: 'The dms files' + description: 'the dms file' }], returns: { }, diff --git a/modules/route/back/methods/agency-term/specs/createInvoiceIn.spec.js b/modules/route/back/methods/agency-term/specs/createInvoiceIn.spec.js new file mode 100644 index 000000000..40a2e7b07 --- /dev/null +++ b/modules/route/back/methods/agency-term/specs/createInvoiceIn.spec.js @@ -0,0 +1,61 @@ +const models = require('vn-loopback/server/server').models; + +describe('AgencyTerm createInvoiceIn()', () => { + const rows = [ + { + routeFk: 2, + supplierFk: 1, + created: '2022-03-02T23:00:00.000Z', + totalPrice: 165 + } + ]; + const dms = [ + { + id: 7, + file: '7.pdf', + contentType: 'application/pdf', + reference: '1', + description: 'Plants SL', + hasFile: false, + companyFk: 442, + dmsTypeFk: 1, + warehouseFk: 1, + workerFk: 9 + } + ]; + const userId = 1; + const activeCtx = { + accessToken: {userId: userId}, + }; + const ctx = {req: activeCtx}; + + it('should make a global invoicing', async() => { + const tx = await models.AgencyTerm.beginTransaction({}); + const options = {transaction: tx}; + + try { + const invoiceInId = 10; + const invoiceInDueDayId = 11; + const invoiceInTaxId = 12; + + const oldInvoiceIn = await models.AgencyTerm.findById(invoiceInId, null, options); + const oldInvoiceInDueDay = await models.AgencyTerm.findById(invoiceInDueDayId, null, options); + const oldInvoiceInTax = await models.AgencyTerm.findById(invoiceInTaxId, null, options); + + const [newInvoiceIn] = await models.Ticket.rawSql('SELECT MAX(id) id FROM invoiceIn', null, options); + // const [newInvoiceInDueDay] = await models.AgencyTerm.rawSql('SELECT MAX(id) id FROM invoiceInDueDay', null, options); + // const [newInvoiceInTax] = await models.AgencyTerm.rawSql('SELECT MAX(id) id FROM invoiceInTax', null, options); + + await models.AgencyTerm.createInvoiceIn(rows, dms, options); + + // expect(newInvoiceIn.id).toEqual(oldInvoiceIn.id + 1); + // expect(newInvoiceInDueDay.id).toEqual(oldInvoiceInDueDay.id + 1); + // expect(newInvoiceInTax.id).toEqual(oldInvoiceInTax.id + 1); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/route/back/methods/agency-term/specs/filter.spec.js b/modules/route/back/methods/agency-term/specs/filter.spec.js new file mode 100644 index 000000000..23671cef3 --- /dev/null +++ b/modules/route/back/methods/agency-term/specs/filter.spec.js @@ -0,0 +1,26 @@ +const models = require('vn-loopback/server/server').models; + +xdescribe('AgencyTerm filter()', () => { + const authUserId = 9; + + it('should all return the tickets matching the filter', async() => { + const tx = await models.AgencyTerm.beginTransaction({}); + + try { + const options = {transaction: tx}; + const filter = {}; + const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}}; + + const agencyTerms = await models.AgencyTerm.filter(ctx, null, options); + const firstAgencyTerm = agencyTerms[0]; + + expect(firstAgencyTerm.routeFk).toEqual(1); + expect(agencyTerms.length).toEqual(3); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/route/front/agency-term/createInvoiceIn/index.js b/modules/route/front/agency-term/createInvoiceIn/index.js index 3f3703a6c..9bfa30d51 100644 --- a/modules/route/front/agency-term/createInvoiceIn/index.js +++ b/modules/route/front/agency-term/createInvoiceIn/index.js @@ -10,7 +10,6 @@ class Controller extends Section { hasFile: false, hasFileAttached: false }; - this.addedDms; } get route() { @@ -83,7 +82,6 @@ class Controller extends Section { this.$http(options).then(res => { if (res) { let addedDms = res.data; - this.vnApp.showSuccess(this.$t('Data saved!')); this.$.watcher.updateOriginalData(); const params = { @@ -94,7 +92,7 @@ class Controller extends Section { this.$http.post('AgencyTerms/createInvoiceIn', params) .then(() => { this.$state.go('route.agencyTerm.index'); - this.vnApp.showSuccess(this.$t('InvoiceIn created')); + this.vnApp.showSuccess(this.$t('Data saved!')); }); } }); diff --git a/modules/route/front/agency-term/createInvoiceIn/index.spec.js b/modules/route/front/agency-term/createInvoiceIn/index.spec.js index fe302f7d2..1c64eee93 100644 --- a/modules/route/front/agency-term/createInvoiceIn/index.spec.js +++ b/modules/route/front/agency-term/createInvoiceIn/index.spec.js @@ -1,64 +1,53 @@ import './index'; +import watcher from 'core/mocks/watcher.js'; -describe('Client', () => { - describe('Component vnClientDmsCreate', () => { +describe('AgencyTerm', () => { + describe('Component vnAgencyTermCreateInvoiceIn', () => { let controller; let $scope; let $httpBackend; let $httpParamSerializer; - beforeEach(ngModule('client')); + beforeEach(ngModule('route')); beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; - const $element = angular.element(''); - controller = $componentController('vnClientDmsCreate', {$element, $scope}); - controller._client = {id: 1101, name: 'Bruce wayne'}; + const $element = angular.element(''); + controller = $componentController('vnAgencyTermCreateInvoiceIn', {$element}); + controller._route = { + id: 1 + }; })); - describe('client() setter', () => { - it('should set the client data and then call setDefaultParams() and getAllowedContentTypes()', () => { + describe('$onChanges()', () => { + it('should update the params data when $params.q is defined', () => { + controller.$params = {q: '{"supplierName": "Plants SL","rows": null}'}; + + const params = {q: '{"supplierName": "Plants SL", "rows": null}'}; + const json = JSON.parse(params.q); + + controller.$onChanges(); + + expect(controller.params).toEqual(json); + }); + }); + + describe('route() setter', () => { + it('should set the ticket data and then call setDefaultParams() and getAllowedContentTypes()', () => { jest.spyOn(controller, 'setDefaultParams'); jest.spyOn(controller, 'getAllowedContentTypes'); - controller.client = { - id: 15, - name: 'Bruce wayne' + controller.route = { + id: 1 }; - expect(controller.client).toBeDefined(); + expect(controller.route).toBeDefined(); expect(controller.setDefaultParams).toHaveBeenCalledWith(); expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); }); }); - describe('setDefaultParams()', () => { - it('should perform a GET query and define the dms property on controller', () => { - const params = {filter: { - where: {code: 'paymentsLaw'} - }}; - let serializedParams = $httpParamSerializer(params); - $httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 12, code: 'paymentsLaw'}); - controller.setDefaultParams(); - $httpBackend.flush(); - - expect(controller.dms).toBeDefined(); - expect(controller.dms.reference).toEqual(1101); - expect(controller.dms.dmsTypeId).toEqual(12); - }); - }); - - describe('onFileChange()', () => { - it('should set dms hasFileAttached property to true if has any files', () => { - const files = [{id: 1, name: 'MyFile'}]; - controller.onFileChange(files); - $scope.$apply(); - - expect(controller.dms.hasFileAttached).toBeTruthy(); - }); - }); - describe('getAllowedContentTypes()', () => { it('should make an HTTP GET request to get the allowed content types', () => { const expectedResponse = ['image/png', 'image/jpg']; @@ -70,5 +59,49 @@ describe('Client', () => { expect(controller.allowedContentTypes).toEqual('image/png, image/jpg'); }); }); + + describe('setDefaultParams()', () => { + it('should perform a GET query and define the dms property on controller', () => { + const params2 = {filter: { + where: {code: 'invoiceIn'} + }}; + let serializedParams = $httpParamSerializer(params2); + $httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 1, code: 'invoiceIn'}); + controller.params = {supplierName: 'Plants SL'}; + controller.setDefaultParams(); + $httpBackend.flush(); + + expect(controller.dms).toBeDefined(); + expect(controller.dms.dmsTypeId).toEqual(1); + }); + }); + + describe('onSubmit()', () => { + it('should make an HTTP POST request to save the form data', () => { + controller.$.watcher = watcher; + + jest.spyOn(controller.$.watcher, 'updateOriginalData'); + const files = [{id: 1, name: 'MyFile'}]; + controller.dms = {files}; + const serializedParams = $httpParamSerializer(controller.dms); + const query = `dms/uploadFile?${serializedParams}`; + controller.params = {rows: null}; + + $httpBackend.expect('POST', query).respond({}); + $httpBackend.expect('POST', 'AgencyTerms/createInvoiceIn').respond({}); + controller.onSubmit(); + $httpBackend.flush(); + }); + }); + + describe('onFileChange()', () => { + it('should set dms hasFileAttached property to true if has any files', () => { + const files = [{id: 1, name: 'MyFile'}]; + controller.onFileChange(files); + $scope.$apply(); + + expect(controller.dms.hasFileAttached).toBeTruthy(); + }); + }); }); }); diff --git a/modules/route/front/agency-term/index/index.js b/modules/route/front/agency-term/index/index.js index c1089e1a7..89d71134b 100644 --- a/modules/route/front/agency-term/index/index.js +++ b/modules/route/front/agency-term/index/index.js @@ -99,10 +99,11 @@ class Controller extends Section { createInvoiceIn() { const rowsToCreateInvoiceIn = []; - const supplier = this.checked[0].supplierFk; + const supplierFk = this.checked[0].supplierFk; for (let agencyTerm of this.checked) { - if (supplier == agencyTerm.supplierFk) { + let hasSameSupplier = supplierFk == agencyTerm.supplierFk; + if (hasSameSupplier) { rowsToCreateInvoiceIn.push({ routeFk: agencyTerm.routeFk, supplierFk: agencyTerm.supplierFk, diff --git a/modules/route/front/agency-term/index/index.spec.js b/modules/route/front/agency-term/index/index.spec.js index 18abde581..18a49284a 100644 --- a/modules/route/front/agency-term/index/index.spec.js +++ b/modules/route/front/agency-term/index/index.spec.js @@ -1,29 +1,93 @@ import './index.js'; +import crudModel from 'core/mocks/crud-model'; -describe('Item', () => { - describe('Component vnItemIndex', () => { +describe('AgencyTerm', () => { + describe('Component vnAgencyTermIndex', () => { let controller; - let $httpBackend; - let $scope; + let $window; - beforeEach(ngModule('item')); + beforeEach(ngModule('route')); - beforeEach(inject(($componentController, _$httpBackend_, $rootScope) => { - $httpBackend = _$httpBackend_; - $scope = $rootScope.$new(); - const $element = angular.element(''); - controller = $componentController('vnItemIndex', {$element, $scope}); + beforeEach(inject(($componentController, _$window_) => { + $window = _$window_; + const $element = angular.element(''); + controller = $componentController('vnAgencyTermIndex', {$element}); + controller.$.model = crudModel; + controller.$.model.data = [ + {supplierFk: 1, totalPrice: null}, + {supplierFk: 1}, + {supplierFk: 2} + ]; })); - describe('onCloneAccept()', () => { - it('should perform a post query and then call go() then update itemSelected in the controller', () => { + describe('checked() getter', () => { + it('should return the checked lines', () => { + const data = controller.$.model.data; + data[0].checked = true; + + const checkedRows = controller.checked; + + const firstCheckedRow = checkedRows[0]; + + expect(firstCheckedRow.supplierFk).toEqual(1); + }); + }); + + describe('totalCheked() getter', () => { + it('should return the total checked lines', () => { + const data = controller.$.model.data; + data[0].checked = true; + + const checkedRows = controller.totalChecked; + + expect(checkedRows).toEqual(1); + }); + }); + + describe('preview()', () => { + it('should show the dialog summary', () => { + controller.$.summary = {show: () => {}}; + jest.spyOn(controller.$.summary, 'show'); + + let event = new MouseEvent('click', { + view: $window, + bubbles: true, + cancelable: true + }); + const route = {id: 1}; + + controller.preview(event, route); + + expect(controller.$.summary.show).toHaveBeenCalledWith(); + }); + }); + + describe('createInvoiceIn()', () => { + it('should throw an error if are checked more than one autonomous', () => { + jest.spyOn(controller.vnApp, 'showError'); + const data = controller.$.model.data; + data[0].checked = true; + data[2].checked = true; + + controller.createInvoiceIn(); + + expect(controller.vnApp.showError).toHaveBeenCalled(); + }); + + it('should call the function go() on $state to go to the file managment', () => { jest.spyOn(controller.$state, 'go'); + const data = controller.$.model.data; + data[0].checked = true; - $httpBackend.expectRoute('POST', `Items/:id/clone`).respond({id: 99}); - controller.onCloneAccept(1); - $httpBackend.flush(); + controller.createInvoiceIn(); - expect(controller.$state.go).toHaveBeenCalledWith('item.card.tags', {id: 99}); + delete data[0].checked; + const params = JSON.stringify({ + supplierName: data[0].supplierName, + rows: [data[0]] + }); + + expect(controller.$state.go).toHaveBeenCalledWith('route.agencyTerm.createInvoiceIn', {q: params}); }); }); }); diff --git a/modules/route/front/agency-term/locale/es.yml b/modules/route/front/agency-term/locale/es.yml index 0e766f5aa..98301ea6e 100644 --- a/modules/route/front/agency-term/locale/es.yml +++ b/modules/route/front/agency-term/locale/es.yml @@ -2,4 +2,5 @@ picture: Foto Buy requests: Peticiones de compra Agency route: Agencia ruta Agency Agreement: Agencia acuerdo -Autonomous: Autónomo \ No newline at end of file +Autonomous: Autónomos +Two autonomous cannot be counted at the same time: Dos autonónomos no pueden ser contabilizados al mismo tiempo \ No newline at end of file diff --git a/modules/route/front/routes.json b/modules/route/front/routes.json index 1e7d94765..7e0273441 100644 --- a/modules/route/front/routes.json +++ b/modules/route/front/routes.json @@ -53,7 +53,7 @@ "url": "/createInvoiceIn?q", "state": "route.agencyTerm.createInvoiceIn", "component": "vn-agency-term-create-invoice-in", - "description": "Create invoiceIn", + "description": "File management", "params": { "route": "$ctrl.route" },