1
0
Fork 0
mirror of synced 2024-06-02 10:34:40 +12:00
budibase/packages/server/src/integrations/tests/mongo.spec.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-03-17 02:54:39 +13:00
const mongo = require("mongodb")
const MongoDBIntegration = require("../mongodb")
jest.mock("mongodb")
class TestConfiguration {
constructor(config = {}) {
2022-05-14 02:56:02 +12:00
this.integration = new MongoDBIntegration.integration(config)
2021-03-17 02:54:39 +13:00
}
}
function disableConsole() {
2022-05-14 02:56:02 +12:00
jest.spyOn(console, "error")
console.error.mockImplementation(() => {})
2022-05-14 02:56:02 +12:00
return console.error.mockRestore
}
2021-03-17 02:54:39 +13:00
describe("MongoDB Integration", () => {
2022-05-14 02:56:02 +12:00
let config
2021-03-17 02:54:39 +13:00
let indexName = "Users"
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const body = {
2022-05-14 02:56:02 +12:00
name: "Hello",
2021-03-17 02:54:39 +13:00
}
await config.integration.create({
2021-03-17 02:54:39 +13:00
index: indexName,
json: body,
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "insertOne" },
2021-03-17 02:54:39 +13:00
})
expect(config.integration.client.insertOne).toHaveBeenCalledWith(body)
})
it("calls the read method with the correct params", async () => {
const query = {
json: {
2022-05-14 02:56:02 +12:00
address: "test",
},
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "find" },
2021-03-17 02:54:39 +13:00
}
const response = await config.integration.read(query)
expect(config.integration.client.find).toHaveBeenCalledWith(query.json)
expect(response).toEqual(expect.any(Array))
})
it("calls the delete method with the correct params", async () => {
const query = {
json: {
2022-05-14 02:56:02 +12:00
id: "test",
},
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "deleteOne" },
}
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: {
2022-05-14 02:56:02 +12:00
filter: {
id: "test",
},
update: {
name: "TestName",
},
options: {
upsert: false,
},
},
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "updateOne" },
}
await config.integration.update(query)
2022-05-14 02:56:02 +12:00
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 = {
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "deleteOne" },
}
let error = null
try {
await config.integration.read(query)
} catch (err) {
error = err
}
expect(error).toBeDefined()
restore()
})
2022-05-14 02:56:02 +12:00
})