Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
#824 Update inventory quantity to not allow negative numbers, added t…
Browse files Browse the repository at this point in the history
…est (#848)
  • Loading branch information
marcorosas1991 authored and jkleinsc committed Dec 9, 2016
1 parent 12e58a6 commit 78ec35d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/components/quantity-conv.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default Ember.Component.extend({
let quantity = this.get('quantity');
let quantityClass = 'has-success';
let targetUnit = this.get('targetUnit');
if (!Ember.isEmpty(targetUnit) && (Ember.isEmpty(quantity) || isNaN(quantity))) {
if (!Ember.isEmpty(targetUnit) && (Ember.isEmpty(quantity) || isNaN(quantity) || quantity < 0)) {
this.set('quantityHelp', 'not a valid number');
quantityClass = 'has-error';
} else {
Expand Down
3 changes: 2 additions & 1 deletion app/models/inv-purchase.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ let InventoryPurchaseItem = AbstractModel.extend(LocationName, {
numericality: true
},
originalQuantity: {
numericality: true
numericality: true,
greaterThanOrEqualTo: 0
},
vendor: {
presence: true
Expand Down
5 changes: 4 additions & 1 deletion app/models/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export default AbstractModel.extend(LocationName, {
presence: true
},
quantity: {
numericality: validateIfNewItem
numericality: {
validateIfNewItem,
greaterThanOrEqualTo: 0
}
},
price: {
numericality: {
Expand Down
37 changes: 37 additions & 0 deletions tests/acceptance/inventory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,43 @@ test('Adding a new inventory item', (assert) => {
});
});

test('Items with negative quantites should not be saved', (assert) => {
runWithPouchDump('default', function() {
authenticateUser();
visit('/inventory/edit/new');

andThen(() => {
assert.equal(currentURL(), '/inventory/edit/new');
});
fillIn('.test-inv-name input', 'Biogesic');
select('.test-inv-rank', 'B');
fillIn('textarea', 'Biogesic nga medisina');
select('.test-inv-type', 'Medication');
fillIn('.test-inv-cross input', '2600');
fillIn('.test-inv-reorder input', '100');
fillIn('.test-inv-price input', '5');
select('.test-inv-dist-unit', 'tablet');
fillIn('.test-inv-quantity input', '-1000');
fillIn('.test-inv-cost input', '4000');
select('.test-inv-unit', 'tablet');
typeAheadFillIn('.test-vendor', 'Alpha Pharmacy');
click('button:contains(Add)');
waitToAppear('.modal-dialog');

andThen(() => {
assert.equal(find('.modal-title').text(), 'Warning!!!!', 'Inventory Item with negative quantity should not be saved.');
});
click('button:contains(Ok)');

andThen(() => {
assert.equal(currentURL(), '/inventory/edit/new');
findWithAssert('button:contains(Add)');
findWithAssert('button:contains(Cancel)');
assert.equal(find('.test-inv-quantity .help-block').text(), 'not a valid number', 'Error message should be present for invalid quantities');
});
});
});

test('Visiting /inventory/barcode', (assert) => {
runWithPouchDump('inventory', function() {
authenticateUser();
Expand Down

0 comments on commit 78ec35d

Please sign in to comment.