From 866f5948afeb87000470f51d0b4b97aaaf181a27 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Mon, 27 Jun 2016 14:23:49 -0700 Subject: [PATCH] 100 percent coverage on create_index helper --- test/src/helpers/create_index.js | 118 +++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 38 deletions(-) diff --git a/test/src/helpers/create_index.js b/test/src/helpers/create_index.js index a0dfc12..e722804 100644 --- a/test/src/helpers/create_index.js +++ b/test/src/helpers/create_index.js @@ -5,54 +5,96 @@ import elasticsearchMock from '../../fixtures/elasticsearch'; import { defaultSettings } from '../../../lib/helpers/constants'; describe('Create Index', function () { - let client; - let createSpy; - beforeEach(function () { - client = new elasticsearchMock.Client(); - createSpy = sinon.spy(client.indices, 'create'); - }); + describe('Does not exist', function () { + let client; + let createSpy; - it('should create the index', function () { - const indexName = 'test-index'; - const result = createIndex(client, indexName); + beforeEach(function () { + client = new elasticsearchMock.Client(); + createSpy = sinon.spy(client.indices, 'create'); + }); - return result - .then(function () { - sinon.assert.callCount(createSpy, 1); - expect(createSpy.getCall(0).args[0]).to.have.property('index', indexName); + it('should return true', function () { + const indexName = 'test-index'; + const result = createIndex(client, indexName); + + return result + .then((exists) => expect(exists).to.be(true)); + }); + + it('should create the index', function () { + const indexName = 'test-index'; + const result = createIndex(client, indexName); + + return result + .then(function () { + sinon.assert.callCount(createSpy, 1); + expect(createSpy.getCall(0).args[0]).to.have.property('index', indexName); + }); + }); + + it('should create the type mappings', function () { + const indexName = 'test-index'; + const docType = defaultSettings.DEFAULT_SETTING_DOCTYPE; + const result = createIndex(client, indexName); + + return result + .then(function () { + const payload = createSpy.getCall(0).args[0]; + sinon.assert.callCount(createSpy, 1); + expect(payload).to.have.property('body'); + expect(payload.body).to.have.property('mappings'); + expect(payload.body.mappings).to.have.property(docType); + expect(payload.body.mappings[docType]).to.have.property('properties'); + }); + }); + + it('should accept a custom doctype', function () { + const indexName = 'test-index'; + const docType = 'my_type'; + const result = createIndex(client, indexName, docType); + + return result + .then(function () { + const payload = createSpy.getCall(0).args[0]; + sinon.assert.callCount(createSpy, 1); + expect(payload).to.have.property('body'); + expect(payload.body).to.have.property('mappings'); + expect(payload.body.mappings).to.have.property(docType); + expect(payload.body.mappings[docType]).to.have.property('properties'); + }); }); }); - it('should create the type mappings', function () { - const indexName = 'test-index'; - const docType = defaultSettings.DEFAULT_SETTING_DOCTYPE; - const result = createIndex(client, indexName); + describe('Does exist', function () { + let client; + let existsStub; + let createSpy; - return result - .then(function () { - const payload = createSpy.getCall(0).args[0]; - sinon.assert.callCount(createSpy, 1); - expect(payload).to.have.property('body'); - expect(payload.body).to.have.property('mappings'); - expect(payload.body.mappings).to.have.property(docType); - expect(payload.body.mappings[docType]).to.have.property('properties'); + beforeEach(function () { + client = new elasticsearchMock.Client(); + existsStub = sinon.stub(client.indices, 'exists', () => Promise.resolve(true)); + createSpy = sinon.spy(client.indices, 'create'); }); - }); - it('should accept a custom doctype', function () { - const indexName = 'test-index'; - const docType = 'my_type'; - const result = createIndex(client, indexName, docType); + it('should return true', function () { + const indexName = 'test-index'; + const result = createIndex(client, indexName); - return result - .then(function () { - const payload = createSpy.getCall(0).args[0]; - sinon.assert.callCount(createSpy, 1); - expect(payload).to.have.property('body'); - expect(payload.body).to.have.property('mappings'); - expect(payload.body.mappings).to.have.property(docType); - expect(payload.body.mappings[docType]).to.have.property('properties'); + return result + .then((exists) => expect(exists).to.be(true)); }); + + it('should not create the index', function () { + const indexName = 'test-index'; + const result = createIndex(client, indexName); + + return result + .then(function () { + sinon.assert.callCount(createSpy, 0); + }); + }); + }); });