From af4e7be229754634c231ace8ae8d5d55762919e2 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 20 Jul 2022 13:41:33 +0200 Subject: [PATCH] feat: add frontTest --- modules/shelving/front/card/index.spec.js | 26 ++++ modules/shelving/front/create/index.js | 2 +- modules/shelving/front/create/index.spec.js | 123 +++--------------- .../shelving/front/descriptor/index.spec.js | 61 ++------- modules/shelving/front/index/index.spec.js | 39 ++++++ modules/shelving/front/main/index.spec.js | 19 +++ modules/shelving/front/summary/index.spec.js | 32 ----- 7 files changed, 113 insertions(+), 189 deletions(-) create mode 100644 modules/shelving/front/card/index.spec.js create mode 100644 modules/shelving/front/index/index.spec.js create mode 100644 modules/shelving/front/main/index.spec.js delete mode 100644 modules/shelving/front/summary/index.spec.js diff --git a/modules/shelving/front/card/index.spec.js b/modules/shelving/front/card/index.spec.js new file mode 100644 index 000000000..85b1bd269 --- /dev/null +++ b/modules/shelving/front/card/index.spec.js @@ -0,0 +1,26 @@ +import './index'; + +describe('component vnShelvingCard', () => { + let controller; + let $httpBackend; + const data = {id: 1, code: 'AAA'}; + + beforeEach(ngModule('shelving')); + + beforeEach(inject(($componentController, _$httpBackend_, $stateParams) => { + $httpBackend = _$httpBackend_; + + let $element = angular.element('
'); + controller = $componentController('vnShelvingCard', {$element}); + + $stateParams.id = data.id; + $httpBackend.whenRoute('GET', 'Shelvings/:id').respond(data); + })); + + it('should reload the controller data', () => { + controller.reload(); + $httpBackend.flush(); + + expect(controller.shelving).toEqual(data); + }); +}); diff --git a/modules/shelving/front/create/index.js b/modules/shelving/front/create/index.js index 22c28f496..bb0e441eb 100644 --- a/modules/shelving/front/create/index.js +++ b/modules/shelving/front/create/index.js @@ -4,7 +4,7 @@ import Section from 'salix/components/section'; export default class Controller extends Section { onSubmit() { return this.$.watcher.submit().then(res => - this.$state.go('shelving.card.summary', {id: res.data.id}) + this.$state.go('shelving.card.basicData', {id: res.data.id}) ); } } diff --git a/modules/shelving/front/create/index.spec.js b/modules/shelving/front/create/index.spec.js index 24fc80d21..1642afc52 100644 --- a/modules/shelving/front/create/index.spec.js +++ b/modules/shelving/front/create/index.spec.js @@ -1,121 +1,28 @@ import './index'; +import watcher from 'core/mocks/watcher'; -describe('Client', () => { - describe('Component vnClientCreate', () => { - let $scope; - let $state; +fdescribe('Shelving', () => { + describe('Component vnShelvingCreate', () => { let controller; + let $element; - beforeEach(ngModule('client')); + beforeEach(ngModule('shelving')); - beforeEach(inject(($componentController, $rootScope, _$state_) => { - $scope = $rootScope.$new(); - $state = _$state_; - $scope.watcher = { - submit: () => { - return { - then: callback => { - callback({data: {id: '1234'}}); - } - }; - } - }; - const $element = angular.element(''); - controller = $componentController('vnClientCreate', {$element, $scope}); + beforeEach(inject(($componentController, $rootScope) => { + const $scope = $rootScope.$new(); + $scope.watcher = watcher; + $element = angular.element(''); + controller = $componentController('vnShelvingCreate', {$element, $scope}); + controller.$params = {}; })); - it('should define and set scope, state and client properties', () => { - expect(controller.$).toBe($scope); - expect(controller.$state).toBe($state); - expect(controller.client.active).toBe(true); - }); - describe('onSubmit()', () => { - it(`should call submit() on the watcher then expect a callback`, () => { - jest.spyOn($state, 'go'); + it(`should redirect to basic data by calling the $state.go function`, () => { + jest.spyOn(controller.$state, 'go'); + controller.onSubmit(); - expect(controller.$state.go).toHaveBeenCalledWith('client.card.basicData', {id: '1234'}); - }); - }); - - describe('province() setter', () => { - it(`should set countryFk property`, () => { - controller.client.countryFk = null; - controller.province = { - id: 1, - name: 'New york', - country: { - id: 2, - name: 'USA' - } - }; - - expect(controller.client.countryFk).toEqual(2); - }); - }); - - describe('town() setter', () => { - it(`should set provinceFk property`, () => { - controller.town = { - provinceFk: 1, - code: 46001, - province: { - id: 1, - name: 'New york', - country: { - id: 2, - name: 'USA' - } - }, - postcodes: [] - }; - - expect(controller.client.provinceFk).toEqual(1); - }); - - it(`should set provinceFk property and fill the postalCode if there's just one`, () => { - controller.town = { - provinceFk: 1, - code: 46001, - province: { - id: 1, - name: 'New york', - country: { - id: 2, - name: 'USA' - } - }, - postcodes: [{code: '46001'}] - }; - - expect(controller.client.provinceFk).toEqual(1); - expect(controller.client.postcode).toEqual('46001'); - }); - }); - - describe('postcode() setter', () => { - it(`should set the town, provinceFk and contryFk properties`, () => { - controller.postcode = { - townFk: 1, - code: 46001, - town: { - id: 1, - name: 'New York', - province: { - id: 1, - name: 'New york', - country: { - id: 2, - name: 'USA' - } - } - } - }; - - expect(controller.client.city).toEqual('New York'); - expect(controller.client.provinceFk).toEqual(1); - expect(controller.client.countryFk).toEqual(2); + expect(controller.$state.go).toHaveBeenCalledWith('shelving.card.basicData', {id: 1234}); }); }); }); diff --git a/modules/shelving/front/descriptor/index.spec.js b/modules/shelving/front/descriptor/index.spec.js index 8926fa4d1..3ee33580b 100644 --- a/modules/shelving/front/descriptor/index.spec.js +++ b/modules/shelving/front/descriptor/index.spec.js @@ -1,64 +1,29 @@ import './index.js'; -describe('Supplier Component vnSupplierDescriptor', () => { +describe('component vnShelvingDescriptor', () => { let $httpBackend; let controller; - let $httpParamSerializer; - const supplier = {id: 1}; - beforeEach(ngModule('supplier')); + const shelving = {id: 1, code: 'AA6'}; + + beforeEach(ngModule('shelving')); beforeEach(inject(($componentController, _$httpBackend_, _$httpParamSerializer_) => { $httpBackend = _$httpBackend_; - $httpParamSerializer = _$httpParamSerializer_; - controller = $componentController('vnSupplierDescriptor', {$element: null}, {supplier}); + controller = $componentController('vnShelvingDescriptor', {$element: null}, {shelving}); + jest.spyOn(controller.vnApp, 'showSuccess'); })); - describe('loadData()', () => { - it('should perform ask for the supplier', () => { - const filter = { - fields: [ - 'id', - 'name', - 'nickname', - 'nif', - 'payMethodFk', - 'payDemFk', - 'payDay', - 'isActive', - 'isSerious', - 'account' - ], - include: [ - { - relation: 'payMethod', - scope: { - fields: ['id', 'name'] - } - }, - { - relation: 'payDem', - scope: { - fields: ['id', 'payDem'] - } - }, - { - relation: 'client', - scope: { - fields: ['id', 'fi'] - } - } - ] - }; - const serializedParams = $httpParamSerializer({filter}); - let query = `Suppliers/${controller.supplier.id}?${serializedParams}`; - jest.spyOn(controller, 'getData'); + describe('onDelete()', () => { + it('should delete entity and go to index', () => { + controller.$state.go = jest.fn(); - $httpBackend.expect('GET', query).respond({id: 1}); - controller.loadData(); + $httpBackend.expectDELETE('Shelvings/1').respond(); + controller.onDelete(); $httpBackend.flush(); - expect(controller.getData).toHaveBeenCalledTimes(1); + expect(controller.$state.go).toHaveBeenCalledWith('shelving.index'); + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); }); }); diff --git a/modules/shelving/front/index/index.spec.js b/modules/shelving/front/index/index.spec.js new file mode 100644 index 000000000..aad79fb0e --- /dev/null +++ b/modules/shelving/front/index/index.spec.js @@ -0,0 +1,39 @@ +import './index.js'; +describe('Component vnShelvingIndex', () => { + let controller; + let $window; + let shelvings = [{ + id: 1, + code: 'AAA' + }, { + id: 2, + code: 'AA1' + }, { + id: 3, + code: 'AA2' + }]; + + beforeEach(ngModule('shelving')); + + beforeEach(inject(($componentController, _$window_) => { + $window = _$window_; + const $element = angular.element(''); + controller = $componentController('vnShelvingIndex', {$element}); + })); + + 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 + }); + controller.preview(event, shelvings[0]); + + expect(controller.$.summary.show).toHaveBeenCalledWith(); + }); + }); +}); diff --git a/modules/shelving/front/main/index.spec.js b/modules/shelving/front/main/index.spec.js new file mode 100644 index 000000000..dcfa912bc --- /dev/null +++ b/modules/shelving/front/main/index.spec.js @@ -0,0 +1,19 @@ +import './index'; + +describe('component vnShelving', () => { + let controller; + + beforeEach(ngModule('shelving')); + + beforeEach(inject($componentController => { + controller = $componentController('vnShelving', {$element: null}); + })); + + describe('exprBuilder()', () => { + it('should search by code', () => { + let expr = controller.exprBuilder('search', 'UXN'); + + expect(expr).toEqual({code: {like: '%UXN%'}},); + }); + }); +}); diff --git a/modules/shelving/front/summary/index.spec.js b/modules/shelving/front/summary/index.spec.js deleted file mode 100644 index aa44cd14f..000000000 --- a/modules/shelving/front/summary/index.spec.js +++ /dev/null @@ -1,32 +0,0 @@ -import './index'; - -describe('Supplier', () => { - describe('Component vnSupplierSummary', () => { - let controller; - let $httpBackend; - let $scope; - - beforeEach(ngModule('supplier')); - - beforeEach(inject(($componentController, _$httpBackend_, $rootScope) => { - $httpBackend = _$httpBackend_; - $scope = $rootScope.$new(); - const $element = angular.element(''); - controller = $componentController('vnSupplierSummary', {$element, $scope}); - })); - - describe('getSummary()', () => { - it('should perform a get asking for the supplier data', () => { - controller.supplier = {id: 1}; - - const query = `Suppliers/${controller.supplier.id}/getSummary`; - - $httpBackend.expectGET(query).respond({id: 1}); - controller.getSummary(); - $httpBackend.flush(); - - expect(controller.summary).toEqual({id: 1}); - }); - }); - }); -});