refactor selfhost middleware tests to use TestConfiguration
This commit is contained in:
parent
05efe05061
commit
c4b3a7c884
|
@ -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
|
|
@ -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()
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue