Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ const fn = mock.get({
})
```

#### `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`

Returns a custom `Connection` class that you **must** pass to the Elasticsearch client instance.
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ declare class ClientMock {
constructor()
add(pattern: MockPattern, resolver: ResolverFn): ClientMock
get(pattern: MockPattern): ResolverFn | null
clear(pattern: Pick<MockPattern, 'method' | 'path'>): void
clearAll(): void
getConnection(): typeof Connection
}

Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ class Mocker {
return null
}

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)
}

clearAll () {
this[kRouter].reset()
}

getConnection () {
return buildConnectionClass(this)
}
Expand Down
100 changes: 100 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,103 @@ test('ndjson API support (as stream with compression)', async t => {
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})

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)
}
})