Test: Add some basic tests for mongodb queries
This commit is contained in:
parent
c6b1ea9a0f
commit
4b48ea94e4
|
@ -5,11 +5,26 @@ module MongoMock {
|
||||||
this.connect = jest.fn()
|
this.connect = jest.fn()
|
||||||
this.close = jest.fn()
|
this.close = jest.fn()
|
||||||
this.insertOne = jest.fn()
|
this.insertOne = jest.fn()
|
||||||
|
this.insertMany = jest.fn(() => ({toArray: () => []}))
|
||||||
this.find = jest.fn(() => ({toArray: () => []}))
|
this.find = jest.fn(() => ({toArray: () => []}))
|
||||||
|
this.findOne = jest.fn()
|
||||||
|
this.count = jest.fn()
|
||||||
|
this.deleteOne = jest.fn()
|
||||||
|
this.deleteMany = jest.fn(() => ({toArray: () => []}))
|
||||||
|
this.updateOne = jest.fn()
|
||||||
|
this.updateMany = jest.fn(() => ({toArray: () => []}))
|
||||||
|
|
||||||
|
|
||||||
this.collection = jest.fn(() => ({
|
this.collection = jest.fn(() => ({
|
||||||
insertOne: this.insertOne,
|
insertOne: this.insertOne,
|
||||||
find: this.find,
|
find: this.find,
|
||||||
|
insertMany: this.insertMany,
|
||||||
|
findOne: this.findOne,
|
||||||
|
count: this.count,
|
||||||
|
deleteOne: this.deleteOne,
|
||||||
|
deleteMany: this.deleteMany,
|
||||||
|
updateOne: this.updateOne,
|
||||||
|
updateMany: this.updateMany,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
this.db = () => ({
|
this.db = () => ({
|
||||||
|
|
|
@ -8,6 +8,13 @@ class TestConfiguration {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function disableConsole() {
|
||||||
|
jest.spyOn(console, 'error');
|
||||||
|
console.error.mockImplementation(() => {});
|
||||||
|
|
||||||
|
return console.error.mockRestore;
|
||||||
|
}
|
||||||
|
|
||||||
describe("MongoDB Integration", () => {
|
describe("MongoDB Integration", () => {
|
||||||
let config
|
let config
|
||||||
let indexName = "Users"
|
let indexName = "Users"
|
||||||
|
@ -22,7 +29,8 @@ describe("MongoDB Integration", () => {
|
||||||
}
|
}
|
||||||
const response = await config.integration.create({
|
const response = await config.integration.create({
|
||||||
index: indexName,
|
index: indexName,
|
||||||
json: body
|
json: body,
|
||||||
|
extra: { collection: 'testCollection', actionTypes: 'insertOne'}
|
||||||
})
|
})
|
||||||
expect(config.integration.client.insertOne).toHaveBeenCalledWith(body)
|
expect(config.integration.client.insertOne).toHaveBeenCalledWith(body)
|
||||||
})
|
})
|
||||||
|
@ -31,10 +39,46 @@ describe("MongoDB Integration", () => {
|
||||||
const query = {
|
const query = {
|
||||||
json: {
|
json: {
|
||||||
address: "test"
|
address: "test"
|
||||||
}
|
},
|
||||||
|
extra: { collection: 'testCollection', actionTypes: 'find'}
|
||||||
}
|
}
|
||||||
const response = await config.integration.read(query)
|
const response = await config.integration.read(query)
|
||||||
expect(config.integration.client.find).toHaveBeenCalledWith(query.json)
|
expect(config.integration.client.find).toHaveBeenCalledWith(query.json)
|
||||||
expect(response).toEqual(expect.any(Array))
|
expect(response).toEqual(expect.any(Array))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("calls the delete method with the correct params", async () => {
|
||||||
|
const query = {
|
||||||
|
json: {
|
||||||
|
id: "test"
|
||||||
|
},
|
||||||
|
extra: { collection: 'testCollection', actionTypes: 'deleteOne'}
|
||||||
|
}
|
||||||
|
const response = await config.integration.delete(query)
|
||||||
|
expect(config.integration.client.deleteOne).toHaveBeenCalledWith(query.json)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("calls the update method with the correct params", async () => {
|
||||||
|
const query = {
|
||||||
|
json: {
|
||||||
|
id: "test"
|
||||||
|
},
|
||||||
|
extra: { collection: 'testCollection', actionTypes: 'updateOne'}
|
||||||
|
}
|
||||||
|
const response = await config.integration.update(query)
|
||||||
|
expect(config.integration.client.updateOne).toHaveBeenCalledWith(query.json)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws an error when an invalid query.extra.actionType is passed for each method", async () => {
|
||||||
|
const restore = disableConsole()
|
||||||
|
|
||||||
|
const query = {
|
||||||
|
extra: { collection: 'testCollection', actionTypes: 'deleteOne'}
|
||||||
|
}
|
||||||
|
// Weird, need to do an IIFE for jest to recognize that it throws
|
||||||
|
expect(() => config.integration.read(query)()).toThrow(expect.any(Object))
|
||||||
|
|
||||||
|
restore()
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
Loading…
Reference in New Issue