Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ab6971

Browse files
committed
additional tests
1 parent 5596366 commit 8ab6971

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

‎tests/configureUserSelection.test.ts‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import { describe, it, expect, vi, beforeEach } from "vitest";
2+
3+
import { CategoryType, LanguageType } from "../src/types";
4+
import { configureUserSelection } from "../src/utils/configureUserSelection";
5+
import { defaultCategory, defaultLanguage } from "../src/utils/consts";
6+
import { slugify } from "../src/utils/slugify";
7+
8+
vi.mock("../src/utils/slugify");
9+
10+
describe("configureUserSelection", () => {
11+
beforeEach(() => {
12+
vi.clearAllMocks();
13+
});
14+
15+
const mockFetch = (urlResponses: Record<string, unknown>) => {
16+
global.fetch = vi.fn(async (url) => {
17+
const response = urlResponses[url as string];
18+
if (response instanceof Error) {
19+
throw response;
20+
}
21+
return {
22+
json: async () => response,
23+
};
24+
}) as unknown as typeof fetch;
25+
};
26+
27+
it("should return default language and category if no arguments are provided", async () => {
28+
mockFetch({
29+
"/consolidated/_index.json": [],
30+
});
31+
32+
const result = await configureUserSelection({
33+
languageName: undefined,
34+
categoryName: undefined,
35+
});
36+
37+
expect(result).toEqual({
38+
language: defaultLanguage,
39+
category: defaultCategory.name,
40+
});
41+
42+
expect(fetch).toHaveBeenCalledWith("/consolidated/_index.json");
43+
});
44+
45+
it("should match the language and default to the first category if categoryName is undefined", async () => {
46+
const mockLanguages: LanguageType[] = [
47+
{
48+
name: "JavaScript",
49+
icon: "js-icon",
50+
subIndexes: [],
51+
},
52+
{
53+
name: "Python",
54+
icon: "python-icon",
55+
subIndexes: [],
56+
},
57+
];
58+
const mockCategories: CategoryType[] = [
59+
{
60+
name: "Basics",
61+
snippets: [],
62+
},
63+
{
64+
name: "Advanced",
65+
snippets: [],
66+
},
67+
];
68+
69+
mockFetch({
70+
"/consolidated/_index.json": mockLanguages,
71+
"/consolidated/javascript.json": mockCategories,
72+
});
73+
74+
vi.mocked(slugify).mockImplementation((str) => str.toLowerCase());
75+
76+
const result = await configureUserSelection({
77+
languageName: "JavaScript",
78+
categoryName: undefined,
79+
});
80+
81+
expect(result).toEqual({
82+
language: mockLanguages[0],
83+
category: mockCategories[0].name,
84+
});
85+
86+
expect(slugify).toHaveBeenCalledWith("JavaScript");
87+
expect(fetch).toHaveBeenCalledWith("/consolidated/_index.json");
88+
expect(fetch).toHaveBeenCalledWith("/consolidated/javascript.json");
89+
});
90+
91+
it("should match the language and specific category if both arguments are provided", async () => {
92+
const mockLanguages: LanguageType[] = [
93+
{
94+
name: "JavaScript",
95+
icon: "js-icon",
96+
subIndexes: [],
97+
},
98+
{
99+
name: "Python",
100+
icon: "python-icon",
101+
subIndexes: [],
102+
},
103+
];
104+
const mockCategories: CategoryType[] = [
105+
{
106+
name: "Basics",
107+
snippets: [],
108+
},
109+
{
110+
name: "Advanced",
111+
snippets: [],
112+
},
113+
];
114+
115+
mockFetch({
116+
"/consolidated/_index.json": mockLanguages,
117+
"/consolidated/javascript.json": mockCategories,
118+
});
119+
120+
vi.mocked(slugify).mockImplementation((str) => str.toLowerCase());
121+
122+
const result = await configureUserSelection({
123+
languageName: "JavaScript",
124+
categoryName: "Advanced",
125+
});
126+
127+
expect(result).toEqual({
128+
language: mockLanguages[0],
129+
category: mockCategories[1].name,
130+
});
131+
132+
expect(slugify).toHaveBeenCalledWith("JavaScript");
133+
expect(slugify).toHaveBeenCalledWith("Advanced");
134+
expect(fetch).toHaveBeenCalledWith("/consolidated/_index.json");
135+
expect(fetch).toHaveBeenCalledWith("/consolidated/javascript.json");
136+
});
137+
138+
it("should return default category if category fetch fails", async () => {
139+
const mockLanguages: LanguageType[] = [
140+
{
141+
name: "JavaScript",
142+
icon: "js-icon",
143+
subIndexes: [],
144+
},
145+
{
146+
name: "Python",
147+
icon: "python-icon",
148+
subIndexes: [],
149+
},
150+
];
151+
152+
mockFetch({
153+
"/consolidated/_index.json": mockLanguages,
154+
"/consolidated/javascript.json": new Error("Network error"),
155+
});
156+
157+
vi.mocked(slugify).mockImplementation((str) => str.toLowerCase());
158+
159+
const result = await configureUserSelection({
160+
languageName: "JavaScript",
161+
categoryName: undefined,
162+
});
163+
164+
expect(result).toEqual({
165+
language: mockLanguages[0],
166+
category: defaultCategory.name,
167+
});
168+
169+
expect(slugify).toHaveBeenCalledWith("JavaScript");
170+
expect(fetch).toHaveBeenCalledWith("/consolidated/_index.json");
171+
expect(fetch).toHaveBeenCalledWith("/consolidated/javascript.json");
172+
});
173+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /