Added tests for patternChecker directive

This commit is contained in:
Matthew Bargar 2016-03-21 13:36:15 -04:00
parent f83c4fdbff
commit 615560f0db
2 changed files with 95 additions and 1 deletions

View file

@ -0,0 +1,95 @@
import ngMock from 'ng_mock';
import expect from 'expect.js';
import _ from 'lodash';
import sinon from 'auto-release-sinon';
describe('pattern checker', function () {
let $httpBackend;
let $compile;
let $rootScope;
let apiResponse;
let $timeout;
const notifyFatalStub = sinon.stub();
beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.module(function ($provide) {
notifyFatalStub.reset();
$provide.value('Notifier', function () {
this.fatal = notifyFatalStub;
});
}));
beforeEach(ngMock.inject(function ($injector, Private) {
$httpBackend = $injector.get('$httpBackend');
$compile = $injector.get('$compile');
$rootScope = $injector.get('$rootScope');
$timeout = $injector.get('$timeout');
apiResponse = $httpBackend.when('POST', /\/api\/kibana\/.*\/_count/);
}));
it('should display the number of documents in a given index pattern', function () {
apiResponse.respond(200, {count: 1});
const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope);
$httpBackend.flush();
$rootScope.$digest();
expect(_.contains(element.html(), `1 results`)).to.be.ok();
});
it('should poll the api for changes to the document count and update the ui', function () {
apiResponse.respond(200, {count: 1});
const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope);
$httpBackend.flush();
$rootScope.$digest();
expect(_.contains(element.html(), `1 results`)).to.be.ok();
apiResponse.respond(200, {count: 2});
$timeout.flush();
$httpBackend.flush();
$rootScope.$digest();
expect(_.contains(element.html(), `2 results`)).to.be.ok();
});
it('should display 0 results when API responds with 404', function () {
apiResponse.respond(404);
const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope);
$httpBackend.flush();
$rootScope.$digest();
expect(_.contains(element.html(), `0 results`)).to.be.ok();
});
it('should throw a fatal notificaiton for any error other than a 404', function () {
apiResponse.respond(500, 'Bad things happened');
const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope);
$httpBackend.flush();
$rootScope.$digest();
expect(notifyFatalStub.called).to.be.ok();
});
it('should stop polling when the scope is destroyed', function () {
apiResponse.respond(200, {count: 1});
const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope);
const scope = element.scope();
$httpBackend.flush();
$rootScope.$digest();
expect(_.contains(element.html(), `1 results`)).to.be.ok();
scope.$destroy();
$timeout.flush();
$httpBackend.verifyNoOutstandingRequest();
});
});

View file

@ -26,7 +26,6 @@ module.directive('patternChecker', function () {
.then(
(response) => {
this.resultCount = response.data.count;
this.isValidated = !!response.data.count;
},
(error) => {
if (error.status === 404) {