From a6fd8f091bb77a69e803f6263a3ba489be69afbc Mon Sep 17 00:00:00 2001 From: Stacey Gammon Date: Thu, 17 Nov 2016 12:06:59 -0500 Subject: [PATCH] Create tests for savedobject.js in prep of refactoring and changes --- src/test_utils/stub_mapper.js | 17 ++ .../public/courier/__tests__/saved_object.js | 213 ++++++++++++++++++ .../__tests__/_index_pattern.js | 12 +- src/ui/public/promises/__tests__/promises.js | 6 + 4 files changed, 238 insertions(+), 10 deletions(-) create mode 100644 src/test_utils/stub_mapper.js create mode 100644 src/ui/public/courier/__tests__/saved_object.js diff --git a/src/test_utils/stub_mapper.js b/src/test_utils/stub_mapper.js new file mode 100644 index 0000000000000..6536f20e32ad9 --- /dev/null +++ b/src/test_utils/stub_mapper.js @@ -0,0 +1,17 @@ +import MapperService from 'ui/index_patterns/_mapper'; +import stubbedLogstashFields from 'fixtures/logstash_fields'; +import sinon from 'auto-release-sinon'; + +export function stubMapper(Private, mockLogstashFields = Private(stubbedLogstashFields)) { + let stubbedMapper = Private(MapperService); + + sinon.stub(stubbedMapper, 'getFieldsForIndexPattern', function () { + return Promise.resolve(mockLogstashFields.filter((field) => { return field.scripted === false; })); + }); + + sinon.stub(stubbedMapper, 'clearCache', function () { + return Promise.resolve(); + }); + + return stubbedMapper; +} diff --git a/src/ui/public/courier/__tests__/saved_object.js b/src/ui/public/courier/__tests__/saved_object.js new file mode 100644 index 0000000000000..2ecf436b30fe6 --- /dev/null +++ b/src/ui/public/courier/__tests__/saved_object.js @@ -0,0 +1,213 @@ +/** + * Tests functionality in ui/public/courier/saved_object/saved_object.js + */ + +import ngMock from 'ng_mock'; +import expect from 'expect.js'; +import sinon from 'auto-release-sinon'; + +import BluebirdPromise from 'bluebird'; +import SavedObjectFactory from '../saved_object/saved_object'; +import { stubMapper } from 'test_utils/stub_mapper'; +import IndexPatternFactory from 'ui/index_patterns/_index_pattern'; + +describe('Saved Object', function () { + require('test_utils/no_digest_promises').activateForSuite(); + + let savedObjectFactory; + let IndexPattern; + let esStub; + + beforeEach(ngMock.module('kibana')); + beforeEach(ngMock.inject(function ($injector, Private) { + savedObjectFactory = Private(SavedObjectFactory); + IndexPattern = Private(IndexPatternFactory); + esStub = $injector.get('es'); + + mockEsService(); + stubMapper(Private); + })); + + /** + * Some default es stubbing to avoid timeouts and allow a default type of 'dashboard'. + */ + function mockEsService() { + // Allows the type 'dashboard' to be used. + // Unfortunately we need to use bluebird here instead of native promises because there is + // a call to finally. + sinon.stub(esStub.indices, 'getFieldMapping').returns(BluebirdPromise.resolve({ + '.kibana' : { + 'mappings': { + 'dashboard': {} + } + } + })); + + // Necessary to avoid a timeout condition. + sinon.stub(esStub.indices, 'putMapping').returns(BluebirdPromise.resolve()); + }; + + /** + * Stubs some of the es retrieval calls so it returns the given response. + * @param {Object} mockDocResponse + */ + function stubESResponse(mockDocResponse) { + sinon.stub(esStub, 'mget').returns(BluebirdPromise.resolve({ docs: [mockDocResponse] })); + sinon.stub(esStub, 'index').returns(BluebirdPromise.resolve(mockDocResponse)); + } + + /** + * Creates a new saved object with the given configuration. Does not call init. + * @param {Object} config + * @returns {SavedObject} an instance of SavedObject + */ + function createSavedObject(config = {}) { + let savedObject = {}; + savedObjectFactory.call(savedObject, config); + return savedObject; + } + + describe ('config', function () { + + it('afterESResp is called', function () { + let afterESRespCallback = sinon.spy(); + const config = { + type: 'dashboard', + afterESResp: afterESRespCallback + }; + + let savedObject = createSavedObject(config); + return savedObject.init().then(() => { + expect(afterESRespCallback.called).to.be(true); + }); + }); + + it('init is called', function () { + let initCallback = sinon.spy(); + const config = { + type: 'dashboard', + init: initCallback + }; + + let savedObject = createSavedObject(config); + return savedObject.init().then(() => { + expect(initCallback.called).to.be(true); + }); + }); + + it('searchSource creates index when true', function () { + const indexPatternId = 'testIndexPattern'; + let afterESRespCallback = sinon.spy(); + + const config = { + type: 'dashboard', + afterESResp: afterESRespCallback, + searchSource: true, + indexPattern: indexPatternId + }; + + const mockDocResponse = { + _source: {}, + _index: indexPatternId, + _type: 'test-type', + _id: indexPatternId, + found: true + }; + + stubESResponse(mockDocResponse); + + let savedObject = createSavedObject(config); + expect(!!savedObject.searchSource.get('index')).to.be(false); + + return savedObject.init().then(() => { + expect(afterESRespCallback.called).to.be(true); + const index = savedObject.searchSource.get('index'); + expect(index instanceof IndexPattern).to.be(true); + expect(index.id).to.equal(indexPatternId); + }); + }); + + describe('type', function () { + it('that is not specified throws an error', function () { + let config = {}; + + let savedObject = createSavedObject(config); + try { + savedObject.init(); + expect(false).to.be(true); + } catch (err) { + expect(err).to.not.be(null); + } + }); + + it('that is invalid invalid throws an error', function () { + let config = {type: 'notypeexists'}; + + let savedObject = createSavedObject(config); + try { + savedObject.init(); + expect(false).to.be(true); + } catch (err) { + expect(err).to.not.be(null); + } + }); + + it('that is valid passes', function () { + let config = {type: 'dashboard'}; + return createSavedObject(config).init(); + }); + }); + + describe('defaults', function () { + it('applied to object with no id', function () { + let config = { + defaults: {testDefault: 'hi'}, + type: 'dashboard' + }; + + let savedObject = createSavedObject(config); + return savedObject.init().then(() => { + expect(savedObject.searchSource).to.be(undefined); + expect(savedObject.defaults.testDefault).to.be(config.defaults.testDefault); + }); + }); + + it('applied to source if an id is given', function () { + const myId = 'myid'; + const customDefault = 'hi'; + const initialOverwriteMeValue = 'this should get overwritten by the server response'; + + let config = { + defaults: { + overwriteMe: initialOverwriteMeValue, + customDefault: customDefault + }, + type: 'dashboard', + id: myId + }; + + const serverValue = 'this should override the initial default value given'; + + const mockDocResponse = { + _source: { + overwriteMe: serverValue + }, + _index: myId, + _type: 'dashboard', + _id: myId, + found: true + }; + + stubESResponse(mockDocResponse); + + let savedObject = createSavedObject(config); + return savedObject.init().then(() => { + expect(!!savedObject._source).to.be(true); + expect(savedObject.defaults.overwriteMe).to.be(initialOverwriteMeValue); + expect(savedObject._source.overwriteMe).to.be(serverValue); + expect(savedObject._source.customDefault).to.be(customDefault); + }); + }); + }); + }); +}); diff --git a/src/ui/public/index_patterns/__tests__/_index_pattern.js b/src/ui/public/index_patterns/__tests__/_index_pattern.js index 98b26aea61b06..5acab51892973 100644 --- a/src/ui/public/index_patterns/__tests__/_index_pattern.js +++ b/src/ui/public/index_patterns/__tests__/_index_pattern.js @@ -8,11 +8,11 @@ import IndexedArray from 'ui/indexed_array'; import FixturesLogstashFieldsProvider from 'fixtures/logstash_fields'; import FixturesStubbedDocSourceResponseProvider from 'fixtures/stubbed_doc_source_response'; import DocSourceProvider from 'ui/courier/data_source/doc_source'; -import IndexPatternsMapperProvider from 'ui/index_patterns/_mapper'; import UtilsMappingSetupProvider from 'ui/utils/mapping_setup'; import IndexPatternsIntervalsProvider from 'ui/index_patterns/_intervals'; import IndexPatternsIndexPatternProvider from 'ui/index_patterns/_index_pattern'; import NoDigestPromises from 'test_utils/no_digest_promises'; +import { stubMapper } from 'test_utils/stub_mapper'; describe('index pattern', function () { NoDigestPromises.activateForSuite(); @@ -40,15 +40,7 @@ describe('index pattern', function () { DocSource = Private(DocSourceProvider); sinon.stub(DocSource.prototype, 'doIndex'); sinon.stub(DocSource.prototype, 'fetch'); - - // stub mapper - mapper = Private(IndexPatternsMapperProvider); - sinon.stub(mapper, 'getFieldsForIndexPattern', function () { - return Promise.resolve(_.filter(mockLogstashFields, { scripted: false })); - }); - sinon.stub(mapper, 'clearCache', function () { - return Promise.resolve(); - }); + mapper = stubMapper(Private, mockLogstashFields); // stub mappingSetup mappingSetup = Private(UtilsMappingSetupProvider); diff --git a/src/ui/public/promises/__tests__/promises.js b/src/ui/public/promises/__tests__/promises.js index 9ded576e76cf3..3cbbe45be80c4 100644 --- a/src/ui/public/promises/__tests__/promises.js +++ b/src/ui/public/promises/__tests__/promises.js @@ -22,6 +22,12 @@ describe('Promise service', function () { }); }); + it('Promise.resolve', function (done) { + Promise.resolve(true).then(() => { done(); }); + // Ugly, but necessary for promises to resolve: https://github.com/angular/angular.js/issues/12555 + $rootScope.$apply(); + }); + describe('Promise.fromNode', function () { it('creates a callback that controls a promise', function () { let callback;