From 83359cbf63f3c984f7d7d59f657f000dde7952f9 Mon Sep 17 00:00:00 2001 From: gerard Date: Thu, 9 Aug 2018 12:46:50 +0200 Subject: [PATCH] Tarea #508 /src/card/index.js Front unit test --- client/order/src/card/index.js | 14 +++--- client/order/src/card/index.spec.js | 67 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 client/order/src/card/index.spec.js diff --git a/client/order/src/card/index.js b/client/order/src/card/index.js index bb19ca6868..f6afa3ec7b 100644 --- a/client/order/src/card/index.js +++ b/client/order/src/card/index.js @@ -5,10 +5,7 @@ class Controller { this.$http = $http; this.$state = $state; this.order = {}; - } - - getOrder() { - let filter = { + this.filter = { include: [ {relation: 'agencyMode', scope: {fields: ['name']}}, {relation: 'address', scope: {fields: ['nickname']}}, @@ -25,13 +22,16 @@ class Controller { } ] }; + } - let json = encodeURIComponent(JSON.stringify(filter)); + getOrder() { + let json = encodeURIComponent(JSON.stringify(this.filter)); let query = `/order/api/Orders/${this.$state.params.id}?filter=${json}`; this.$http.get(query).then(res => { - if (res.data) + if (res.data) { this.order = res.data; - this.getTotal(); + this.getTotal(); + } }); } diff --git a/client/order/src/card/index.spec.js b/client/order/src/card/index.spec.js new file mode 100644 index 0000000000..45e2b1375d --- /dev/null +++ b/client/order/src/card/index.spec.js @@ -0,0 +1,67 @@ +import './index.js'; + +describe('Order', () => { + describe('Component vnOrderCard', () => { + let $componentController; + let $scope; + let controller; + let $httpBackend; + let $state; + + beforeEach(() => { + angular.mock.module('order'); + }); + + beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_) => { + $componentController = _$componentController_; + $httpBackend = _$httpBackend_; + $scope = $rootScope.$new(); + $scope.card = {createOrder: () => {}}; + $state = {params: {id: 1}}; + controller = $componentController('vnOrderCard', {$scope: $scope, $state: $state}); + })); + + describe('getOrder()', () => { + it(`should make a query, save the data in order and call get order if the response has data`, () => { + spyOn(controller, 'getTotal'); + let json = encodeURIComponent(JSON.stringify(controller.filter)); + $httpBackend.expectGET(`/order/api/Orders/${controller.$state.params.id}?filter=${json}`).respond({id: 1}); + controller.getOrder(); + $httpBackend.flush(); + + expect(controller.order).toEqual({id: 1}); + expect(controller.getTotal).toHaveBeenCalledWith(); + }); + + it(`should make a query and not call getTotal if the response is not defined`, () => { + spyOn(controller, 'getTotal'); + let json = encodeURIComponent(JSON.stringify(controller.filter)); + $httpBackend.expectGET(`/order/api/Orders/${controller.$state.params.id}?filter=${json}`).respond(undefined); + controller.getOrder(); + $httpBackend.flush(); + + expect(controller.order).toEqual({}); + expect(controller.getTotal).not.toHaveBeenCalledWith(); + }); + }); + + describe('getTotal()', () => { + it(`should make a query and save the data in order.total`, () => { + $httpBackend.expectGET(`/order/api/Orders/${controller.$state.params.id}/getTotal`).respond({total: '20M'}); + controller.getTotal(); + $httpBackend.flush(); + + expect(controller.order.total).toEqual('20M'); + }); + + it(`should make a query and not save the respones if is not defined`, () => { + $httpBackend.expectGET(`/order/api/Orders/${controller.$state.params.id}/getTotal`).respond(); + controller.getTotal(); + $httpBackend.flush(); + + expect(controller.order.total).toEqual(undefined); + }); + }); + }); +}); +