1
0
Fork 0
mirror of synced 2024-06-30 03:50:37 +12:00

refactor selfhost middleware tests to use TestConfiguration

This commit is contained in:
Martin McKeaveney 2021-03-09 11:33:16 +00:00
parent 758e964977
commit c073b8639d
2 changed files with 46 additions and 46 deletions

View file

@ -1,32 +0,0 @@
let env = require("../../environment")
class TestConfiguration {
constructor(middleware) {
// env = config.env || {}
this.middleware = middleware
this.next = jest.fn()
this.throwMock = jest.fn()
}
callMiddleware(ctx, next) {
return this.middleware(ctx, next)
}
clear() {
jest.clearAllMocks()
}
setEnv(config) {
env = config
}
async init() {
// return this.createApp(appName)
}
end() {
// this.server.close()
}
}
module.exports = TestConfiguration

View file

@ -4,40 +4,72 @@ const hosting = require("../../utilities/builder/hosting");
jest.mock("../../environment")
jest.mock("../../utilities/builder/hosting")
class TestConfiguration {
constructor() {
this.next = jest.fn()
this.throw = jest.fn()
this.middleware = selfHostMiddleware
this.ctx = {
next: this.next,
throw: this.throw
}
}
executeMiddleware() {
return this.middleware(this.ctx, this.next)
}
setCloudHosted() {
env.CLOUD = 1
env.SELF_HOSTED = 0
}
setSelfHosted() {
env.CLOUD = 0
env.SELF_HOSTED = 1
}
afterEach() {
jest.clearAllMocks()
}
}
describe("Self host middleware", () => {
const next = jest.fn()
const throwMock = jest.fn()
let config
beforeEach(() => {
config = new TestConfiguration()
})
afterEach(() => {
jest.clearAllMocks()
config.afterEach()
})
it("calls next() when CLOUD and SELF_HOSTED env vars are set", async () => {
env.CLOUD = 1
env.SELF_HOSTED = 1
await selfHostMiddleware({}, next)
expect(next).toHaveBeenCalled()
await config.executeMiddleware()
expect(config.next).toHaveBeenCalled()
})
it("throws when hostingInfo type is cloud", async () => {
env.CLOUD = 0
env.SELF_HOSTED = 0
config.setSelfHosted()
hosting.getHostingInfo.mockImplementationOnce(() => ({ type: hosting.HostingTypes.CLOUD }))
await selfHostMiddleware({ throw: throwMock }, next)
expect(throwMock).toHaveBeenCalledWith(400, "Endpoint unavailable in cloud hosting.")
expect(next).not.toHaveBeenCalled()
await config.executeMiddleware()
expect(config.throw).toHaveBeenCalledWith(400, "Endpoint unavailable in cloud hosting.")
expect(config.next).not.toHaveBeenCalled()
})
it("calls the self hosting middleware to pass through to next() when the hostingInfo type is self", async () => {
env.CLOUD = 0
env.SELF_HOSTED = 0
config.setSelfHosted()
hosting.getHostingInfo.mockImplementationOnce(() => ({ type: hosting.HostingTypes.SELF }))
await selfHostMiddleware({}, next)
expect(next).toHaveBeenCalled()
await config.executeMiddleware()
expect(config.next).toHaveBeenCalled()
})
})