rename create client, fix client instance test
This commit is contained in:
@@ -3,7 +3,8 @@ import elasticsearch from 'elasticsearch';
|
|||||||
export default function createClient(options) {
|
export default function createClient(options) {
|
||||||
let client;
|
let client;
|
||||||
|
|
||||||
if (options instanceof elasticsearch.Client) {
|
// if there's a transport property, assume it's a client instance
|
||||||
|
if (isClient(options)) {
|
||||||
client = options;
|
client = options;
|
||||||
} else {
|
} else {
|
||||||
client = new elasticsearch.Client(options);
|
client = new elasticsearch.Client(options);
|
||||||
@@ -11,3 +12,7 @@ export default function createClient(options) {
|
|||||||
|
|
||||||
return client;
|
return client;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function isClient(client) {
|
||||||
|
return !!client.transport;
|
||||||
|
}
|
||||||
@@ -2,24 +2,25 @@ import expect from 'expect.js';
|
|||||||
import proxyquire from 'proxyquire';
|
import proxyquire from 'proxyquire';
|
||||||
import * as elasticsearchMock from '../../fixtures/elasticsearch';
|
import * as elasticsearchMock from '../../fixtures/elasticsearch';
|
||||||
|
|
||||||
const createClient = proxyquire.noPreserveCache()('../../../lib/helpers/create_client', {
|
const module = proxyquire.noPreserveCache()('../../../lib/helpers/es_client', {
|
||||||
elasticsearch: elasticsearchMock
|
elasticsearch: elasticsearchMock
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const createClient = module.default;
|
||||||
|
const { isClient } = module;
|
||||||
|
|
||||||
describe('Create client helper', function () {
|
describe('Create client helper', function () {
|
||||||
it('should have a client', function () {
|
it('should have a client', function () {
|
||||||
const options = {
|
const options = {
|
||||||
host: 'http://localhost:9200'
|
host: 'http://localhost:9200'
|
||||||
};
|
};
|
||||||
const client = createClient(options);
|
const client = createClient(options);
|
||||||
|
expect(isClient(client)).to.be.ok();
|
||||||
expect(client).to.be.a(elasticsearchMock.Client);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use passed in instance', function () {
|
it('should use passed in instance', function () {
|
||||||
const clientInstance = new elasticsearchMock.Client();
|
const clientInstance = new elasticsearchMock.Client();
|
||||||
const client = createClient(clientInstance);
|
const client = createClient(clientInstance);
|
||||||
|
|
||||||
expect(client).to.equal(clientInstance);
|
expect(client).to.equal(clientInstance);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user