adding tests
This commit is contained in:
parent
242fe85c9e
commit
420c1bcf77
|
@ -3,28 +3,127 @@ import expect from 'expect.js';
|
|||
import ngMock from 'ng_mock';
|
||||
import _ from 'lodash';
|
||||
|
||||
var list = [
|
||||
{ title: 'apple' },
|
||||
{ title: 'orange' },
|
||||
{ title: 'coconut' },
|
||||
{ title: 'banana' },
|
||||
{ title: 'grapes' }
|
||||
];
|
||||
var $scope;
|
||||
var $element;
|
||||
var $isolatedScope;
|
||||
|
||||
function test(val) {
|
||||
return val;
|
||||
}
|
||||
|
||||
var init = function (arr) {
|
||||
// Load the application
|
||||
ngMock.module('kibana');
|
||||
|
||||
// Create the scope
|
||||
ngMock.inject(function ($rootScope, $compile) {
|
||||
$scope = $rootScope.$new();
|
||||
$scope.perPage = 5;
|
||||
$scope.list = list;
|
||||
$scope.listProperty = 'title';
|
||||
$scope.test = test;
|
||||
|
||||
// Create the element
|
||||
$element = angular.element('<paginated-selectable-list perPage="perPage" list="list"' +
|
||||
'list-property="listProperty" user-make-url="test" user-on-select="test"></paginated-selectable-list>');
|
||||
|
||||
// And compile it
|
||||
$compile($element)($scope);
|
||||
|
||||
// Fire a digest cycle
|
||||
$element.scope().$digest();
|
||||
|
||||
// Grab the isolate scope so we can test it
|
||||
$isolatedScope = $element.isolateScope();
|
||||
});
|
||||
};
|
||||
|
||||
describe('paginatedSelectableList', function () {
|
||||
var list = [
|
||||
{ title: 'apple' },
|
||||
{ title: 'orange' },
|
||||
{ title: 'coconut' },
|
||||
{ title: 'banana' },
|
||||
{ title: 'grapes' }
|
||||
];
|
||||
var $controller;
|
||||
|
||||
beforeEach(ngMock.module('kibana'));
|
||||
beforeEach(ngMock.inject(function (_$controller_) {
|
||||
$controller = _$controller_;
|
||||
}));
|
||||
|
||||
describe('$scope.hits', function () {
|
||||
it('should sort and save the list to a hits array', function () {
|
||||
const $scope = { list: list, listProperty: 'title' };
|
||||
const controller = $controller('paginatedSelectableList', { $scope: $scope });
|
||||
beforeEach(function () {
|
||||
init(list);
|
||||
});
|
||||
|
||||
expect($scope.hits).to.be.an('array');
|
||||
expect($scope.hits).toEqual(_.sortBy(list, 'title'));
|
||||
it('should initially sort an array of objects in ascending order', function () {
|
||||
var property = $isolatedScope.listProperty;
|
||||
var sortedList = _.sortBy(list, property);
|
||||
|
||||
expect($isolatedScope.hits).to.be.an('array');
|
||||
|
||||
$isolatedScope.hits.forEach(function (hit, index) {
|
||||
expect(hit[property]).to.equal(sortedList[index][property]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('$scope.sortHits', function () {
|
||||
beforeEach(function () {
|
||||
init(list);
|
||||
});
|
||||
|
||||
it('should sort an array of objects in ascending order', function () {
|
||||
var property = $isolatedScope.listProperty;
|
||||
var sortedList = _.sortBy(list, property);
|
||||
|
||||
$isolatedScope.isAscending = false;
|
||||
$isolatedScope.sortHits(list);
|
||||
|
||||
expect($isolatedScope.isAscending).to.be(true);
|
||||
|
||||
$isolatedScope.hits.forEach(function (hit, index) {
|
||||
expect(hit[property]).to.equal(sortedList[index][property]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should sort an array of objects in descending order', function () {
|
||||
var property = $isolatedScope.listProperty;
|
||||
var reversedList = _.sortBy(list, property).reverse();
|
||||
|
||||
$isolatedScope.isAscending = true;
|
||||
$isolatedScope.sortHits(list);
|
||||
|
||||
expect($isolatedScope.isAscending).to.be(false);
|
||||
|
||||
$isolatedScope.hits.forEach(function (hit, index) {
|
||||
expect(hit[property]).to.equal(reversedList[index][property]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('$scope.makeUrl', function () {
|
||||
beforeEach(function () {
|
||||
init(list);
|
||||
});
|
||||
|
||||
it('should return the result of the function its passed', function () {
|
||||
var property = $isolatedScope.listProperty;
|
||||
var sortedList = _.sortBy(list, property);
|
||||
|
||||
$isolatedScope.hits.forEach(function (hit, index) {
|
||||
expect($isolatedScope.makeUrl(hit)[property]).to.equal(sortedList[index][property]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('$scope.onSelect', function () {
|
||||
beforeEach(function () {
|
||||
init(list);
|
||||
});
|
||||
|
||||
it('should return the result of the function its passed', function () {
|
||||
var property = $isolatedScope.listProperty;
|
||||
var sortedList = _.sortBy(list, property);
|
||||
|
||||
$isolatedScope.hits.forEach(function (hit, index) {
|
||||
expect($isolatedScope.onSelect(hit)[property]).to.equal(sortedList[index][property]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -9,11 +9,11 @@ module.directive('paginatedSelectableList', function (kbnUrl) {
|
|||
return {
|
||||
restrict: 'E',
|
||||
scope: {
|
||||
perPage: '=',
|
||||
perPage: '=?',
|
||||
list: '=',
|
||||
listProperty: '=',
|
||||
userMakeUrl: '=',
|
||||
userOnSelect: '='
|
||||
userMakeUrl: '=?',
|
||||
userOnSelect: '=?'
|
||||
},
|
||||
template: paginatedSelectableListTemplate,
|
||||
controller: function ($scope, $element, $filter) {
|
||||
|
@ -41,11 +41,15 @@ module.directive('paginatedSelectableList', function (kbnUrl) {
|
|||
};
|
||||
|
||||
$scope.makeUrl = function (hit) {
|
||||
return $scope.userMakeUrl(hit);
|
||||
if ($scope.userMakeUrl) {
|
||||
return $scope.userMakeUrl(hit);
|
||||
}
|
||||
};
|
||||
|
||||
$scope.onSelect = function (hit, $event) {
|
||||
return $scope.userOnSelect(hit, $event);
|
||||
if ($scope.userOnSelect) {
|
||||
return $scope.userOnSelect(hit, $event);
|
||||
}
|
||||
};
|
||||
|
||||
$scope.$watch('query', function (val) {
|
||||
|
|
Loading…
Reference in a new issue