131 lines
5.9 KiB
TypeScript
131 lines
5.9 KiB
TypeScript
import "@testing-library/jest-dom";
|
|
import { render, screen } from "@testing-library/react";
|
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
import { MemoryRouter } from "react-router";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import IndexRoute, { loader as indexLoader } from "./$slug.($lang)";
|
|
import CategoryRoute, { loader as categoryLoader } from "./$slug.($lang).categories.$category";
|
|
import ArticleRoute, { action as articleAction, loader as articleLoader } from "./$slug.($lang).articles.$article";
|
|
import { KbRoutesIndexApi } from "~/modules/knowledgeBase/routes/api/KbRoutes.Index.Api";
|
|
import { KbRoutesCategoryApi } from "~/modules/knowledgeBase/routes/api/KbRoutes.Category.Api";
|
|
import { KbRoutesArticleApi } from "~/modules/knowledgeBase/routes/api/KbRoutes.Article.Api";
|
|
|
|
type MockKnowledgeBase = { id: string; defaultLanguage: string; metatags: { title: string }[] };
|
|
type MockCategory = { id: string; metatags: { title: string }[] };
|
|
type MockArticle = { id: string; metatags: { title: string }[] };
|
|
|
|
const mockKb: MockKnowledgeBase = { id: "kb_1", defaultLanguage: "en", metatags: [{ title: "Docs" }] };
|
|
const mockCategory: MockCategory = { id: "category_1", metatags: [{ title: "Getting started" }] };
|
|
const mockArticle: MockArticle = { id: "article_1", metatags: [{ title: "Install" }] };
|
|
|
|
vi.mock("~/components/ui/errors/ServerError", () => ({ default: () => <div data-testid="server-error" /> }));
|
|
vi.mock("~/modules/knowledgeBase/routes/views/KbRoutes.Index.View", () => ({ default: () => <div data-testid="kb-index-view" /> }));
|
|
vi.mock("~/modules/knowledgeBase/routes/views/KbRoutes.Category.View", () => ({ default: () => <div data-testid="kb-category-view" /> }));
|
|
vi.mock("~/modules/knowledgeBase/routes/views/KbRoutes.Article.View", () => ({ default: () => <div data-testid="kb-article-view" /> }));
|
|
|
|
vi.mock("~/modules/knowledgeBase/routes/api/KbRoutes.Index.Api", () => ({
|
|
KbRoutesIndexApi: { loader: vi.fn() },
|
|
}));
|
|
vi.mock("~/modules/knowledgeBase/routes/api/KbRoutes.Category.Api", () => ({
|
|
KbRoutesCategoryApi: { loader: vi.fn() },
|
|
}));
|
|
vi.mock("~/modules/knowledgeBase/routes/api/KbRoutes.Article.Api", () => ({
|
|
KbRoutesArticleApi: { loader: vi.fn(), action: vi.fn() },
|
|
}));
|
|
|
|
function renderRoute(Route: React.ComponentType) {
|
|
return render(
|
|
<MemoryRouter>
|
|
<Route />
|
|
</MemoryRouter>
|
|
);
|
|
}
|
|
|
|
function loaderArgs(pathname: string, params: Record<string, string>): LoaderFunctionArgs {
|
|
return {
|
|
request: new Request(`https://example.test${pathname}`),
|
|
params,
|
|
context: {},
|
|
unstable_pattern: pathname.slice(1),
|
|
} as LoaderFunctionArgs;
|
|
}
|
|
|
|
function actionArgs(pathname: string, params: Record<string, string>): ActionFunctionArgs {
|
|
return {
|
|
request: new Request(`https://example.test${pathname}`, { method: "POST", body: new FormData() }),
|
|
params,
|
|
context: {},
|
|
unstable_pattern: pathname.slice(1),
|
|
} as ActionFunctionArgs;
|
|
}
|
|
|
|
describe("help knowledge-base routes", () => {
|
|
it("delegates the help index loader and renders the index view", async () => {
|
|
const args = loaderArgs("/help/docs/en", { slug: "docs", lang: "en" });
|
|
const response: KbRoutesIndexApi.LoaderData = {
|
|
metatags: mockKb.metatags,
|
|
kb: mockKb as KbRoutesIndexApi.LoaderData["kb"],
|
|
search: undefined,
|
|
categories: [],
|
|
featured: [],
|
|
isAdmin: false,
|
|
};
|
|
vi.mocked(KbRoutesIndexApi.loader).mockResolvedValueOnce(response);
|
|
|
|
await expect(indexLoader(args)).resolves.toBe(response);
|
|
expect(KbRoutesIndexApi.loader).toHaveBeenCalledWith(args);
|
|
|
|
renderRoute(IndexRoute);
|
|
expect(screen.getByTestId("kb-index-view")).toBeInTheDocument();
|
|
});
|
|
|
|
it("delegates the help category loader and renders the category view", async () => {
|
|
const args = loaderArgs("/help/docs/en/categories/getting-started", { slug: "docs", lang: "en", category: "getting-started" });
|
|
const response: KbRoutesCategoryApi.LoaderData = {
|
|
metatags: mockCategory.metatags,
|
|
kb: mockKb as KbRoutesCategoryApi.LoaderData["kb"],
|
|
search: undefined,
|
|
item: mockCategory as KbRoutesCategoryApi.LoaderData["item"],
|
|
language: "en",
|
|
allCategories: [],
|
|
};
|
|
vi.mocked(KbRoutesCategoryApi.loader).mockResolvedValueOnce(response);
|
|
|
|
await expect(categoryLoader(args)).resolves.toBe(response);
|
|
expect(KbRoutesCategoryApi.loader).toHaveBeenCalledWith(args);
|
|
|
|
renderRoute(CategoryRoute);
|
|
expect(screen.getByTestId("kb-category-view")).toBeInTheDocument();
|
|
});
|
|
|
|
it("delegates the help article loader/action and renders the article view", async () => {
|
|
const loaderArguments = loaderArgs("/help/docs/en/articles/install", { slug: "docs", lang: "en", article: "install" });
|
|
const actionArguments = actionArgs("/help/docs/en/articles/install", { slug: "docs", lang: "en", article: "install" });
|
|
const loaderResponse: KbRoutesArticleApi.LoaderData = {
|
|
metatags: mockArticle.metatags,
|
|
kb: mockKb as KbRoutesArticleApi.LoaderData["kb"],
|
|
search: undefined,
|
|
item: {
|
|
article: mockArticle as NonNullable<KbRoutesArticleApi.LoaderData["item"]>["article"],
|
|
category: mockCategory as NonNullable<KbRoutesArticleApi.LoaderData["item"]>["category"],
|
|
},
|
|
userState: { hasThumbsUp: false, hasThumbsDown: false },
|
|
isAdmin: false,
|
|
categories: [],
|
|
};
|
|
const actionResponse = Response.json({ ok: true });
|
|
vi.mocked(KbRoutesArticleApi.loader).mockResolvedValueOnce(loaderResponse);
|
|
vi.mocked(KbRoutesArticleApi.action).mockResolvedValueOnce(actionResponse);
|
|
|
|
await expect(articleLoader(loaderArguments)).resolves.toBe(loaderResponse);
|
|
expect(KbRoutesArticleApi.loader).toHaveBeenCalledWith(loaderArguments);
|
|
|
|
await expect(articleAction(actionArguments)).resolves.toBe(actionResponse);
|
|
expect(KbRoutesArticleApi.action).toHaveBeenCalledWith(actionArguments);
|
|
|
|
renderRoute(ArticleRoute);
|
|
expect(screen.getByTestId("kb-article-view")).toBeInTheDocument();
|
|
});
|
|
});
|