Skip to content

Commit

Permalink
Merge branch '21-unit-tests-for-api-routes' into 25-migrating-oauth-p…
Browse files Browse the repository at this point in the history
…assportjs
  • Loading branch information
sametcodes committed Mar 7, 2023
2 parents 76581df + 56a0a8d commit 293fbaf
Show file tree
Hide file tree
Showing 34 changed files with 321 additions and 11,539 deletions.
53 changes: 53 additions & 0 deletions __tests__/pages/platform/codewars.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { testApiHandler } from "next-test-api-route-handler";
import handlePlatformAPI from "@/services/api/handler";

import { encode } from "querystring";

import * as services from "@/services/platform/codewars";
import * as templates from "@/components/svgs/codewars";

const handler = handlePlatformAPI("codewars", services, templates);
const methods = Object.keys(services);

describe("Codewars Platform APIs", () => {
methods.forEach((method) => {
test(`/api/platform/codewars?method=${method}`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/codewars?${encode({
method,
uid: "6405534466df6b87ed0adb53",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.status).toBe(200);

const xml = await res.text();
expect(xml.indexOf("<svg")).toBe(0);
},
});
});

test(`/api/platform/codewars?method=${method}&returnType=json`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/codewars?${encode({
method,
uid: "6405534466df6b87ed0adb53",
returnType: "json",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.headers.get("content-type")).toContain("application/json");
expect(res.status).toBe(200);
},
});
});
});
});

export {};
56 changes: 56 additions & 0 deletions __tests__/pages/platform/github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { testApiHandler } from "next-test-api-route-handler";
import handlePlatformAPI from "@/services/api/handler";

import { encode } from "querystring";

import * as services from "@/services/platform/github";
import * as templates from "@/components/svgs/github";

const handler = handlePlatformAPI("github", services, templates);
const methods = Object.keys(services);

describe("Github Platform APIs", () => {
methods.forEach((method) => {
test(`/api/platform/github?method=${method}`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/github?${encode({
method,
uid: "6405534466df6b87ed0adb53",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.status).toBe(200);

const xml = await res.text();
expect(xml.indexOf("<svg")).toBe(0);
},
});
});

test(`/api/platform/github?method=${method}&returnType=json`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/github?${encode({
method,
uid: "6405534466df6b87ed0adb53",
returnType: "json",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.headers.get("content-type")).toContain("application/json");
expect(res.status).toBe(200);

const json = await res.json();
expect(json).toHaveProperty("data");
},
});
});
});
});

export {};
53 changes: 53 additions & 0 deletions __tests__/pages/platform/stackoverflow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { testApiHandler } from "next-test-api-route-handler";
import handlePlatformAPI from "@/services/api/handler";

import { encode } from "querystring";

import * as services from "@/services/platform/stackoverflow";
import * as templates from "@/components/svgs/stackoverflow";

const handler = handlePlatformAPI("stackoverflow", services, templates);
const methods = Object.keys(services);

describe("Stackoverflow Platform APIs", () => {
methods.forEach((method) => {
test(`/api/platform/stackoverflow?method=${method}`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/stackoverflow?${encode({
method,
uid: "6405534466df6b87ed0adb53",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.status).toBe(200);

const xml = await res.text();
expect(xml.indexOf("<svg")).toBe(0);
},
});
});

test(`/api/platform/stackoverflow?method=${method}&returnType=json`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/stackoverflow?${encode({
method,
uid: "6405534466df6b87ed0adb53",
returnType: "json",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.headers.get("content-type")).toContain("application/json");
expect(res.status).toBe(200);
},
});
});
});
});

export {};
53 changes: 53 additions & 0 deletions __tests__/pages/platform/wakatime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { testApiHandler } from "next-test-api-route-handler";
import handlePlatformAPI from "@/services/api/handler";

import { encode } from "querystring";

import * as services from "@/services/platform/wakatime";
import * as templates from "@/components/svgs/wakatime";

const handler = handlePlatformAPI("wakatime", services, templates);
const methods = Object.keys(services);

describe("Wakatime Platform APIs", () => {
methods.forEach((method) => {
test(`/api/platform/wakatime?method=${method}`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/wakatime?${encode({
method,
uid: "6405534466df6b87ed0adb53",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.status).toBe(200);

const xml = await res.text();
expect(xml.indexOf("<svg")).toBe(0);
},
});
});

test(`/api/platform/wakatime?method=${method}&returnType=json`, async () => {
await testApiHandler({
handler,
requestPatcher: (req) => {
req.url = `/api/platform/wakatime?${encode({
method,
uid: "6405534466df6b87ed0adb53",
returnType: "json",
})}`;
},
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
expect(res.headers.get("content-type")).toContain("application/json");
expect(res.status).toBe(200);
},
});
});
});
});

export {};
3 changes: 2 additions & 1 deletion app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";
import { useSession, signIn, signOut } from "next-auth/react";
import Image from "next/image";

export default function LoginPage() {
const { data: session } = useSession();
Expand All @@ -8,7 +9,7 @@ export default function LoginPage() {
return (
<>
<p>Welcome, {session?.user?.name}</p>
<img src={session?.user?.image || ""} alt="photo" width={60} />
<Image src={session?.user?.image || ""} alt="photo" width={60} />
<p>
Signed in as {session?.user?.email} <br />
</p>
Expand Down
6 changes: 3 additions & 3 deletions components/svgs/codewars/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as Icons from "@components/icons";
import * as Icons from "@/components/icons";
import {
Document,
DocumentTitle,
List,
ListItem,
} from "@components/svgs/document";
import { ObjectEntries } from "@utils";
} from "@/components/svgs/document";
import { ObjectEntries } from "@/utils";

export const getUser = (result: any, platform: any) => {
const langs = ObjectEntries(result.ranks.languages).map(([key, value]) => ({
Expand Down
4 changes: 2 additions & 2 deletions components/svgs/github/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Icons from "@components/icons";
import * as Icons from "@/components/icons";
import {
Document,
DocumentTitle,
List,
ListItem,
} from "@components/svgs/document";
} from "@/components/svgs/document";

export const getCurrentYearContributions = (result: any, platform: any) => {
const { totalContributions } =
Expand Down
4 changes: 2 additions & 2 deletions components/svgs/stackoverflow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
DocumentTitle,
List,
ListItem,
} from "@components/svgs/document";
import * as Icons from "@components/icons";
} from "@/components/svgs/document";
import * as Icons from "@/components/icons";

export const getReputation = (result: any, platform: any) => {
return (
Expand Down
4 changes: 2 additions & 2 deletions components/svgs/wakatime/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
DocumentTitle,
List,
ListItem,
} from "@components/svgs/document";
import * as Icons from "@components/icons";
} from "@/components/svgs/document";
import * as Icons from "@/components/icons";

export const getAllTimeSinceToday = (result: any, platform: any) => {
return (
Expand Down
17 changes: 17 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const nextJest = require("next/jest");

const createJestConfig = nextJest({
dir: "./",
});

// Add any custom config to be passed to Jest
const customJestConfig = {
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/$1",
},
testEnvironment: "jest-environment-jsdom",
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);
8 changes: 8 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "@testing-library/jest-dom/extend-expect";

import { TextEncoder, TextDecoder } from "util";
import fetch from "node-fetch";

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
global.fetch = fetch;
Loading

0 comments on commit 293fbaf

Please sign in to comment.