Created
February 7, 2018 21:20
-
-
Save phillipkregg/442b730612008745604d2d96c36d0a2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
moduleFor, | |
test | |
} from 'ember-qunit'; | |
import { run } from '@ember/runloop'; | |
import _ from 'lodash'; | |
import atAGlanceMetrics from '../../mock-data/market-survey/at-a-glance-metrics'; | |
import dataProviderData from '../../mock-data/market-survey/data-provider'; | |
import trendMetrics from '../../mock-data/market-survey/trend-metrics'; | |
import volumeMetrics from '../../mock-data/market-survey/volume-metrics'; | |
import datesArray from '../../mock-data/market-survey/dates-array'; | |
moduleFor('controller:market-survey', 'Unit | Controller | market survey', { | |
beforeEach() { | |
let self = this; | |
// Attaching the subject to 'this' prevents a global variable | |
self.controller = this.subject(); | |
self.mockDate = Ember.Object.create({ | |
month: 4, | |
year: 2017 | |
}); | |
self.controller.mockMetricsData = dataProviderData; | |
var filter = function () { | |
var mockMetricsData = self.controller.mockMetricsData; | |
return { | |
map: function () { | |
return mockMetricsData; | |
} | |
}; | |
}.bind(self); | |
let model = Ember.Object.create({ | |
last: { | |
value: { | |
useDateRange: 'Month', | |
metrics: { | |
filter: filter, | |
map: function () { | |
return self.controller.mockMetricsData; | |
} | |
}, | |
segment: dataProviderData.model.last.value.segment, | |
segments: dataProviderData.model.last.value.segments | |
} | |
} | |
}); | |
self.controller.set('model', model); | |
self.controller.set('metrics', atAGlanceMetrics); | |
self.controller.set('atAGlanceMetrics', atAGlanceMetrics); | |
self.controller.set('selectedDate', self.mockDate); | |
self.controller.set('date', self.mockDate); | |
// Mock function that is called in last step of changeUseDateRange | |
self.controller.set('changeUseDateRange', function(arg) { | |
return `changed to ${arg}`; | |
}); | |
} | |
}); | |
test('it exists', function (assert) { | |
assert.ok(this.controller); | |
}); | |
/************** ROOT LEVEL METHOD TESTS *****************/ | |
test('getMetrics(period, metricType, date) method', function (assert) { | |
assert.expect(15); | |
let self = this; | |
let mockData = self.controller.mockMetricsData; | |
let metricType = 'Unit', | |
period = 'month', | |
date = Ember.Object.create({ | |
month: 4, | |
year: 2017 | |
}); | |
// Kick off the 'getMetrics' method with mock data | |
let returnedData = self.controller.getMetrics(period, metricType, date); | |
self.controller.set('atAGlanceMetrics', returnedData); | |
assert.strictEqual(returnedData.incomePerAvailableSquareFoot, mockData.incomePerAvailableSquareFoot, 'incomePerAvailableSquareFoot matches'); | |
assert.strictEqual(returnedData.incomePerAvailableUnit, mockData.incomePerAvailableUnit, 'incomePerAvailableUnit matches'); | |
assert.strictEqual(returnedData.isLastYear, mockData.isLastYear, 'isLastYear matches'); | |
assert.strictEqual(returnedData.isPercentChange, mockData.isPercentChange, 'isPercentChange matches'); | |
assert.strictEqual(returnedData.month, mockData.month, 'month matches'); | |
assert.strictEqual(returnedData.moveIns, mockData.moveIns, 'moveIns matches'); | |
assert.strictEqual(returnedData.moveOuts, mockData.moveOuts, 'moveOuts matches'); | |
assert.strictEqual(returnedData.period, mockData.period, 'period matches'); | |
assert.strictEqual(returnedData.rentalRate, mockData.rentalRate, 'rentalRate matches'); | |
assert.strictEqual(returnedData.segment, mockData.segment, 'segment matches'); | |
assert.strictEqual(returnedData.squareFootOccupancy, mockData.squareFootOccupancy, 'squareFootOccupancy matches'); | |
assert.strictEqual(returnedData.squareFootRate, mockData.squareFootRate, 'squareFootRate matches'); | |
assert.strictEqual(returnedData.unitOccupancy, mockData.unitOccupancy, 'unitOccupancy matches'); | |
assert.strictEqual(returnedData.year, mockData.year, 'year matches'); | |
assert.strictEqual(_.isFunction(returnedData.callbackReturned), true, 'returned a callback function'); | |
}); | |
test('mapMetrics(metrics)', function (assert) { | |
assert.expect(12); | |
run(() => { | |
// Kick off the mapMetrics method passing in mock data | |
let self = this; | |
let returnedData = self.controller.mapMetrics(atAGlanceMetrics); | |
assert.strictEqual(Ember.isPresent(returnedData.lastYear), true, 'returned data contains "lastYear"'); | |
assert.strictEqual(returnedData.thisYear, undefined, 'returned data contains "thisYear" with an undefined value'); | |
assert.strictEqual(Ember.isPresent(returnedData.percentChange), true, 'returned data contains "percentChange"'); | |
assert.strictEqual(returnedData.lastYear.available, "532", 'lastYear available matches'); | |
assert.strictEqual(returnedData.percentChange.available, "1%", '% change available matches'); | |
assert.strictEqual(returnedData.percentChange.values.available, 1, '% change values available matches'); | |
assert.strictEqual(returnedData.percentChange.values.moveIns, 2, 'values available moveIns matches'); | |
assert.strictEqual(returnedData.percentChange.values.moveOuts, 3, 'values available moveOuts matches'); | |
assert.strictEqual(returnedData.percentChange.values.occupancy, 4, 'values available occupancy matches'); | |
assert.strictEqual(returnedData.percentChange.values.rate, 5, 'values available rate matches'); | |
assert.strictEqual(returnedData.percentChange.values.rented, 6, 'values available rented matches'); | |
assert.strictEqual(returnedData.percentChange.values.yield, 7, 'values available yield matches'); | |
}); | |
}); | |
test('getTableMetrics(self, date)', function (assert) { | |
assert.expect(6); | |
run(() => { | |
// Kick off the mapMetrics method passing in mock data | |
let self = this; | |
let returnedData = self.controller.getTableMetrics(self.controller, self.mockDate); | |
assert.strictEqual(Ember.isPresent(typeof(returnedData.date)), true, 'the method returns a date object'); | |
assert.strictEqual(Ember.isPresent(Ember.isArray(returnedData.metricGroups)), true, 'the method returns a metricGroups array'); | |
assert.strictEqual(Ember.isPresent(Ember.isArray(returnedData.metricGroups)), true, 'the method returns a date object'); | |
assert.strictEqual(returnedData.date.month, 4, 'the month returned is correct'); | |
assert.strictEqual(returnedData.date.year, 2017, 'the year returned is correct'); | |
assert.strictEqual(returnedData.metricGroups.length, 4, 'the metricGroups array returns the proper number of records.'); | |
}); | |
}); | |
test('changeUseDateRange(useDateRange)', function (assert) { | |
assert.expect(2); | |
run(() => { | |
// This method calls itself in the final line. | |
// The method is mocked in the beforeEach hook. | |
// If the mocked method returns, we know that the initial call happened too. | |
let changeTrue = this.controller.changeUseDateRange(true); | |
let changeFalse = this.controller.changeUseDateRange(false); | |
assert.strictEqual(changeTrue, 'changed to true', 'the method fired and returned true'); | |
assert.strictEqual(changeFalse, 'changed to false', 'the method fired and returned false'); | |
}); | |
}); | |
/************** COMPUTED TESTS *****************/ | |
test('dates(model.last.value)', function (assert) { | |
assert.expect(2); | |
run(() => { | |
let self = this; | |
let controller = self.controller; | |
var filter = function () { | |
return { | |
map: function () { | |
return dataProviderData; | |
} | |
}; | |
}.bind(self); | |
// Mock changes in model | |
let model = Ember.Object.create({ | |
last: { | |
value: { | |
useDateRange: 'Month', | |
metrics: { | |
filter: filter, | |
// Mocking the callback 'map' method that will fire when the dates computed is called | |
map: function () { | |
return datesArray; | |
} | |
}, | |
segment: "market:Houston, TX", | |
segments: controller.get('model.last.value.segments') | |
} | |
} | |
}); | |
run(() => { | |
// Set the controller to the new model to see if computed changed | |
controller.set('model', model); | |
// Call the computed property | |
let dates = controller.get('dates'); | |
assert.strictEqual(Ember.isArray(dates), true, 'computed returns a mock array'); | |
assert.strictEqual(dates.length, 18, 'the mock array is 18 items long'); | |
}); | |
}); | |
}); | |
test('periods(model.last.value.useDateRange)', function (assert) { | |
assert.expect(10); | |
run(() => { | |
let self = this; | |
let controller = self.controller; | |
var filter = function () { | |
return { | |
map: function () { | |
return dataProviderData; | |
} | |
}; | |
}.bind(self); | |
// Mock changes in model | |
let model = Ember.Object.create({ | |
last: { | |
value: { | |
metrics: { | |
filter: filter, | |
// Mocking the callback 'map' method that will fire when the dates computed is called | |
map: function () { | |
return datesArray; | |
} | |
}, | |
segment: "market:Houston, TX", | |
segments: controller.get('model.last.value.segments') | |
} | |
} | |
}); | |
run(() => { | |
// Set the controller to the new model to see if computed changed | |
model.last.value.useDateRange = false; | |
controller.set('model', model); | |
// Call the computed property | |
let periodsFalse = controller.get('periods'); | |
assert.strictEqual(Ember.isArray(periodsFalse), true, 'returns an array'); | |
assert.strictEqual(periodsFalse.length, 2, 'the array length is 2'); | |
assert.strictEqual(periodsFalse[0], "Month", 'the first item is Month'); | |
assert.strictEqual(periodsFalse[1], "YTD", 'the second item is YTD'); | |
// Change the property listened to | |
controller.set('model.last.value.useDateRange', 'Month'); | |
controller.set('model', model); | |
// Call the computed property | |
let periodsTrue = controller.get('periods'); | |
assert.strictEqual(Ember.isArray(periodsTrue), true, 'returns an array'); | |
assert.strictEqual(periodsTrue.length, 4, 'the array length is 4'); | |
assert.strictEqual(periodsTrue[0], "Month", 'the first item is Month'); | |
assert.strictEqual(periodsTrue[1], "YTD", 'the second item is YTD'); | |
assert.strictEqual(periodsTrue[2], "R3", 'the third item is R3'); | |
assert.strictEqual(periodsTrue[3], "R12", 'the fourth item is R12'); | |
}); | |
}); | |
}); | |
test('dataProviderMetrics', function (assert) { | |
assert.expect(1); | |
run(() => { | |
let self = this; | |
let controller = self.controller; | |
var filter = function () { | |
var mockMetricsData = self.controller.mockMetricsData; | |
return { | |
map: function () { | |
return mockMetricsData; | |
} | |
}; | |
}.bind(self); | |
// Mock changes in model | |
let model = Ember.Object.create({ | |
last: { | |
value: { | |
useDateRange: 'Month', | |
metrics: { | |
filter: filter, | |
map: function () { | |
return self.controller.mockMetricsData; | |
} | |
}, | |
segment: "market:Dallas, TX", | |
segments: controller.get('model.last.value.segments') | |
} | |
} | |
}); | |
run(() => { | |
// Set the controller to the new model to see if computed changed | |
controller.set('model', model); | |
// Call the computed property | |
let metrics = controller.get('dataProviderMetrics'); | |
assert.strictEqual(metrics.segment, 'Dallas, TX', 'computed property matches new model changes'); | |
}); | |
}); | |
}); | |
test('paidUserMetrics(model, period, metricType)', function (assert) { | |
run(() => { | |
let self = this; | |
let controller = self.controller; | |
var filter = function () { | |
var mockMetricsData = this.controller.mockMetricsData; | |
return { | |
map: function () { | |
return mockMetricsData; | |
} | |
}; | |
}.bind(this); | |
// Mock changes in model | |
let model = Ember.Object.create({ | |
last: { | |
value: { | |
useDateRange: 'Month', | |
metrics: { | |
filter: filter, | |
map: function () { | |
return self.controller.mockMetricsData; | |
} | |
}, | |
segment: "market:Houston, TX", | |
segments: controller.get('model.last.value.segments') | |
} | |
} | |
}); | |
run(() => { | |
// Set the controller to the new model to see if computed changed | |
// Originally the segment is 'Dallas', after the model change, the segment should be 'Houston' | |
controller.set('model', model); | |
controller.set('dates', datesArray); | |
// Call the computed property to see if the segment changed | |
let paidUserMetrics = controller.get('paidUserMetrics'); | |
assert.strictEqual(Ember.isArray(paidUserMetrics), true, 'returns an array of data'); | |
assert.strictEqual(paidUserMetrics.length, 18, 'the array has 18 items'); | |
_.forEach(paidUserMetrics, function(metric, index) { | |
assert.strictEqual(metric.segment, 'Houston, TX', `metric ${index} has the correct segment name`); | |
}); | |
}); | |
}); | |
}); | |
test('atAGlanceMetrics(model, period, metricType, selectedDate)', function (assert) { | |
assert.expect(5); | |
run(() => { | |
let self = this; | |
let controller = self.controller; | |
let filter = function () { | |
var mockMetricsData = this.controller.mockMetricsData; | |
return { | |
map: function () { | |
return mockMetricsData; | |
} | |
}; | |
}.bind(self); | |
// Mock changes in model | |
let model = Ember.Object.create({ | |
last: { | |
value: { | |
useDateRange: 'Month', | |
metrics: { | |
filter: filter, | |
map: function () { | |
return self.controller.mockMetricsData; | |
} | |
}, | |
segment: "market:Austin, TX", | |
segments: self.controller.model.last.value.segments | |
} | |
} | |
}); | |
run(() => { | |
// Set the controller to the new model to see if computed changed | |
controller.set('model', model); | |
// Call the computed property | |
let returnedMetrics = controller.get('atAGlanceMetrics'); | |
assert.strictEqual(typeof(returnedMetrics), 'object', 'the returned metrics data is an object'); | |
assert.strictEqual(Ember.isPresent(returnedMetrics.lastYear), true, 'the returned metrics contains the "lastYear" property'); | |
assert.strictEqual(Ember.isPresent(returnedMetrics.percentChange), true, 'the returned metrics contains the "percentChange" property'); | |
assert.strictEqual(Ember.isPresent(returnedMetrics.thisYear), true, 'the returned metrics contains the "thisYear" property'); | |
assert.strictEqual(returnedMetrics.lastYear.segment, "Market: Austin, TX", 'the segment is correct'); | |
}); | |
}); | |
}); | |
test('trendMetrics(model.last.value.metrics, period, metricType, trendMetric)', function (assert) { | |
assert.expect(3); | |
run(() => { | |
let self = this; | |
let controller = self.controller; | |
var filter = function () { | |
return { | |
map: function () { | |
// If "getMetrics" is called, it will use this callback to return trend specific data | |
return trendMetrics; | |
} | |
}; | |
}.bind(self); | |
var map = function () { | |
return self.controller.mockMetricsData; | |
}; | |
run(() => { | |
// Change the model properties the computed listens to | |
// These callbacks will get triggered by the other methods that use the computed property (getMetrics) | |
controller.set('model.last.value.metrics', { | |
filter: filter, | |
map: map | |
}); | |
// Call the computed property | |
let returnedMetrics = controller.get('trendMetrics'); | |
// If the computed fired, it should return the trendMetrics data | |
assert.strictEqual(Ember.isArray(returnedMetrics), true, 'the returned metrics data is an array'); | |
assert.strictEqual(returnedMetrics.length, 4, 'the array contains the proper number of items'); | |
assert.strictEqual(returnedMetrics[0].type, 'trendMetrics', 'a mock property that returns the "type" of data is correct'); | |
}); | |
}); | |
}); | |
test('volumeMetrics(model.last.value.segment, selectedDate, period)', function (assert) { | |
assert.expect(5); | |
run(() => { | |
let self = this; | |
let controller = self.controller; | |
var filter = function () { | |
return { | |
map: function () { | |
return volumeMetrics; | |
} | |
}; | |
}.bind(self); | |
let model = volumeMetrics.model; | |
model.last.value.metrics.filter = filter; | |
run(() => { | |
// Change the model properties the computed listens to | |
// These callbacks will get triggered by the other methods that use the computed property (getMetrics) | |
controller.set('model', model); | |
controller.set('model.last.value.segment', { "segment": "market:Dallas, TX" }); | |
// Call the computed property | |
let returnedMetrics = controller.get('volumeMetrics'); | |
// If the computed fired, it should return the trendMetrics data | |
assert.strictEqual(typeof(returnedMetrics), 'object', 'the returned metrics data is an object'); | |
assert.strictEqual(Ember.isPresent(returnedMetrics.lastYear), true, 'the object contains the property "lastYear"'); | |
assert.strictEqual(Ember.isPresent(returnedMetrics.thisYear), true, 'the object contains the property "thisYear"'); | |
assert.strictEqual(Ember.isPresent(returnedMetrics.percentChange), true, 'the object contains the property "percentChange"'); | |
assert.strictEqual(returnedMetrics.thisYear.type, 'volumeMetrics', 'a mock property that returns the "type" of data is correct'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment