1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add test for throwing route handler

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-02-14 12:14:11 +02:00
parent 878488f4d0
commit b57178157c
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
2 changed files with 54 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import parseRequestInjectable from "./router/parse-request.injectable";
import { contentTypes } from "./router-content-types";
import mockFs from "mock-fs";
describe("router", () => {
let router: Router;
@ -23,6 +24,8 @@ describe("router", () => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
mockFs();
di.override(parseRequestInjectable, () => () => Promise.resolve({ payload: "some-payload" }));
await di.runSetups();
@ -44,6 +47,10 @@ describe("router", () => {
router = di.inject(routerInjectable);
});
afterEach(() => {
mockFs.restore();
});
describe("when navigating to the route", () => {
let actualPromise: Promise<boolean>;
let clusterStub: Cluster;
@ -97,6 +104,46 @@ describe("router", () => {
expect(responseStub.end).toHaveBeenCalledWith('{"some":"object"}');
});
describe("when handler resolves without any result", () => {
beforeEach(async () => {
await routeHandlerMock.resolve(undefined);
await actualPromise;
});
it("resolves as plain text", () => {
expect(responseStub.setHeader.mock.calls).toEqual([["Content-Type", "text/plain"]]);
});
it("resolves with status code for no content", async () => {
expect(responseStub.statusCode).toBe(204);
});
it("resolves without content", async () => {
expect(responseStub.end.mock.calls).toEqual([[]]);
});
});
describe("when handler rejects", () => {
beforeEach(async () => {
await routeHandlerMock.reject(new Error("some-error"));
await actualPromise;
});
it("resolves as plain text", () => {
expect(responseStub.setHeader.mock.calls).toEqual([["Content-Type", "text/plain"]]);
});
it('resolves with "422" status code', () => {
expect(responseStub.statusCode).toBe(422);
});
it("resolves with error", () => {
expect(responseStub.end).toHaveBeenCalledWith("Error: some-error");
});
});
[
{ contentType: "text/plain", contentTypeObject: contentTypes.txt },
{ contentType: "application/json", contentTypeObject: contentTypes.json },
@ -164,7 +211,7 @@ describe("router", () => {
await actualPromise;
expect(responseStub.end).toHaveBeenCalledWith(undefined);
expect(responseStub.end.mock.calls).toEqual([[]]);
});
it(`given content type is "${scenario.contentType}", when handler resolves with error, has error as body`, async () => {

View File

@ -142,7 +142,7 @@ const handleRoute = (route: Route<any>) => async (request: LensApiRequest) => {
if (!result) {
const mappedResult = contentTypes.txt.resultMapper({
statusCode: 204,
response: null,
response: undefined,
});
writeServerResponse(mappedResult);
@ -188,5 +188,9 @@ const writeServerResponseFor =
return;
}
serverResponse.end(content);
if (content) {
serverResponse.end(content);
} else {
serverResponse.end();
}
};