From 3dbd30421730f7985c88c5d1508b8bac108c26d6 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 4 Oct 2022 09:21:24 +0200 Subject: [PATCH] Updated unit tests --- front/core/services/report.js | 3 +- modules/client/front/sample/create/index.html | 5 +- modules/client/front/sample/create/index.js | 42 +++++--- .../client/front/sample/create/index.spec.js | 95 ++++++++++--------- modules/ticket/front/index/index.spec.js | 4 +- 5 files changed, 87 insertions(+), 62 deletions(-) diff --git a/front/core/services/report.js b/front/core/services/report.js index 45024d9e0..d6eb28ea4 100644 --- a/front/core/services/report.js +++ b/front/core/services/report.js @@ -18,7 +18,8 @@ class Report { access_token: this.vnToken.token }, params); const serializedParams = this.$httpParamSerializer(params); - window.open(`api/${path}?${serializedParams}`); + const query = serializedParams ? `?${serializedParams}` : ''; + window.open(`api/${path}${query}`); } } Report.$inject = ['$httpParamSerializer', 'vnToken']; diff --git a/modules/client/front/sample/create/index.html b/modules/client/front/sample/create/index.html index 5df2b29ef..725d0cd0f 100644 --- a/modules/client/front/sample/create/index.html +++ b/modules/client/front/sample/create/index.html @@ -41,8 +41,7 @@ model="ClientSample.typeFk" data="samplesVisible" show-field="description" - label="Sample" - required="true"> + label="Sample"> @@ -80,11 +79,9 @@ diff --git a/modules/client/front/sample/create/index.js b/modules/client/front/sample/create/index.js index 4e6256ee6..13ef875da 100644 --- a/modules/client/front/sample/create/index.js +++ b/modules/client/front/sample/create/index.js @@ -37,27 +37,38 @@ class Controller extends Section { onSubmit() { this.$.watcher.check(); + + const validationMessage = this.validate(); + if (validationMessage) + return this.vnApp.showError(this.$t(validationMessage)); + this.$.watcher.realSubmit().then(() => this.send()); } - validateParams(params) { + validate() { const sampleType = this.$.sampleType.selection; - if (!params.recipient) - return this.vnApp.showError(this.$t('Email cannot be blank')); + if (!this.clientSample.recipient) + return 'Email cannot be blank'; if (!sampleType) - return this.vnApp.showError(this.$t('Choose a sample')); + return 'Choose a sample'; if (sampleType.hasCompany && !this.clientSample.companyFk) - return this.vnApp.showError(this.$t('Choose a company')); + return 'Choose a company'; + + if (sampleType.datepickerEnabled && !this.clientSample.from) + return 'Choose a date'; + + return; + } + + setParams(params) { + const sampleType = this.$.sampleType.selection; if (sampleType.hasCompany) params.companyId = this.clientSample.companyFk; - if (sampleType.datepickerEnabled && !this.clientSample.from) - return this.vnApp.showError(this.$t('Choose a date')); - if (sampleType.datepickerEnabled) params.from = this.clientSample.from; } @@ -66,11 +77,14 @@ class Controller extends Section { const sampleType = this.$.sampleType.selection; const params = { - recipientId: this.$params.id, - recipient: this.clientSample.recipient + recipientId: this.$params.id }; - this.validateParams(params); + const validationMessage = this.validate(); + if (validationMessage) + return this.vnApp.showError(this.$t(validationMessage)); + + this.setParams(params); const path = `${sampleType.model}/${this.$params.id}/${sampleType.code}-html`; this.$http.get(path, {params}) @@ -94,7 +108,11 @@ class Controller extends Section { replyTo: this.clientSample.replyTo }; - this.validateParams(params); + const validationMessage = this.validate(); + if (validationMessage) + return this.vnApp.showError(this.$t(validationMessage)); + + this.setParams(params); const path = `${sampleType.model}/${this.$params.id}/${sampleType.code}-email`; this.vnEmail.send(path, params) diff --git a/modules/client/front/sample/create/index.spec.js b/modules/client/front/sample/create/index.spec.js index 6e9da54ef..8e33a1075 100644 --- a/modules/client/front/sample/create/index.spec.js +++ b/modules/client/front/sample/create/index.spec.js @@ -40,7 +40,7 @@ describe('Client', () => { $httpParamSerializer = _$httpParamSerializer_; $element = angular.element(''); controller = $componentController('vnClientSampleCreate', {$element, $scope}); - controller.client = {id: 1101}; + controller._client = {id: 1101}; const element = document.createElement('div'); document.body.querySelector = () => { return { @@ -49,11 +49,23 @@ describe('Client', () => { } }; }; + // $httpBackend.expectGET('EmailUsers?filter=%7B%22where%22:%7B%7D%7D').respond(); })); describe('onSubmit()', () => { it(`should call send() method`, () => { - jest.spyOn(controller, 'send'); + controller.send = jest.fn(); + + controller.$.sampleType.selection = { + hasCompany: false, + code: 'MyReport', + model: 'Clients' + }; + + controller.clientSample = { + recipient: 'email@email' + }; + controller.onSubmit(); expect(controller.send).toHaveBeenCalledWith(); @@ -73,7 +85,7 @@ describe('Client', () => { recipientId: 1101 }; - controller.send(false, () => {}); + controller.send(); expect(controller.$http.get).not.toHaveBeenCalled(); }); @@ -87,7 +99,7 @@ describe('Client', () => { recipient: 'client@email.com' }; - controller.send(false, () => {}); + controller.send(); expect(controller.$http.get).not.toHaveBeenCalled(); }); @@ -104,84 +116,81 @@ describe('Client', () => { recipient: 'client@email.com' }; - controller.send(false, () => {}); + controller.send(); expect(controller.$http.get).not.toHaveBeenCalled(); }); it(`should perform an HTTP query without passing companyFk param`, () => { + $state.go = jest.fn(); + controller.$.sampleType.selection = { hasCompany: false, - code: 'MyReport' + code: 'my-report', + model: 'Clients' }; controller.clientSample = { recipientId: 1101, recipient: 'client@email.com' }; - const expectedParams = { - recipientId: 1101, - recipient: 'client@email.com' - }; - const serializedParams = $httpParamSerializer(expectedParams); - $httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true); - controller.send(false, () => {}); + const expectedPath = `Clients/${controller.client.id}/my-report-email`; + $httpBackend.expect('POST', expectedPath).respond(true); + controller.send(); $httpBackend.flush(); }); it(`should perform an HTTP query passing companyFk param`, () => { + $state.go = jest.fn(); + controller.$.sampleType.selection = { hasCompany: true, - code: 'MyReport' + code: 'my-report', + model: 'Clients' }; controller.clientSample = { recipientId: 1101, recipient: 'client@email.com', companyFk: 442 }; - const expectedParams = { - recipientId: 1101, - recipient: 'client@email.com', - companyId: 442 - }; - const serializedParams = $httpParamSerializer(expectedParams); - $httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true); - controller.send(false, () => {}); + const expectedPath = `Clients/${controller.client.id}/my-report-email`; + $httpBackend.expect('POST', expectedPath).respond(true); + controller.send(); $httpBackend.flush(); }); }); - describe('showPreview()', () => { + describe('preview()', () => { it(`should open a sample preview`, () => { jest.spyOn(controller.$.showPreview, 'show'); - controller.send = (isPreview, cb) => { - cb({ - data: '
' - }); + controller.$.sampleType.selection = { + hasCompany: true, + code: 'my-report', + model: 'Clients' }; - controller.showPreview(); + controller.clientSample = { + recipientId: 1101, + recipient: 'client@email.com', + companyFk: 442 + }; + + const expectedParams = { + companyId: 442, + recipientId: 1101 + }; + const serializedParams = $httpParamSerializer(expectedParams); + + const expectedPath = `Clients/${controller.client.id}/my-report-html?${serializedParams}`; + $httpBackend.expect('GET', expectedPath).respond(true); + controller.preview(); + $httpBackend.flush(); expect(controller.$.showPreview.show).toHaveBeenCalledWith(); }); }); - describe('sendSample()', () => { - it(`should perform a query (GET) and call go() method`, () => { - jest.spyOn(controller.$state, 'go'); - - controller.send = (isPreview, cb) => { - cb({ - data: true - }); - }; - controller.sendSample(); - - expect(controller.$state.go).toHaveBeenCalledWith('client.card.sample.index'); - }); - }); - describe('getWorkerEmail()', () => { it(`should perform a query and then set the replyTo property to the clientSample object`, () => { const expectedEmail = 'batman@arkhamcity.com'; diff --git a/modules/ticket/front/index/index.spec.js b/modules/ticket/front/index/index.spec.js index 84473686c..03071654e 100644 --- a/modules/ticket/front/index/index.spec.js +++ b/modules/ticket/front/index/index.spec.js @@ -96,8 +96,8 @@ describe('Component vnTicketIndex', () => { controller.setDelivered(); $httpBackend.flush(); - expect($window.open).toHaveBeenCalledWith(`Tickets/${tickets[1].id}/delivery-note-pdf`); - expect($window.open).toHaveBeenCalledWith(`Tickets/${tickets[2].id}/delivery-note-pdf`); + expect($window.open).toHaveBeenCalledWith(`api/Tickets/${tickets[1].id}/delivery-note-pdf`); + expect($window.open).toHaveBeenCalledWith(`api/Tickets/${tickets[2].id}/delivery-note-pdf`); }); });