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

Define issues with MedicationDetails and tests #874

Merged
merged 3 commits into from
Dec 15, 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
28 changes: 23 additions & 5 deletions app/mixins/medication-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import Ember from 'ember';
import DS from 'ember-data';
export default Ember.Mixin.create({
// Denormalized medication details so that inventory records do not need to be retrieved

/**
* Does not return name on first call if medicationName is
* not set and name is obtained via inventoryAttribute
* Additional calls will return the name as medicationName
* will then be set
*/
getMedicationName(inventoryAttribute) {
let medicationTitle = this.get('medicationTitle');
if (!Ember.isEmpty(medicationTitle)) {
return medicationTitle;
} else {
let inventoryObject = this.get(inventoryAttribute);
if (inventoryObject.then) {
this.get(inventoryAttribute).then((inventoryItem) => {
inventoryObject.then((inventoryItem) => {
this.set('medicationTitle', inventoryItem.get('name'));
});
} else {
Expand All @@ -18,14 +25,25 @@ export default Ember.Mixin.create({
}
},

/**
* Does not return name on first call if priceOfMedication is
* not set and price is obtained via inventoryAttribute
* Additional calls will return the price as priceOfMedication
* will then be set
*/
getMedicationPrice(inventoryAttribute) {
let priceOfMedication = this.get('priceOfMedication');
if (!Ember.isEmpty(priceOfMedication)) {
return priceOfMedication;
} else {
this.get(inventoryAttribute).then((inventoryItem) => {
this.set('priceOfMedication', inventoryItem.get('price'));
});
let inventoryObject = this.get(inventoryAttribute);
if (inventoryObject.then) {
inventoryObject.then((inventoryItem) => {
this.set('priceOfMedication', inventoryItem.get('price'));
});
} else {
this.set('priceOfMedication', inventoryObject.get('price'));
}
}
},

Expand All @@ -41,7 +59,7 @@ export default Ember.Mixin.create({
} else {
let objectInventoryItem = this.get(inventoryAttribute);
if (objectInventoryItem.then) {
this.get(inventoryAttribute).then((inventoryItem) => {
objectInventoryItem.then((inventoryItem) => {
resolve({
name: inventoryItem.get('name'),
price: inventoryItem.get('price')
Expand Down
132 changes: 132 additions & 0 deletions tests/unit/mixins/medication-details-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import MedicationDetails from 'hospitalrun/mixins/medication-details';
import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';
import DS from 'ember-data';

moduleFor('mixin:medication-details', 'Unit | Mixin | medication-details', {
needs: [
'ember-validations@validator:local/numericality',
'ember-validations@validator:local/presence',
'model:inventory',
'model:inv-purchase',
'model:inv-location'
],
subject(attrs) {
let subject;
Ember.run(() => {
let Test = DS.Model.extend(MedicationDetails);
this.register('model:test', Test);
subject = this.store().createRecord('test', attrs);
});

return subject;
},
store() {
return this.container.lookup('service:store');
}
});

test('getMedicationName', function(assert) {
let medicationDetails = this.subject({
medicationTitle: 'Medication Title'
});

assert.strictEqual(medicationDetails.getMedicationName(), 'Medication Title');
});

test('getMedicationName prefer medicationTitle', function(assert) {
let inventoryItem;
Ember.run(() => {
inventoryItem = this.store().createRecord('inventory', {
name: 'Test Item'
});
});

let medicationDetails = this.subject({
medicationTitle: 'Medication Title',
inventoryItem
});

assert.strictEqual(medicationDetails.getMedicationName('inventoryItem'), 'Medication Title');
});

test('getMedicationName attribute', function(assert) {
let inventoryItem;
Ember.run(() => {
inventoryItem = this.store().createRecord('inventory', {
name: 'Test Item'
});
});

let medicationDetails = this.subject({ inventoryItem });
/**
* We run this twice because if this gets the value from
* the attribute it does not actually return the value
*/
Ember.run(() => medicationDetails.getMedicationName('inventoryItem'));

assert.strictEqual(medicationDetails.getMedicationName('inventoryItem'), 'Test Item');
});

test('getMedicationPrice', function(assert) {
let medicationDetails = this.subject({
priceOfMedication: 15.50
});

assert.strictEqual(medicationDetails.getMedicationPrice(), 15.50);
});

test('getMedicationPrice prefer priceOfMedication', function(assert) {
let inventoryItem;
Ember.run(() => {
inventoryItem = this.store().createRecord('inventory', {
name: 'Test Item',
price: 12.15
});
});

let medicationDetails = this.subject({
priceOfMedication: 15.5,
inventoryItem
});

assert.strictEqual(medicationDetails.getMedicationPrice('inventoryItem'), 15.5);
});

test('getMedicationPrice attribute', function(assert) {
let inventoryItem;
Ember.run(() => {
inventoryItem = this.store().createRecord('inventory', {
name: 'Test Item',
price: 22.33
});
});

let medicationDetails = this.subject({ inventoryItem });

/**
* We run this twice because if this gets the value from
* the attribute it does not actually return the value
*/
Ember.run(() => medicationDetails.getMedicationPrice('inventoryItem'));

assert.strictEqual(medicationDetails.getMedicationPrice('inventoryItem'), 22.33);
});

test('getMedicationDetails', function(assert) {
let done = assert.async();

let medicationDetails = this.subject({
medicationTitle: 'Medication Title',
priceOfMedication: 65.77
});

medicationDetails.getMedicationDetails().then((result) => {
assert.deepEqual(result, {
name: 'Medication Title',
price: 65.77
});

done();
});
});