2016-02-26 19:09:51 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
define(
|
|
|
|
[
|
|
|
|
"../../src/controllers/MCTTableController"
|
|
|
|
],
|
|
|
|
function (MCTTableController) {
|
|
|
|
|
|
|
|
describe('The MCTTable Controller', function() {
|
|
|
|
|
|
|
|
var controller,
|
|
|
|
mockScope,
|
|
|
|
watches,
|
|
|
|
mockTimeout,
|
|
|
|
mockElement;
|
|
|
|
|
|
|
|
function promise(value) {
|
|
|
|
return {
|
2016-04-08 23:05:04 +00:00
|
|
|
then: function (callback) {
|
2016-02-26 19:09:51 +00:00
|
|
|
return promise(callback(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
watches = {};
|
|
|
|
|
|
|
|
mockScope = jasmine.createSpyObj('scope', [
|
2016-03-09 05:36:33 +00:00
|
|
|
'$watch',
|
|
|
|
'$on',
|
2016-02-26 19:09:51 +00:00
|
|
|
'$watchCollection'
|
|
|
|
]);
|
|
|
|
mockScope.$watchCollection.andCallFake(function(event, callback) {
|
|
|
|
watches[event] = callback;
|
|
|
|
});
|
|
|
|
|
|
|
|
mockElement = jasmine.createSpyObj('element', [
|
|
|
|
'find',
|
|
|
|
'on'
|
|
|
|
]);
|
|
|
|
mockElement.find.andReturn(mockElement);
|
|
|
|
|
|
|
|
mockScope.displayHeaders = true;
|
|
|
|
mockTimeout = jasmine.createSpy('$timeout');
|
2016-03-09 05:36:33 +00:00
|
|
|
mockTimeout.andReturn(promise(undefined));
|
2016-02-26 19:09:51 +00:00
|
|
|
|
|
|
|
controller = new MCTTableController(mockScope, mockTimeout, mockElement);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Reacts to changes to filters, headers, and rows', function() {
|
|
|
|
expect(mockScope.$watchCollection).toHaveBeenCalledWith('filters', jasmine.any(Function));
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.$watch).toHaveBeenCalledWith('headers', jasmine.any(Function));
|
|
|
|
expect(mockScope.$watch).toHaveBeenCalledWith('rows', jasmine.any(Function));
|
2016-02-26 19:09:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('rows', function() {
|
|
|
|
var testRows = [];
|
|
|
|
beforeEach(function() {
|
|
|
|
testRows = [
|
|
|
|
{
|
|
|
|
'col1': {'text': 'row1 col1 match'},
|
|
|
|
'col2': {'text': 'def'},
|
|
|
|
'col3': {'text': 'row1 col3'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'col1': {'text': 'row2 col1 match'},
|
|
|
|
'col2': {'text': 'abc'},
|
|
|
|
'col3': {'text': 'row2 col3'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'col1': {'text': 'row3 col1'},
|
|
|
|
'col2': {'text': 'ghi'},
|
|
|
|
'col3': {'text': 'row3 col3'}
|
|
|
|
}
|
|
|
|
];
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.rows = testRows;
|
2016-02-26 19:09:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Filters results based on filter input', function() {
|
|
|
|
var filters = {},
|
|
|
|
filteredRows;
|
|
|
|
|
|
|
|
mockScope.filters = filters;
|
|
|
|
|
|
|
|
filteredRows = controller.filterRows(testRows);
|
|
|
|
expect(filteredRows.length).toBe(3);
|
|
|
|
filters.col1 = 'row1';
|
|
|
|
filteredRows = controller.filterRows(testRows);
|
|
|
|
expect(filteredRows.length).toBe(1);
|
|
|
|
filters.col1 = 'match';
|
|
|
|
filteredRows = controller.filterRows(testRows);
|
|
|
|
expect(filteredRows.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Sets rows on scope when rows change', function() {
|
|
|
|
controller.updateRows(testRows);
|
|
|
|
expect(mockScope.displayRows.length).toBe(3);
|
|
|
|
expect(mockScope.displayRows).toEqual(testRows);
|
|
|
|
});
|
|
|
|
|
2016-03-09 05:36:33 +00:00
|
|
|
it('Supports adding rows individually', function() {
|
2016-03-17 01:26:14 +00:00
|
|
|
var addRowFunc = mockScope.$on.calls[mockScope.$on.calls.length-2].args[1],
|
2016-03-09 05:36:33 +00:00
|
|
|
row4 = {
|
|
|
|
'col1': {'text': 'row3 col1'},
|
|
|
|
'col2': {'text': 'ghi'},
|
|
|
|
'col3': {'text': 'row3 col3'}
|
|
|
|
};
|
|
|
|
controller.updateRows(testRows);
|
|
|
|
expect(mockScope.displayRows.length).toBe(3);
|
2016-03-14 02:37:08 +00:00
|
|
|
testRows.push(row4);
|
2016-03-17 01:26:14 +00:00
|
|
|
addRowFunc(undefined, 3);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows.length).toBe(4);
|
|
|
|
});
|
|
|
|
|
2016-03-17 01:26:14 +00:00
|
|
|
it('Supports removing rows individually', function() {
|
|
|
|
var removeRowFunc = mockScope.$on.calls[mockScope.$on.calls.length-1].args[1];
|
|
|
|
controller.updateRows(testRows);
|
|
|
|
expect(mockScope.displayRows.length).toBe(3);
|
|
|
|
spyOn(controller, 'setVisibleRows');
|
|
|
|
//controller.setVisibleRows.andReturn(undefined);
|
|
|
|
removeRowFunc(undefined, 2);
|
|
|
|
expect(mockScope.displayRows.length).toBe(2);
|
|
|
|
expect(controller.setVisibleRows).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2016-02-26 19:09:51 +00:00
|
|
|
describe('sorting', function() {
|
|
|
|
var sortedRows;
|
|
|
|
|
|
|
|
it('Sorts rows ascending', function() {
|
|
|
|
mockScope.sortColumn = 'col1';
|
|
|
|
mockScope.sortDirection = 'asc';
|
|
|
|
|
|
|
|
sortedRows = controller.sortRows(testRows);
|
|
|
|
expect(sortedRows[0].col1.text).toEqual('row1 col1 match');
|
|
|
|
expect(sortedRows[1].col1.text).toEqual('row2 col1' +
|
|
|
|
' match');
|
|
|
|
expect(sortedRows[2].col1.text).toEqual('row3 col1');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Sorts rows descending', function() {
|
|
|
|
mockScope.sortColumn = 'col1';
|
|
|
|
mockScope.sortDirection = 'desc';
|
|
|
|
|
|
|
|
sortedRows = controller.sortRows(testRows);
|
|
|
|
expect(sortedRows[0].col1.text).toEqual('row3 col1');
|
|
|
|
expect(sortedRows[1].col1.text).toEqual('row2 col1 match');
|
|
|
|
expect(sortedRows[2].col1.text).toEqual('row1 col1 match');
|
|
|
|
});
|
|
|
|
it('Sorts rows descending based on selected sort column', function() {
|
|
|
|
mockScope.sortColumn = 'col2';
|
|
|
|
mockScope.sortDirection = 'desc';
|
|
|
|
|
|
|
|
sortedRows = controller.sortRows(testRows);
|
|
|
|
expect(sortedRows[0].col2.text).toEqual('ghi');
|
|
|
|
expect(sortedRows[1].col2.text).toEqual('def');
|
|
|
|
expect(sortedRows[2].col2.text).toEqual('abc');
|
|
|
|
});
|
2016-03-09 05:36:33 +00:00
|
|
|
|
|
|
|
describe('Adding new rows', function() {
|
|
|
|
var row4,
|
|
|
|
row5,
|
|
|
|
row6;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
row4 = {
|
|
|
|
'col1': {'text': 'row5 col1'},
|
|
|
|
'col2': {'text': 'xyz'},
|
|
|
|
'col3': {'text': 'row5 col3'}
|
|
|
|
};
|
|
|
|
row5 = {
|
|
|
|
'col1': {'text': 'row6 col1'},
|
|
|
|
'col2': {'text': 'aaa'},
|
|
|
|
'col3': {'text': 'row6 col3'}
|
|
|
|
};
|
|
|
|
row6 = {
|
|
|
|
'col1': {'text': 'row6 col1'},
|
|
|
|
'col2': {'text': 'ggg'},
|
|
|
|
'col3': {'text': 'row6 col3'}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Adds new rows at the correct sort position when' +
|
|
|
|
' sorted ', function() {
|
|
|
|
mockScope.sortColumn = 'col2';
|
|
|
|
mockScope.sortDirection = 'desc';
|
|
|
|
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.displayRows = controller.sortRows(testRows.slice(0));
|
2016-03-09 05:36:33 +00:00
|
|
|
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.rows.push(row4);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows[0].col2.text).toEqual('xyz');
|
2016-03-14 02:37:08 +00:00
|
|
|
|
|
|
|
mockScope.rows.push(row5);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows[4].col2.text).toEqual('aaa');
|
2016-03-14 02:37:08 +00:00
|
|
|
|
|
|
|
mockScope.rows.push(row6);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows[2].col2.text).toEqual('ggg');
|
|
|
|
|
|
|
|
//Add a duplicate row
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.rows.push(row6);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows[2].col2.text).toEqual('ggg');
|
|
|
|
expect(mockScope.displayRows[3].col2.text).toEqual('ggg');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Adds new rows at the correct sort position when' +
|
|
|
|
' sorted and filtered', function() {
|
|
|
|
mockScope.sortColumn = 'col2';
|
|
|
|
mockScope.sortDirection = 'desc';
|
|
|
|
mockScope.filters = {'col2': 'a'};//Include only
|
|
|
|
// rows with 'a'
|
|
|
|
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.displayRows = controller.sortRows(testRows.slice(0));
|
2016-03-09 05:36:33 +00:00
|
|
|
mockScope.displayRows = controller.filterRows(testRows);
|
|
|
|
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.rows.push(row5);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows.length).toBe(2);
|
|
|
|
expect(mockScope.displayRows[1].col2.text).toEqual('aaa');
|
|
|
|
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.rows.push(row6);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows.length).toBe(2);
|
|
|
|
//Row was not added because does not match filter
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Adds new rows at the correct sort position when' +
|
|
|
|
' not sorted ', function() {
|
|
|
|
mockScope.sortColumn = undefined;
|
|
|
|
mockScope.sortDirection = undefined;
|
|
|
|
mockScope.filters = {};
|
|
|
|
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.displayRows = testRows.slice(0);
|
2016-03-09 05:36:33 +00:00
|
|
|
|
2016-03-14 02:37:08 +00:00
|
|
|
mockScope.rows.push(row5);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows[3].col2.text).toEqual('aaa');
|
2016-03-14 02:37:08 +00:00
|
|
|
|
|
|
|
mockScope.rows.push(row6);
|
|
|
|
controller.newRow(undefined, mockScope.rows.length-1);
|
2016-03-09 05:36:33 +00:00
|
|
|
expect(mockScope.displayRows[4].col2.text).toEqual('ggg');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2016-02-26 19:09:51 +00:00
|
|
|
});
|
2016-03-09 05:36:33 +00:00
|
|
|
|
|
|
|
|
2016-02-26 19:09:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|