Fix test for update

This commit is contained in:
Mel O'Hagan 2022-05-13 15:56:02 +01:00 committed by mike12345567
parent a58f35a6cd
commit 672bfda7dd
1 changed files with 28 additions and 16 deletions

View File

@ -4,19 +4,19 @@ jest.mock("mongodb")
class TestConfiguration {
constructor(config = {}) {
this.integration = new MongoDBIntegration.integration(config)
this.integration = new MongoDBIntegration.integration(config)
}
}
function disableConsole() {
jest.spyOn(console, 'error');
console.error.mockImplementation(() => {});
jest.spyOn(console, "error")
console.error.mockImplementation(() => {})
return console.error.mockRestore;
return console.error.mockRestore
}
describe("MongoDB Integration", () => {
let config
let config
let indexName = "Users"
beforeEach(() => {
@ -25,12 +25,12 @@ describe("MongoDB Integration", () => {
it("calls the create method with the correct params", async () => {
const body = {
name: "Hello"
name: "Hello",
}
await config.integration.create({
index: indexName,
json: body,
extra: { collection: 'testCollection', actionTypes: 'insertOne'}
extra: { collection: "testCollection", actionTypes: "insertOne" },
})
expect(config.integration.client.insertOne).toHaveBeenCalledWith(body)
})
@ -38,9 +38,9 @@ describe("MongoDB Integration", () => {
it("calls the read method with the correct params", async () => {
const query = {
json: {
address: "test"
address: "test",
},
extra: { collection: 'testCollection', actionTypes: 'find'}
extra: { collection: "testCollection", actionTypes: "find" },
}
const response = await config.integration.read(query)
expect(config.integration.client.find).toHaveBeenCalledWith(query.json)
@ -50,9 +50,9 @@ describe("MongoDB Integration", () => {
it("calls the delete method with the correct params", async () => {
const query = {
json: {
id: "test"
id: "test",
},
extra: { collection: 'testCollection', actionTypes: 'deleteOne'}
extra: { collection: "testCollection", actionTypes: "deleteOne" },
}
await config.integration.delete(query)
expect(config.integration.client.deleteOne).toHaveBeenCalledWith(query.json)
@ -61,19 +61,31 @@ describe("MongoDB Integration", () => {
it("calls the update method with the correct params", async () => {
const query = {
json: {
id: "test"
filter: {
id: "test",
},
update: {
name: "TestName",
},
options: {
upsert: false,
},
},
extra: { collection: 'testCollection', actionTypes: 'updateOne'}
extra: { collection: "testCollection", actionTypes: "updateOne" },
}
await config.integration.update(query)
expect(config.integration.client.updateOne).toHaveBeenCalledWith(query.json)
expect(config.integration.client.updateOne).toHaveBeenCalledWith(
query.json.filter,
query.json.update,
query.json.options
)
})
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'}
extra: { collection: "testCollection", actionTypes: "deleteOne" },
}
let error = null
@ -85,4 +97,4 @@ describe("MongoDB Integration", () => {
expect(error).toBeDefined()
restore()
})
})
})