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("../../environment")
|
||||||
jest.mock("../../utilities/builder/hosting")
|
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", () => {
|
describe("Self host middleware", () => {
|
||||||
const next = jest.fn()
|
let config
|
||||||
const throwMock = jest.fn()
|
|
||||||
|
beforeEach(() => {
|
||||||
|
config = new TestConfiguration()
|
||||||
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.clearAllMocks()
|
config.afterEach()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls next() when CLOUD and SELF_HOSTED env vars are set", async () => {
|
it("calls next() when CLOUD and SELF_HOSTED env vars are set", async () => {
|
||||||
env.CLOUD = 1
|
env.CLOUD = 1
|
||||||
env.SELF_HOSTED = 1
|
env.SELF_HOSTED = 1
|
||||||
|
|
||||||
await selfHostMiddleware({}, next)
|
await config.executeMiddleware()
|
||||||
expect(next).toHaveBeenCalled()
|
expect(config.next).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("throws when hostingInfo type is cloud", async () => {
|
it("throws when hostingInfo type is cloud", async () => {
|
||||||
env.CLOUD = 0
|
config.setSelfHosted()
|
||||||
env.SELF_HOSTED = 0
|
|
||||||
|
|
||||||
hosting.getHostingInfo.mockImplementationOnce(() => ({ type: hosting.HostingTypes.CLOUD }))
|
hosting.getHostingInfo.mockImplementationOnce(() => ({ type: hosting.HostingTypes.CLOUD }))
|
||||||
|
|
||||||
await selfHostMiddleware({ throw: throwMock }, next)
|
await config.executeMiddleware()
|
||||||
expect(throwMock).toHaveBeenCalledWith(400, "Endpoint unavailable in cloud hosting.")
|
expect(config.throw).toHaveBeenCalledWith(400, "Endpoint unavailable in cloud hosting.")
|
||||||
expect(next).not.toHaveBeenCalled()
|
expect(config.next).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls the self hosting middleware to pass through to next() when the hostingInfo type is self", async () => {
|
it("calls the self hosting middleware to pass through to next() when the hostingInfo type is self", async () => {
|
||||||
env.CLOUD = 0
|
config.setSelfHosted()
|
||||||
env.SELF_HOSTED = 0
|
|
||||||
|
|
||||||
hosting.getHostingInfo.mockImplementationOnce(() => ({ type: hosting.HostingTypes.SELF }))
|
hosting.getHostingInfo.mockImplementationOnce(() => ({ type: hosting.HostingTypes.SELF }))
|
||||||
|
|
||||||
await selfHostMiddleware({}, next)
|
await config.executeMiddleware()
|
||||||
expect(next).toHaveBeenCalled()
|
expect(config.next).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue