Browse Source

refactor selfhost middleware tests to use TestConfiguration

pull/1266/head
Martin McKeaveney 6 years ago
parent
commit
c073b8639d
  1. 32
      packages/server/src/middleware/tests/TestConfiguration.js
  2. 60
      packages/server/src/middleware/tests/selfhost.spec.js

32
packages/server/src/middleware/tests/TestConfiguration.js

@ -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

60
packages/server/src/middleware/tests/selfhost.spec.js

@ -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…
Cancel
Save