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

#824 Update inventory quantity to not allow negative numbers, acceptance test added #848

Merged
merged 1 commit into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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