refactor: named arguments for render error function (#4537)
* refactor: named arguments for render error function * Update tests/api.test.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Alexandr <qwerty541zxc@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
Alexandr
parent
f7920901b6
commit
e9d0e8fd17
+30
-17
@@ -1,3 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
import { jest } from "@jest/globals";
|
||||
import axios from "axios";
|
||||
import MockAdapter from "axios-mock-adapter";
|
||||
@@ -7,6 +9,9 @@ import { renderStatsCard } from "../src/cards/stats.js";
|
||||
import { CONSTANTS, renderError } from "../src/common/utils.js";
|
||||
import { expect, it, describe, afterEach } from "@jest/globals";
|
||||
|
||||
/**
|
||||
* @type {import("../src/fetchers/stats").StatsData}
|
||||
*/
|
||||
const stats = {
|
||||
name: "Anurag Hazra",
|
||||
totalStars: 100,
|
||||
@@ -19,7 +24,7 @@ const stats = {
|
||||
totalDiscussionsStarted: 10,
|
||||
totalDiscussionsAnswered: 40,
|
||||
contributedTo: 50,
|
||||
rank: null,
|
||||
rank: { level: "DEV", percentile: 0 },
|
||||
};
|
||||
|
||||
stats.rank = calculateRank({
|
||||
@@ -115,10 +120,11 @@ describe("Test /api/", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
error.errors[0].message,
|
||||
"Make sure the provided username is not an organization",
|
||||
),
|
||||
renderError({
|
||||
message: error.errors[0].message,
|
||||
secondaryMessage:
|
||||
"Make sure the provided username is not an organization",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -129,11 +135,12 @@ describe("Test /api/", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
error.errors[0].message,
|
||||
"Make sure the provided username is not an organization",
|
||||
{ theme: "merko" },
|
||||
),
|
||||
renderError({
|
||||
message: error.errors[0].message,
|
||||
secondaryMessage:
|
||||
"Make sure the provided username is not an organization",
|
||||
renderOptions: { theme: "merko" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -307,11 +314,11 @@ describe("Test /api/", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
"This username is blacklisted",
|
||||
"Please deploy your own instance",
|
||||
{ show_repo_link: false },
|
||||
),
|
||||
renderError({
|
||||
message: "This username is blacklisted",
|
||||
secondaryMessage: "Please deploy your own instance",
|
||||
renderOptions: { show_repo_link: false },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -322,7 +329,10 @@ describe("Test /api/", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError("Something went wrong", "Language not found"),
|
||||
renderError({
|
||||
message: "Something went wrong",
|
||||
secondaryMessage: "Language not found",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -340,7 +350,10 @@ describe("Test /api/", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError("Could not fetch total commits.", "Please try again later"),
|
||||
renderError({
|
||||
message: "Could not fetch total commits.",
|
||||
secondaryMessage: "Please try again later",
|
||||
}),
|
||||
);
|
||||
// Received SVG output should not contain string "https://tiny.one/readme-stats"
|
||||
expect(res.send.mock.calls[0][0]).not.toContain(
|
||||
|
||||
+11
-6
@@ -1,3 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
import { jest } from "@jest/globals";
|
||||
import "@testing-library/jest-dom";
|
||||
import axios from "axios";
|
||||
@@ -124,10 +126,10 @@ describe("Test /api/gist", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
'Missing params "id" make sure you pass the parameters in URL',
|
||||
"/api/gist?id=GIST_ID",
|
||||
),
|
||||
renderError({
|
||||
message: 'Missing params "id" make sure you pass the parameters in URL',
|
||||
secondaryMessage: "/api/gist?id=GIST_ID",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -148,7 +150,7 @@ describe("Test /api/gist", () => {
|
||||
await gist(req, res);
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(renderError("Gist not found"));
|
||||
expect(res.send).toBeCalledWith(renderError({ message: "Gist not found" }));
|
||||
});
|
||||
|
||||
it("should render error if wrong locale is provided", async () => {
|
||||
@@ -167,7 +169,10 @@ describe("Test /api/gist", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError("Something went wrong", "Language not found"),
|
||||
renderError({
|
||||
message: "Something went wrong",
|
||||
secondaryMessage: "Language not found",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
+22
-12
@@ -1,3 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
import { jest } from "@jest/globals";
|
||||
import "@testing-library/jest-dom";
|
||||
import axios from "axios";
|
||||
@@ -56,6 +58,7 @@ describe("Test /api/pin", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
// @ts-ignore
|
||||
renderRepoCard({
|
||||
...data_repo.repository,
|
||||
starCount: data_repo.repository.stargazers.totalCount,
|
||||
@@ -86,6 +89,7 @@ describe("Test /api/pin", () => {
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderRepoCard(
|
||||
// @ts-ignore
|
||||
{
|
||||
...data_repo.repository,
|
||||
starCount: data_repo.repository.stargazers.totalCount,
|
||||
@@ -113,7 +117,9 @@ describe("Test /api/pin", () => {
|
||||
await pin(req, res);
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(renderError("User Repository Not found"));
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError({ message: "User Repository Not found" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("should render error card if org repo not found", async () => {
|
||||
@@ -135,7 +141,7 @@ describe("Test /api/pin", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError("Organization Repository Not found"),
|
||||
renderError({ message: "Organization Repository Not found" }),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -156,11 +162,11 @@ describe("Test /api/pin", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
"This username is blacklisted",
|
||||
"Please deploy your own instance",
|
||||
{ show_repo_link: false },
|
||||
),
|
||||
renderError({
|
||||
message: "This username is blacklisted",
|
||||
secondaryMessage: "Please deploy your own instance",
|
||||
renderOptions: { show_repo_link: false },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -182,7 +188,10 @@ describe("Test /api/pin", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError("Something went wrong", "Language not found"),
|
||||
renderError({
|
||||
message: "Something went wrong",
|
||||
secondaryMessage: "Language not found",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -199,10 +208,11 @@ describe("Test /api/pin", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
'Missing params "username", "repo" make sure you pass the parameters in URL',
|
||||
"/api/pin?username=USERNAME&repo=REPO_NAME",
|
||||
),
|
||||
renderError({
|
||||
message:
|
||||
'Missing params "username", "repo" make sure you pass the parameters in URL',
|
||||
secondaryMessage: "/api/pin?username=USERNAME&repo=REPO_NAME",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
+20
-11
@@ -1,3 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
import { jest } from "@jest/globals";
|
||||
import "@testing-library/jest-dom";
|
||||
import axios from "axios";
|
||||
@@ -140,10 +142,11 @@ describe("Test /api/top-langs", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
error.errors[0].message,
|
||||
"Make sure the provided username is not an organization",
|
||||
),
|
||||
renderError({
|
||||
message: error.errors[0].message,
|
||||
secondaryMessage:
|
||||
"Make sure the provided username is not an organization",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -164,7 +167,10 @@ describe("Test /api/top-langs", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError("Something went wrong", "Incorrect layout input"),
|
||||
renderError({
|
||||
message: "Something went wrong",
|
||||
secondaryMessage: "Incorrect layout input",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -184,11 +190,11 @@ describe("Test /api/top-langs", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError(
|
||||
"This username is blacklisted",
|
||||
"Please deploy your own instance",
|
||||
{ show_repo_link: false },
|
||||
),
|
||||
renderError({
|
||||
message: "This username is blacklisted",
|
||||
secondaryMessage: "Please deploy your own instance",
|
||||
renderOptions: { show_repo_link: false },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -209,7 +215,10 @@ describe("Test /api/top-langs", () => {
|
||||
|
||||
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
||||
expect(res.send).toBeCalledWith(
|
||||
renderError("Something went wrong", "Locale not found"),
|
||||
renderError({
|
||||
message: "Something went wrong",
|
||||
secondaryMessage: "Locale not found",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
+5
-5
@@ -46,7 +46,7 @@ describe("Test utils.js", () => {
|
||||
});
|
||||
|
||||
it("should test renderError", () => {
|
||||
document.body.innerHTML = renderError("Something went wrong");
|
||||
document.body.innerHTML = renderError({ message: "Something went wrong" });
|
||||
expect(
|
||||
queryByTestId(document.body, "message").children[0],
|
||||
).toHaveTextContent(/Something went wrong/gim);
|
||||
@@ -55,10 +55,10 @@ describe("Test utils.js", () => {
|
||||
).toBeEmptyDOMElement(2);
|
||||
|
||||
// Secondary message
|
||||
document.body.innerHTML = renderError(
|
||||
"Something went wrong",
|
||||
"Secondary Message",
|
||||
);
|
||||
document.body.innerHTML = renderError({
|
||||
message: "Something went wrong",
|
||||
secondaryMessage: "Secondary Message",
|
||||
});
|
||||
expect(
|
||||
queryByTestId(document.body, "message").children[1],
|
||||
).toHaveTextContent(/Secondary Message/gim);
|
||||
|
||||
Reference in New Issue
Block a user