From 52a1e3f652ea45507aab0f09b2a5c5ca9b4be927 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Mon, 18 Jan 2021 16:32:54 +0100 Subject: [PATCH] fixed price controller tests --- modules/item/front/fixed-price/index.js | 2 +- modules/item/front/fixed-price/index.spec.js | 87 ++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 modules/item/front/fixed-price/index.spec.js diff --git a/modules/item/front/fixed-price/index.js b/modules/item/front/fixed-price/index.js index 8e47ea5f2..ead3e4899 100644 --- a/modules/item/front/fixed-price/index.js +++ b/modules/item/front/fixed-price/index.js @@ -16,8 +16,8 @@ export default class Controller extends Section { upsertPrice(price) { price.hasMinPrice = price.minPrice ? true : false; - let requiredFields = ['itemFk', 'started', 'ended', 'rate2', 'rate3']; + let requiredFields = ['itemFk', 'started', 'ended', 'rate2', 'rate3']; for (let field of requiredFields) if (price[field] == undefined) return; diff --git a/modules/item/front/fixed-price/index.spec.js b/modules/item/front/fixed-price/index.spec.js new file mode 100644 index 000000000..faf6b850a --- /dev/null +++ b/modules/item/front/fixed-price/index.spec.js @@ -0,0 +1,87 @@ +import './index'; + +describe('fixed price', () => { + describe('Component vnFixedPrice', () => { + let controller; + let $httpBackend; + + beforeEach(ngModule('item')); + + beforeEach(inject(($componentController, _$httpBackend_, $rootScope) => { + $httpBackend = _$httpBackend_; + const $scope = $rootScope.$new(); + const $element = angular.element(''); + controller = $componentController('vnFixedPrice', {$element, $scope}); + })); + + describe('upsertPrice()', () => { + it('should do nothing if one or more required arguments are missing', () => { + jest.spyOn(controller.vnApp, 'showSuccess'); + + controller.upsertPrice({}); + + expect(controller.vnApp.showSuccess).not.toHaveBeenCalled(); + }); + + it('should perform an http request to update the price', () => { + const now = new Date(); + jest.spyOn(controller.vnApp, 'showSuccess'); + + $httpBackend.expectPATCH('FixedPrices/upsertFixedPrice').respond(); + controller.upsertPrice({ + itemFk: 1, + started: now, + ended: now, + rate2: 1, + rate3: 2 + }); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + + describe('removePrice()', () => { + it(`should only remove the created instance by the model as it doesn't have an ID yet`, () => { + const $index = 0; + controller.$ = { + model: { + remove: () => {}, + data: [{ + foo: 'bar' + }] + } + }; + jest.spyOn(controller.vnApp, 'showSuccess'); + jest.spyOn(controller.$.model, 'remove'); + + controller.removePrice($index); + + expect(controller.vnApp.showSuccess).not.toHaveBeenCalled(); + expect(controller.$.model.remove).toHaveBeenCalled(); + }); + + it('should remove the instance performing an delete http request', () => { + const $index = 0; + controller.$ = { + model: { + remove: () => {}, + data: [{ + id: '1' + }] + } + }; + jest.spyOn(controller.vnApp, 'showSuccess'); + jest.spyOn(controller.$.model, 'remove'); + + const query = `FixedPrices/${controller.$.model.data[0].id}`; + $httpBackend.expectDELETE(query).respond(); + controller.removePrice($index); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + expect(controller.$.model.remove).toHaveBeenCalled(); + }); + }); + }); +});