Skip to content

Commit

Permalink
test(#21): created units tests for Platform API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
sametcodes committed Mar 6, 2023
1 parent 0aee800 commit 56a0a8d
Show file tree
Hide file tree
Showing 8 changed files with 12,551 additions and 5,879 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 {};
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 56a0a8d

Please sign in to comment.