Skip to content

Commit c3322e3

Browse files
committed
Added tests for MctTableController
1 parent 8e76ebb commit c3322e3

File tree

6 files changed

+215
-27
lines changed

6 files changed

+215
-27
lines changed

platform/commonUI/formats/src/UTCTimeFormatSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,26 @@ define([
5858
expect(format.format(APRIL, scale)).toBe("April");
5959
expect(format.format(TWENTY_SIXTEEN, scale)).toBe("2016");
6060
});
61+
62+
it("Returns appropriate time units for a given time span", function () {
63+
var ONE_DAY = 1000 * 60 * 60 * 24;
64+
var FIVE_DAYS = 5 * ONE_DAY;
65+
var TWO_MONTHS = 60 * ONE_DAY;
66+
67+
var ONE_YEAR = 365 * ONE_DAY;
68+
var SEVEN_YEARS = 7 * ONE_YEAR;
69+
var TWO_DECADES = 20 * ONE_YEAR;
70+
71+
//A span of one day should show a zoom label of "Hours"
72+
expect(format.timeUnits(ONE_DAY)).toEqual("Hours");
73+
//Multiple days should display "Days"
74+
expect(format.timeUnits(FIVE_DAYS)).toEqual("Days");
75+
expect(format.timeUnits(TWO_MONTHS)).toEqual("Months");
76+
//A span of one year should show a zoom level of "Months".
77+
// Multiple years will show "Years"
78+
expect(format.timeUnits(ONE_YEAR)).toEqual("Months");
79+
expect(format.timeUnits(SEVEN_YEARS)).toEqual("Years");
80+
expect(format.timeUnits(TWO_DECADES)).toEqual("Decades");
81+
});
6182
});
6283
});

platform/features/table/res/templates/historical-table.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
rows="rows"
77
enableFilter="true"
88
enableSort="true"
9-
default-sort="defaultSort"
9+
sort-column="defaultSort"
1010
class="tabular-holder has-control-bar">
1111
</mct-table>
1212
</div>

platform/features/table/res/templates/rt-table.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
enableFilter="true"
77
enableSort="true"
88
class="tabular-holder has-control-bar"
9-
default-sort="defaultSort"
9+
sort-column="defaultSort"
1010
auto-scroll="true">
1111
</mct-table>
1212
</div>

platform/features/table/src/controllers/MCTTableController.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ define(
102102
* Populated from the default-sort attribute on MctTable
103103
* directive tag.
104104
*/
105-
$scope.$watch('defaultSort', function (defaultSort) {
106-
$scope.sortColumn = defaultSort;
107-
$scope.sortDirection = 'asc';
108-
});
105+
$scope.$watch('sortColumn', $scope.toggleSort);
109106

110107
/*
111108
* Listen for resize events to trigger recalculation of table width
@@ -559,7 +556,8 @@ define(
559556
}
560557

561558
this.$scope.displayRows = this.filterAndSort(newRows || []);
562-
this.resize(newRows).then(this.setVisibleRows)
559+
this.resize(newRows)
560+
.then(this.setVisibleRows)
563561
//Timeout following setVisibleRows to allow digest to
564562
// perform DOM changes, otherwise scrollTo won't work.
565563
.then(this.$timeout)
@@ -632,18 +630,24 @@ define(
632630
* will be sorted before display.
633631
*/
634632
MCTTableController.prototype.setTimeOfInterest = function (newTOI) {
633+
var isSortedByTime =
634+
this.$scope.timeColumns &&
635+
this.$scope.timeColumns.indexOf(this.$scope.sortColumn) !== -1;
636+
635637
this.$scope.toiRowIndex = -1;
636-
if (this.$scope.timeColumns.indexOf(this.$scope.sortColumn) !== -1
637-
&& newTOI
638-
&& this.$scope.displayRows.length > 0) {
639-
var formattedTOI = this.toiFormatter.format(newTOI);
640-
// array, searchElement, min, max
641-
var rowIndex = this.binarySearch(this.$scope.displayRows,
642-
formattedTOI, 0, this.$scope.displayRows.length - 1);
643-
if (rowIndex > 0 && rowIndex < this.$scope.displayRows.length) {
644-
this.$scope.toiRowIndex = rowIndex;
645-
this.scrollToRow(this.$scope.toiRowIndex);
646-
}
638+
639+
if (newTOI && isSortedByTime) {
640+
var formattedTOI = this.toiFormatter.format(newTOI);
641+
var rowIndex = this.binarySearch(
642+
this.$scope.displayRows,
643+
formattedTOI,
644+
0,
645+
this.$scope.displayRows.length - 1);
646+
647+
if (rowIndex > 0 && rowIndex < this.$scope.displayRows.length) {
648+
this.$scope.toiRowIndex = rowIndex;
649+
this.scrollToRow(this.$scope.toiRowIndex);
650+
}
647651
}
648652
};
649653

platform/features/table/src/directives/MCTTable.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ define(
102102
// by the column that can be used for time conductor
103103
// time of interest.
104104
timeColumns: "=?",
105-
// Indicate the column that should be sorted on by default
106-
defaultSort: "=?"
105+
// Indicate a column to sort on. Allows control of sort
106+
// via configuration (eg. for default sort column).
107+
sortColumn: "=?"
107108
}
108109
};
109110
}

platform/features/table/test/controllers/MCTTableControllerSpec.js

+169-7
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
define(
2424
[
2525
"zepto",
26+
"moment",
2627
"../../src/controllers/MCTTableController"
2728
],
28-
function ($, MCTTableController) {
29+
function ($, moment, MCTTableController) {
2930

3031
var MOCK_ELEMENT_TEMPLATE =
3132
'<div><div class="l-view-section scrolling">' +
@@ -40,7 +41,10 @@ define(
4041
watches,
4142
mockTimeout,
4243
mockElement,
43-
mockExportService;
44+
mockExportService,
45+
mockConductor,
46+
mockFormatService,
47+
mockFormat;
4448

4549
function promise(value) {
4650
return {
@@ -50,6 +54,12 @@ define(
5054
};
5155
}
5256

57+
function getCallback(target, event) {
58+
return target.calls.filter(function (call) {
59+
return call.args[0] === event;
60+
})[0].args[1];
61+
}
62+
5363
beforeEach(function () {
5464
watches = {};
5565

@@ -67,15 +77,33 @@ define(
6777
'exportCSV'
6878
]);
6979

80+
mockConductor = jasmine.createSpyObj('conductor', [
81+
'bounds',
82+
'timeOfInterest',
83+
'timeSystem',
84+
'on',
85+
'off'
86+
]);
87+
7088
mockScope.displayHeaders = true;
7189
mockTimeout = jasmine.createSpy('$timeout');
7290
mockTimeout.andReturn(promise(undefined));
91+
mockFormat = jasmine.createSpyObj('formatter', [
92+
'parse',
93+
'format'
94+
]);
95+
mockFormatService = jasmine.createSpyObj('formatService', [
96+
'getFormat'
97+
]);
98+
mockFormatService.getFormat.andReturn(mockFormat);
7399

74100
controller = new MCTTableController(
75101
mockScope,
76102
mockTimeout,
77103
mockElement,
78-
mockExportService
104+
mockExportService,
105+
mockFormatService,
106+
{conductor: mockConductor}
79107
);
80108
spyOn(controller, 'setVisibleRows').andCallThrough();
81109
});
@@ -86,6 +114,124 @@ define(
86114
expect(mockScope.$watch).toHaveBeenCalledWith('rows', jasmine.any(Function));
87115
});
88116

117+
describe('The time of interest', function() {
118+
var rowsAsc = [];
119+
var rowsDesc = [];
120+
beforeEach(function () {
121+
rowsAsc = [
122+
{
123+
'col1': {'text': 'row1 col1 match'},
124+
'col2': {'text': '2012-10-31 00:00:00.000Z'},
125+
'col3': {'text': 'row1 col3'}
126+
},
127+
{
128+
'col1': {'text': 'row2 col1 match'},
129+
'col2': {'text': '2012-11-01 00:00:00.000Z'},
130+
'col3': {'text': 'row2 col3'}
131+
},
132+
{
133+
'col1': {'text': 'row3 col1'},
134+
'col2': {'text': '2012-11-03 00:00:00.000Z'},
135+
'col3': {'text': 'row3 col3'}
136+
},
137+
{
138+
'col1': {'text': 'row3 col1'},
139+
'col2': {'text': '2012-11-04 00:00:00.000Z'},
140+
'col3': {'text': 'row3 col3'}
141+
}
142+
];
143+
rowsDesc = [
144+
{
145+
'col1': {'text': 'row1 col1 match'},
146+
'col2': {'text': '2012-11-02 00:00:00.000Z'},
147+
'col3': {'text': 'row1 col3'}
148+
},
149+
{
150+
'col1': {'text': 'row2 col1 match'},
151+
'col2': {'text': '2012-11-01 00:00:00.000Z'},
152+
'col3': {'text': 'row2 col3'}
153+
},
154+
{
155+
'col1': {'text': 'row3 col1'},
156+
'col2': {'text': '2012-10-30 00:00:00.000Z'},
157+
'col3': {'text': 'row3 col3'}
158+
},
159+
{
160+
'col1': {'text': 'row3 col1'},
161+
'col2': {'text': '2012-10-29 00:00:00.000Z'},
162+
'col3': {'text': 'row3 col3'}
163+
}
164+
];
165+
mockScope.timeColumns = ['col2'];
166+
mockScope.sortColumn = 'col2';
167+
controller.toiFormatter = mockFormat;
168+
});
169+
it("is observed for changes", function () {
170+
//Mock setting time columns
171+
getCallback(mockScope.$watch, 'timeColumns')(['col2']);
172+
173+
expect(mockConductor.on).toHaveBeenCalledWith('timeOfInterest',
174+
jasmine.any(Function));
175+
});
176+
describe("causes corresponding row to be highlighted", function () {
177+
it("when changed and rows sorted ascending", function () {
178+
var testDate = "2012-11-02 00:00:00.000Z";
179+
mockScope.rows = rowsAsc;
180+
mockScope.displayRows = rowsAsc;
181+
mockScope.sortDirection = 'asc';
182+
183+
var toi = moment.utc(testDate).valueOf();
184+
mockFormat.parse.andReturn(toi);
185+
mockFormat.format.andReturn(testDate);
186+
187+
//mock setting the timeColumns parameter
188+
getCallback(mockScope.$watch, 'timeColumns')(['col2']);
189+
190+
var toiCallback = getCallback(mockConductor.on, 'timeOfInterest');
191+
toiCallback(toi);
192+
193+
expect(mockScope.toiRowIndex).toBe(2);
194+
});
195+
it("when changed and rows sorted descending", function () {
196+
var testDate = "2012-10-31 00:00:00.000Z";
197+
mockScope.rows = rowsDesc;
198+
mockScope.displayRows = rowsDesc;
199+
mockScope.sortDirection = 'desc';
200+
201+
var toi = moment.utc(testDate).valueOf();
202+
mockFormat.parse.andReturn(toi);
203+
mockFormat.format.andReturn(testDate);
204+
205+
//mock setting the timeColumns parameter
206+
getCallback(mockScope.$watch, 'timeColumns')(['col2']);
207+
208+
var toiCallback = getCallback(mockConductor.on, 'timeOfInterest');
209+
toiCallback(toi);
210+
211+
expect(mockScope.toiRowIndex).toBe(2);
212+
});
213+
it("when rows are set and sorted ascending", function () {
214+
var testDate = "2012-11-02 00:00:00.000Z";
215+
mockScope.sortDirection = 'asc';
216+
217+
var toi = moment.utc(testDate).valueOf();
218+
mockFormat.parse.andReturn(toi);
219+
mockFormat.format.andReturn(testDate);
220+
mockConductor.timeOfInterest.andReturn(toi);
221+
222+
//mock setting the timeColumns parameter
223+
getCallback(mockScope.$watch, 'timeColumns')(['col2']);
224+
225+
//Mock setting the rows on scope
226+
var rowsCallback = getCallback(mockScope.$watch, 'rows');
227+
rowsCallback(rowsAsc);
228+
229+
expect(mockScope.toiRowIndex).toBe(2);
230+
});
231+
232+
});
233+
});
234+
89235
describe('rows', function () {
90236
var testRows = [];
91237
beforeEach(function () {
@@ -132,7 +278,7 @@ define(
132278
});
133279

134280
it('Supports adding rows individually', function () {
135-
var addRowFunc = mockScope.$on.calls[mockScope.$on.calls.length - 2].args[1],
281+
var addRowFunc = getCallback(mockScope.$on, 'add:row'),
136282
row4 = {
137283
'col1': {'text': 'row3 col1'},
138284
'col2': {'text': 'ghi'},
@@ -146,7 +292,7 @@ define(
146292
});
147293

148294
it('Supports removing rows individually', function () {
149-
var removeRowFunc = mockScope.$on.calls[mockScope.$on.calls.length - 1].args[1];
295+
var removeRowFunc = getCallback(mockScope.$on, 'remove:row');
150296
controller.setRows(testRows);
151297
expect(mockScope.displayRows.length).toBe(3);
152298
removeRowFunc(undefined, 2);
@@ -173,6 +319,10 @@ define(
173319
describe('sorting', function () {
174320
var sortedRows;
175321

322+
beforeEach(function() {
323+
sortedRows = [];
324+
})
325+
176326
it('Sorts rows ascending', function () {
177327
mockScope.sortColumn = 'col1';
178328
mockScope.sortDirection = 'asc';
@@ -204,6 +354,20 @@ define(
204354
expect(sortedRows[2].col2.text).toEqual('abc');
205355
});
206356

357+
it('Allows sort column to be changed externally by ' +
358+
'setting or changing sortBy attribute', function () {
359+
mockScope.displayRows = testRows;
360+
var sortByCB = getCallback(mockScope.$watch, 'sortColumn');
361+
sortByCB('col2');
362+
363+
expect(mockScope.sortDirection).toEqual('asc');
364+
365+
expect(mockScope.displayRows[0].col2.text).toEqual('abc');
366+
expect(mockScope.displayRows[1].col2.text).toEqual('def');
367+
expect(mockScope.displayRows[2].col2.text).toEqual('ghi');
368+
369+
});
370+
207371
// https://github.com/nasa/openmct/issues/910
208372
it('updates visible rows in scope', function () {
209373
var oldRows;
@@ -369,8 +533,6 @@ define(
369533

370534
});
371535
});
372-
373-
374536
});
375537
});
376538
});

0 commit comments

Comments
 (0)