Skip to content

Commit

Permalink
Rewrite unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jul 30, 2024
1 parent e9e7311 commit 38be87f
Showing 1 changed file with 32 additions and 55 deletions.
87 changes: 32 additions & 55 deletions packages/server/src/custom/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { createContent } from "../../test/utils";
import type { CustomRequest } from ".";
import { CustomComment } from ".";

type MockRequest = CustomRequest & {
auth?: boolean;
};

const mockAdapter: StorageAdapter = {
deleteComment() {
// does nothing
Expand Down Expand Up @@ -44,16 +48,20 @@ const mockAdapter: StorageAdapter = {
},
};

const app = CustomComment({ adapter: mockAdapter });
const app = CustomComment<MockRequest>({
adapter: mockAdapter,
getSession(req) {
if (req.auth) return { id: "mock_user" };
return null;
},
});

describe("Custom Comment Routes", () => {
test.each<{ name: string; req: CustomRequest; success: boolean }>([
test.each<{ name: string; req: MockRequest; success: boolean }>([
{
name: "Normal",
req: {
getSession() {
return null;
},
auth: false,
body() {
return null;
},
Expand All @@ -65,9 +73,7 @@ describe("Custom Comment Routes", () => {
{
name: "With Auth",
req: {
getSession() {
return { id: "mock_user" };
},
auth: true,
body() {
return null;
},
Expand All @@ -79,9 +85,7 @@ describe("Custom Comment Routes", () => {
{
name: "Invalid",
req: {
getSession() {
return null;
},
auth: false,
body() {
return null;
},
Expand All @@ -96,16 +100,14 @@ describe("Custom Comment Routes", () => {
expect(result.type).toBe(success ? "success" : "error");
});

test.each<{ name: string; req: CustomRequest; success: boolean }>([
test.each<{ name: string; req: MockRequest; success: boolean }>([
{
name: "Normal",
req: {
auth: true,
body() {
return { content: createContent("Hello World") };
},
getSession() {
return { id: "mock_user" };
},
params: new Map([["page", "default"]]),
queryParams: new Map(),
},
Expand All @@ -114,12 +116,10 @@ describe("Custom Comment Routes", () => {
{
name: "Invalid",
req: {
auth: true,
body() {
return { content: createContent(" ") };
},
getSession() {
return { id: "mock_user" };
},
params: new Map(),
queryParams: new Map(),
},
Expand All @@ -128,12 +128,10 @@ describe("Custom Comment Routes", () => {
{
name: "Unauthorized",
req: {
auth: false,
body() {
return { content: createContent("Hello World") };
},
getSession() {
return null;
},
params: new Map([["page", "default"]]),
queryParams: new Map(),
},
Expand All @@ -145,16 +143,14 @@ describe("Custom Comment Routes", () => {
expect(result.type).toBe(success ? "success" : "error");
});

test.each<{ name: string; req: CustomRequest; success: boolean }>([
test.each<{ name: string; req: MockRequest; success: boolean }>([
{
name: "Normal",
req: {
auth: true,
body() {
return { content: createContent("Hello World") };
},
getSession() {
return { id: "mock_user" };
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -166,12 +162,10 @@ describe("Custom Comment Routes", () => {
{
name: "Invalid",
req: {
auth: true,
body() {
return { content: createContent(" ") };
},
getSession() {
return { id: "mock_user" };
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -183,12 +177,10 @@ describe("Custom Comment Routes", () => {
{
name: "Unauthorized",
req: {
auth: false,
body() {
return { content: createContent("Hello World") };
},
getSession() {
return null;
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -203,16 +195,14 @@ describe("Custom Comment Routes", () => {
expect(result.type).toBe(success ? "success" : "error");
});

test.each<{ name: string; req: CustomRequest; success: boolean }>([
test.each<{ name: string; req: MockRequest; success: boolean }>([
{
name: "Normal",
req: {
auth: true,
body() {
return null;
},
getSession() {
return { id: "mock_user" };
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -224,12 +214,10 @@ describe("Custom Comment Routes", () => {
{
name: "Unauthorized",
req: {
auth: false,
body() {
return null;
},
getSession() {
return null;
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -244,16 +232,14 @@ describe("Custom Comment Routes", () => {
expect(result.type).toBe(success ? "success" : "error");
});

test.each<{ name: string; req: CustomRequest; success: boolean }>([
test.each<{ name: string; req: MockRequest; success: boolean }>([
{
name: "Normal",
req: {
auth: true,
body() {
return { like: true };
},
getSession() {
return { id: "mock_user" };
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -268,9 +254,7 @@ describe("Custom Comment Routes", () => {
body() {
return { like: "invalid" };
},
getSession() {
return { id: "mock_user" };
},
auth: true,
params: new Map([["id", "test"]]),
queryParams: new Map(),
},
Expand All @@ -279,12 +263,10 @@ describe("Custom Comment Routes", () => {
{
name: "Unauthorized",
req: {
auth: false,
body() {
return null;
},
getSession() {
return null;
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -299,16 +281,14 @@ describe("Custom Comment Routes", () => {
expect(result.type).toBe(success ? "success" : "error");
});

test.each<{ name: string; req: CustomRequest; success: boolean }>([
test.each<{ name: string; req: MockRequest; success: boolean }>([
{
name: "Normal",
req: {
auth: true,
body() {
return null;
},
getSession() {
return { id: "mock_user" };
},
params: new Map([
["id", "test"],
["page", "default"],
Expand All @@ -323,9 +303,6 @@ describe("Custom Comment Routes", () => {
body() {
return null;
},
getSession() {
return null;
},
params: new Map([
["id", "test"],
["page", "default"],
Expand Down

0 comments on commit 38be87f

Please sign in to comment.