automatically mock angular in the tests

added SearchSource#getNormalizedSort() method

Closes #217
This commit is contained in:
Spencer Alger 2014-08-01 12:28:21 -07:00 committed by Rashid Khan
parent 9fce04d47a
commit e46b6b488d
15 changed files with 127 additions and 15 deletions

View file

@ -91,6 +91,37 @@ define(function (require) {
this._fetchDisabled = false;
};
/**
* Special reader function for sort, which will transform the sort syntax into a simple
* map of `field: dir`
*/
SearchSource.prototype.getNormalizedSort = function () {
var sort = this.get('sort');
if (!sort) return;
var normal = {};
(function read(lvl) {
if (_.isString(lvl)) {
normal[lvl] = 'asc';
}
else if (_.isArray(lvl)) {
_.forEach(lvl, read);
}
else if (_.isObject(lvl)) {
_.forOwn(lvl, function (dir, field) {
if (_.isObject(dir)) {
normal[field] = dir.dir || 'asc';
} else {
normal[field] = String(dir);
}
});
}
}(sort));
return normal;
};
/******
* PRIVATE APIS
******/

View file

@ -12,7 +12,6 @@ require.config({
// bower_components
angular: '../bower_components/angular/angular',
'angular-mocks': '../bower_components/angular-mocks/angular-mocks',
'angular-route': '../bower_components/angular-route/angular-route',
'angular-bootstrap': '../bower_components/angular-bootstrap/ui-bootstrap-tpls',
'angular-bindonce': '../bower_components/angular-bindonce/bindonce',
@ -43,7 +42,6 @@ require.config({
},
gridster: ['jquery'],
'angular-route': ['angular'],
'angular-mocks': ['angular'],
'elasticsearch': ['angular'],
'angular-bootstrap': ['angular'],
'angular-bindonce': ['angular'],

View file

@ -22,9 +22,18 @@
fixtures: '../../test/unit/fixtures',
specs: '../../test/unit/specs',
sinon: '../../test/utils/sinon',
bluebird: '../bower_components/bluebird/js/browser/bluebird'
bluebird: '../bower_components/bluebird/js/browser/bluebird',
angular: '../bower_components/angular-mocks/angular-mocks',
angular_src: '../bower_components/angular/angular'
},
shim: {
angular: {
deps: [
'jquery',
'angular_src'
],
exports: 'angular'
},
'sinon/sinon': {
deps: [
'sinon/sinon-timers-1.8.2'
@ -63,7 +72,8 @@
'specs/utils/datemath',
'specs/utils/interval',
'specs/utils/versionmath',
'specs/utils/routes/index'
'specs/utils/routes/index',
'specs/courier/search_source/_get_normalized_sort'
], function (kibana, sinon) {
kibana.load(function () {
var xhr = sinon.useFakeXMLHttpRequest();

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var _ = require('lodash');
var $ = require('jquery');

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var _ = require('lodash');
var $ = require('jquery');

View file

@ -1,5 +1,5 @@
define(function (require) {
var mocks = require('angular-mocks');
var angular = require('angular');
var _ = require('lodash');
var $ = require('jquery');
var sinon = require('test_utils/auto_release_sinon');

View file

@ -0,0 +1,83 @@
define(function (require) {
describe('SearchSource#getNormalizedSort', function () {
require('services/private');
require('angular').module('getNormalizedSort', ['kibana']);
var source;
beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
var SearchSource = Private(require('components/courier/data_source/search_source'));
source = new SearchSource();
}));
[
{
sort: {
starCount: 'asc'
},
normal: {
starCount: 'asc'
}
},
{
sort: {
starCount: 'desc'
},
normal: {
starCount: 'desc'
}
},
{
sort: 'starCount',
normal: {
starCount: 'asc'
}
},
{
sort: [
'starCount'
],
normal: {
starCount: 'asc'
}
},
{
sort: [
'starCount',
'name'
],
normal: {
starCount: 'asc',
name: 'asc'
}
},
{
sort: [
{ name: 'asc' }
],
normal: {
name: 'asc'
}
},
{
sort: [
{ starCount: 'desc' },
{ name: 'asc' }
],
normal: {
starCount: 'desc',
name: 'asc'
}
}
].forEach(function (test) {
it('reads ' + JSON.stringify(test.sort) + ' properly', function () {
source.sort(test.sort);
var normal = source.getNormalizedSort();
expect(normal).to.eql(test.normal);
});
});
});
});

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var $ = require('jquery');
// Load the kibana app dependencies.

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var $ = require('jquery');
// Load the kibana app dependencies.

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var moment = require('moment');
var _ = require('lodash');
var $ = require('jquery');

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var $ = require('jquery');
// Load the kibana app dependencies.

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var _ = require('lodash');
// Load the kibana app dependencies.

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var sinon = require('sinon/sinon');
var moment = require('moment');

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
// Load the kibana app dependencies.
require('angular-route');

View file

@ -1,6 +1,5 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var _ = require('lodash');
// Load the kibana app dependencies.