@elastic/elasticsearch-mock
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -11,2 +11,4 @@ // Licensed to Elasticsearch B.V under one or more agreements. | ||
get(pattern: MockPattern): ResolverFn | null | ||
clear(pattern: Pick<MockPattern, 'method' | 'path'>): ClientMock | ||
clearAll(): ClientMock | ||
getConnection(): typeof Connection | ||
@@ -13,0 +15,0 @@ } |
22
index.js
@@ -79,2 +79,24 @@ // Licensed to Elasticsearch B.V under one or more agreements. | ||
clear (pattern) { | ||
for (const key of ['method', 'path']) { | ||
if (Array.isArray(pattern[key])) { | ||
for (const value of pattern[key]) { | ||
this.clear({ ...pattern, [key]: value }) | ||
} | ||
return this | ||
} | ||
} | ||
if (typeof pattern.method !== 'string') throw new ConfigurationError('The method is not defined') | ||
if (typeof pattern.path !== 'string') throw new ConfigurationError('The path is not defined') | ||
this[kRouter].off(pattern.method, pattern.path) | ||
return this | ||
} | ||
clearAll () { | ||
this[kRouter].reset() | ||
return this | ||
} | ||
getConnection () { | ||
@@ -81,0 +103,0 @@ return buildConnectionClass(this) |
{ | ||
"name": "@elastic/elasticsearch-mock", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Mock utility for the Elasticsearch's Node.js client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -89,2 +89,21 @@ <img align="right" width="auto" height="auto" src="https://www.elastic.co/static-res/images/elastic-logo-200.png"> | ||
#### `clear` | ||
Clears/removes mocks for specific route(s). | ||
```js | ||
mock.clear({ | ||
method: ['GET'], | ||
path: ['/_search', '/:index/_search'] | ||
}) | ||
``` | ||
#### `clearAll` | ||
Clears all mocks. | ||
```js | ||
mock.clearAll() | ||
``` | ||
#### `getConnection` | ||
@@ -91,0 +110,0 @@ |
100
test.js
@@ -781,1 +781,101 @@ // Licensed to Elasticsearch B.V under one or more agreements. | ||
}) | ||
test('Should clear individual mocks', async t => { | ||
const mock = new Mock() | ||
const client = new Client({ | ||
node: 'http://localhost:9200', | ||
Connection: mock.getConnection() | ||
}) | ||
mock.add({ | ||
method: 'GET', | ||
path: ['/test1/_search', '/test2/_search'] | ||
}, () => { | ||
return { status: 'ok' } | ||
}) | ||
// Clear test1 but not test2 | ||
mock.clear({ method: 'GET', path: ['/test1/_search'] }) | ||
// test2 still works | ||
const response = await client.search({ | ||
index: 'test2', | ||
q: 'foo:bar' | ||
}) | ||
t.deepEqual(response.body, { status: 'ok' }) | ||
t.is(response.statusCode, 200) | ||
// test1 does not | ||
try { | ||
await client.search({ | ||
index: 'test1', | ||
q: 'foo:bar' | ||
}) | ||
t.fail('Should throw') | ||
} catch (err) { | ||
t.true(err instanceof errors.ResponseError) | ||
t.deepEqual(err.body, { error: 'Mock not found' }) | ||
t.is(err.statusCode, 404) | ||
} | ||
}) | ||
test('.mock should throw if method and path are not defined', async t => { | ||
const mock = new Mock() | ||
try { | ||
mock.clear({ path: '/' }, () => {}) | ||
t.fail('Should throw') | ||
} catch (err) { | ||
t.true(err instanceof errors.ConfigurationError) | ||
t.is(err.message, 'The method is not defined') | ||
} | ||
try { | ||
mock.clear({ method: 'GET' }, () => {}) | ||
t.fail('Should throw') | ||
} catch (err) { | ||
t.true(err instanceof errors.ConfigurationError) | ||
t.is(err.message, 'The path is not defined') | ||
} | ||
}) | ||
test('Should clear all mocks', async t => { | ||
const mock = new Mock() | ||
const client = new Client({ | ||
node: 'http://localhost:9200', | ||
Connection: mock.getConnection() | ||
}) | ||
mock.add({ | ||
method: 'GET', | ||
path: ['/test1/_search', '/test2/_search'] | ||
}, () => { | ||
return { status: 'ok' } | ||
}) | ||
// Clear mocks | ||
mock.clearAll() | ||
try { | ||
await client.search({ | ||
index: 'test1', | ||
q: 'foo:bar' | ||
}) | ||
t.fail('Should throw') | ||
} catch (err) { | ||
t.true(err instanceof errors.ResponseError) | ||
t.deepEqual(err.body, { error: 'Mock not found' }) | ||
t.is(err.statusCode, 404) | ||
} | ||
try { | ||
await client.search({ | ||
index: 'test2', | ||
q: 'foo:bar' | ||
}) | ||
t.fail('Should throw') | ||
} catch (err) { | ||
t.true(err instanceof errors.ResponseError) | ||
t.deepEqual(err.body, { error: 'Mock not found' }) | ||
t.is(err.statusCode, 404) | ||
} | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47953
1130
242