|
| 1 | +/* |
| 2 | + * This program is part of the OpenLMIS logistics management information system platform software. |
| 3 | + * Copyright © 2017 VillageReach |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify it under the terms |
| 6 | + * of the GNU Affero General Public License as published by the Free Software Foundation, either |
| 7 | + * version 3 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 10 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + * See the GNU Affero General Public License for more details. You should have received a copy of |
| 12 | + * the GNU Affero General Public License along with this program. If not, see |
| 13 | + * http://www.gnu.org/licenses. For additional information contact info@OpenLMIS.org. |
| 14 | + */ |
| 15 | + |
| 16 | +describe('QuantityUnitInputController', function() { |
| 17 | + |
| 18 | + let vm, $controller, quantityUnitCalculateService; |
| 19 | + |
| 20 | + beforeEach(function() { |
| 21 | + module('openlmis-quantity-unit-input'); |
| 22 | + |
| 23 | + quantityUnitCalculateService = jasmine.createSpyObj('quantityUnitCalculateService', [ |
| 24 | + 'recalculateInputQuantity' |
| 25 | + ]); |
| 26 | + |
| 27 | + inject(function($injector) { |
| 28 | + $controller = $injector.get('$controller'); |
| 29 | + }); |
| 30 | + |
| 31 | + vm = $controller('QuantityUnitInputController', { |
| 32 | + quantityUnitCalculateService: quantityUnitCalculateService |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('isQuantityRemainderInDosesDisabled', function() { |
| 37 | + |
| 38 | + it('should return true when netContent is equal to 1', function() { |
| 39 | + vm.netContent = 1; |
| 40 | + |
| 41 | + expect(vm.isQuantityRemainderInDosesDisabled()).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return false when netContent is greater than 1', function() { |
| 45 | + vm.netContent = 10; |
| 46 | + |
| 47 | + expect(vm.isQuantityRemainderInDosesDisabled()).toBe(false); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('onInit', function() { |
| 52 | + |
| 53 | + it('should reset quantityRemainderInDoses to 0 when disabled', function() { |
| 54 | + vm.netContent = 1; |
| 55 | + vm.item = { |
| 56 | + quantityRemainderInDoses: 5 |
| 57 | + }; |
| 58 | + |
| 59 | + vm.$onInit(); |
| 60 | + |
| 61 | + expect(vm.item.quantityRemainderInDoses).toBe(0); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not change quantityRemainderInDoses when not disabled', function() { |
| 65 | + vm.netContent = 10; |
| 66 | + vm.item = { |
| 67 | + quantityRemainderInDoses: 5 |
| 68 | + }; |
| 69 | + |
| 70 | + vm.$onInit(); |
| 71 | + |
| 72 | + expect(vm.item.quantityRemainderInDoses).toBe(5); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should not fail when item is not set', function() { |
| 76 | + vm.netContent = 1; |
| 77 | + vm.item = undefined; |
| 78 | + |
| 79 | + expect(function() { |
| 80 | + vm.$onInit(); |
| 81 | + }).not.toThrow(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('changeValue', function() { |
| 86 | + |
| 87 | + it('should recalculate input quantity and emit the change', function() { |
| 88 | + const item = { |
| 89 | + quantityInPacks: 2 |
| 90 | + }; |
| 91 | + const recalculated = { |
| 92 | + quantityInPacks: 2, |
| 93 | + quantity: 20 |
| 94 | + }; |
| 95 | + vm.netContent = 10; |
| 96 | + vm.showInDoses = false; |
| 97 | + vm.onChangeQuantity = jasmine.createSpy('onChangeQuantity'); |
| 98 | + quantityUnitCalculateService.recalculateInputQuantity.andReturn(recalculated); |
| 99 | + |
| 100 | + vm.changeValue(item); |
| 101 | + |
| 102 | + expect(quantityUnitCalculateService.recalculateInputQuantity) |
| 103 | + .toHaveBeenCalledWith(item, 10, false); |
| 104 | + |
| 105 | + expect(vm.onChangeQuantity).toHaveBeenCalledWith({ |
| 106 | + lineItem: recalculated |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | +}); |
0 commit comments