23
23
define (
24
24
[
25
25
"zepto" ,
26
+ "moment" ,
26
27
"../../src/controllers/MCTTableController"
27
28
] ,
28
- function ( $ , MCTTableController ) {
29
+ function ( $ , moment , MCTTableController ) {
29
30
30
31
var MOCK_ELEMENT_TEMPLATE =
31
32
'<div><div class="l-view-section scrolling">' +
@@ -40,7 +41,10 @@ define(
40
41
watches ,
41
42
mockTimeout ,
42
43
mockElement ,
43
- mockExportService ;
44
+ mockExportService ,
45
+ mockConductor ,
46
+ mockFormatService ,
47
+ mockFormat ;
44
48
45
49
function promise ( value ) {
46
50
return {
@@ -50,6 +54,12 @@ define(
50
54
} ;
51
55
}
52
56
57
+ function getCallback ( target , event ) {
58
+ return target . calls . filter ( function ( call ) {
59
+ return call . args [ 0 ] === event ;
60
+ } ) [ 0 ] . args [ 1 ] ;
61
+ }
62
+
53
63
beforeEach ( function ( ) {
54
64
watches = { } ;
55
65
@@ -67,15 +77,33 @@ define(
67
77
'exportCSV'
68
78
] ) ;
69
79
80
+ mockConductor = jasmine . createSpyObj ( 'conductor' , [
81
+ 'bounds' ,
82
+ 'timeOfInterest' ,
83
+ 'timeSystem' ,
84
+ 'on' ,
85
+ 'off'
86
+ ] ) ;
87
+
70
88
mockScope . displayHeaders = true ;
71
89
mockTimeout = jasmine . createSpy ( '$timeout' ) ;
72
90
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 ) ;
73
99
74
100
controller = new MCTTableController (
75
101
mockScope ,
76
102
mockTimeout ,
77
103
mockElement ,
78
- mockExportService
104
+ mockExportService ,
105
+ mockFormatService ,
106
+ { conductor : mockConductor }
79
107
) ;
80
108
spyOn ( controller , 'setVisibleRows' ) . andCallThrough ( ) ;
81
109
} ) ;
@@ -86,6 +114,124 @@ define(
86
114
expect ( mockScope . $watch ) . toHaveBeenCalledWith ( 'rows' , jasmine . any ( Function ) ) ;
87
115
} ) ;
88
116
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
+
89
235
describe ( 'rows' , function ( ) {
90
236
var testRows = [ ] ;
91
237
beforeEach ( function ( ) {
@@ -132,7 +278,7 @@ define(
132
278
} ) ;
133
279
134
280
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' ) ,
136
282
row4 = {
137
283
'col1' : { 'text' : 'row3 col1' } ,
138
284
'col2' : { 'text' : 'ghi' } ,
@@ -146,7 +292,7 @@ define(
146
292
} ) ;
147
293
148
294
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' ) ;
150
296
controller . setRows ( testRows ) ;
151
297
expect ( mockScope . displayRows . length ) . toBe ( 3 ) ;
152
298
removeRowFunc ( undefined , 2 ) ;
@@ -173,6 +319,10 @@ define(
173
319
describe ( 'sorting' , function ( ) {
174
320
var sortedRows ;
175
321
322
+ beforeEach ( function ( ) {
323
+ sortedRows = [ ] ;
324
+ } )
325
+
176
326
it ( 'Sorts rows ascending' , function ( ) {
177
327
mockScope . sortColumn = 'col1' ;
178
328
mockScope . sortDirection = 'asc' ;
@@ -204,6 +354,20 @@ define(
204
354
expect ( sortedRows [ 2 ] . col2 . text ) . toEqual ( 'abc' ) ;
205
355
} ) ;
206
356
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
+
207
371
// https://github.com/nasa/openmct/issues/910
208
372
it ( 'updates visible rows in scope' , function ( ) {
209
373
var oldRows ;
@@ -369,8 +533,6 @@ define(
369
533
370
534
} ) ;
371
535
} ) ;
372
-
373
-
374
536
} ) ;
375
537
} ) ;
376
538
} ) ;
0 commit comments