move to webapp

This commit is contained in:
m5r
2021-07-18 23:32:45 +08:00
parent 61c23ec9a7
commit a989125e6e
167 changed files with 26607 additions and 24066 deletions

109
jest/helpers.ts Normal file
View File

@ -0,0 +1,109 @@
import type { NextApiHandler } from "next";
import type { IncomingMessage, RequestListener, ServerResponse } from "http";
import http from "http";
import type { __ApiPreviewProps } from "next/dist/next-server/server/api-utils";
import { apiResolver } from "next/dist/next-server/server/api-utils";
import listen from "test-listen";
import fetch from "isomorphic-fetch";
import crypto from "crypto";
import CookieStore from "../lib/cookie-store";
import Session from "../lib/session";
type Authentication =
| "none"
| "auth0"
| "google-oauth2"
| "facebook"
| "twitter";
type Params = {
method: string;
body?: any;
headers?: Record<string, string>;
query?: Record<string, string>;
authentication?: Authentication;
};
const apiPreviewProps: __ApiPreviewProps = {
previewModeEncryptionKey: crypto.randomBytes(16).toString("hex"),
previewModeId: crypto.randomBytes(32).toString("hex"),
previewModeSigningKey: crypto.randomBytes(32).toString("hex"),
};
export async function callApiHandler(handler: NextApiHandler, params: Params) {
const {
method = "GET",
body,
headers = {},
query = {},
authentication = "none",
} = params;
const requestHandler: RequestListener = (req, res) => {
const propagateError = false;
Object.assign(req.headers, headers);
if (req.url !== "/") {
// in these API tests, our http server uses the same handler for all routes, it has no idea about our app's routes
// when we're hitting anything else than the / route, it means that we've been redirected
const fallbackHandler: NextApiHandler = (req, res) =>
res.status(200).end();
return apiResolver(
req,
res,
query,
fallbackHandler,
apiPreviewProps,
propagateError,
);
}
if (authentication !== "none") {
writeSessionToCookie(req, res, authentication);
}
return apiResolver(
req,
res,
query,
handler,
apiPreviewProps,
propagateError,
);
};
const server = http.createServer(requestHandler);
const url = await listen(server);
let fetchOptions: RequestInit = { method, redirect: "manual" };
if (body) {
fetchOptions.body = JSON.stringify(body);
fetchOptions.headers = { "Content-Type": "application/json" };
}
const response = await fetch(url, fetchOptions);
server.close();
return response;
}
function writeSessionToCookie(
req: IncomingMessage,
res: ServerResponse,
authentication: Authentication,
) {
const cookieStore = new CookieStore();
const session: Session = new Session({
id: `${authentication}|userId`,
email: "test@fss.test",
name: "Groot",
teamId: "teamId",
role: "owner",
});
cookieStore.save(req, res, session);
const setCookieHeader = res.getHeader("Set-Cookie") as string[];
// write it to request headers to immediately have access to the user's session
req.headers.cookie = setCookieHeader.join("");
}

24
jest/setup.ts Normal file
View File

@ -0,0 +1,24 @@
import "@testing-library/jest-dom/extend-expect";
jest.mock("next/config", () => () => {
// see https://github.com/vercel/next.js/issues/4024
const config = require("../next.config");
return {
serverRuntimeConfig: config.serverRuntimeConfig,
publicRuntimeConfig: config.publicRuntimeConfig,
};
});
jest.mock("../lib/logger", () => ({
child: jest.fn().mockReturnValue({
log: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
warn: jest.fn(),
}),
}));
export function noop() {
// exported function to mark the file as a module
}

1
jest/testing-library.ts Normal file
View File

@ -0,0 +1 @@
export * from "@testing-library/react";