Skip to content

Commit 4a14999

Browse files
committed
🚨 Add tests for workbook (#2020)
1 parent 54c58a9 commit 4a14999

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

src/test/lib/utils/url.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from 'vitest';
22

3-
import { isValidUrl, sanitizeUrl } from '$lib/utils/url';
3+
import { isValidUrl, isValidUrlSlug, sanitizeUrl } from '$lib/utils/url';
44

55
type TestCaseForUrlValidation = {
66
rawUrl: string;
@@ -61,6 +61,67 @@ describe('URL', () => {
6161
}
6262
});
6363

64+
describe('is valid URL slug', () => {
65+
describe('when valid URL slugs are given', () => {
66+
const testCases = [
67+
{ rawUrl: 'a' },
68+
{ rawUrl: 'a'.repeat(2) },
69+
{ rawUrl: 'a'.repeat(30) },
70+
{ rawUrl: 'bfs' },
71+
{ rawUrl: 'dfs' },
72+
{ rawUrl: 'dp' },
73+
{ rawUrl: 'union-find' },
74+
{ rawUrl: 'warshall-floyd' },
75+
{ rawUrl: 'digit-dp' },
76+
{ rawUrl: '2-sat' },
77+
{ rawUrl: 'directed-acyclic-graph' },
78+
];
79+
80+
runTests('isValidUrlSlug', testCases, ({ rawUrl }: TestCaseForUrlValidation) => {
81+
expect(isValidUrlSlug(rawUrl)).toBeTruthy();
82+
});
83+
});
84+
85+
describe('when invalid URL slugs are given', () => {
86+
const testCases = [
87+
{ rawUrl: '' },
88+
{ rawUrl: '-bfs' },
89+
{ rawUrl: 'bfs-' },
90+
{ rawUrl: 'bfs--dfs' },
91+
{ rawUrl: 'Bfs' },
92+
{ rawUrl: 'A' },
93+
{ rawUrl: 'BFS' },
94+
{ rawUrl: 'Union_Find' },
95+
{ rawUrl: 'union_find' },
96+
{ rawUrl: 'union.find' },
97+
{ rawUrl: 'union/find' },
98+
{ rawUrl: 'union@find' },
99+
{ rawUrl: '@union-find' },
100+
{ rawUrl: 'union-find@' },
101+
{ rawUrl: 'directed acyclic graph' },
102+
{ rawUrl: '-' },
103+
{ rawUrl: '--' },
104+
{ rawUrl: 'ー' },
105+
{ rawUrl: '1' },
106+
{ rawUrl: '2' },
107+
{ rawUrl: '9' },
108+
{ rawUrl: '10' },
109+
];
110+
111+
runTests('isValidUrlSlug', testCases, ({ rawUrl }: TestCaseForUrlValidation) => {
112+
expect(isValidUrlSlug(rawUrl)).toBeFalsy();
113+
});
114+
});
115+
116+
function runTests(
117+
testName: string,
118+
testCases: TestCasesForUrlValidation,
119+
testFunction: (testCase: TestCaseForUrlValidation) => void,
120+
) {
121+
test.each(testCases)(`${testName}(rawUrl: $rawUrl)`, testFunction);
122+
}
123+
});
124+
64125
describe('sanitize URL', () => {
65126
describe('when sanitization is not required URLs are given', () => {
66127
const testCases = [

src/test/lib/utils/workbook.test.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect, test } from 'vitest';
2-
import { parseWorkBookId } from '$lib/utils/workbook';
2+
import { parseWorkBookId, parseWorkBookUrlSlug } from '$lib/utils/workbook';
33

44
type TestCase = {
55
slug: string;
6-
expected: number | null;
6+
expected: number | string | null;
77
};
88

99
type TestCases = TestCase[];
@@ -90,3 +90,47 @@ describe('Workbook', () => {
9090
}
9191
});
9292
});
93+
94+
describe('parse workbook URL slug', () => {
95+
describe('when valid URL slugs are given', () => {
96+
const testCases = [
97+
{ slug: 'a', expected: 'a' },
98+
{ slug: 'a'.repeat(30), expected: 'a'.repeat(30) },
99+
{ slug: 'bfs', expected: 'bfs' },
100+
{ slug: 'union-find', expected: 'union-find' },
101+
{ slug: '2-sat', expected: '2-sat' },
102+
];
103+
104+
runTests('parseWorkBookUrlSlug', testCases, ({ slug, expected }: TestCase) => {
105+
expect(parseWorkBookUrlSlug(slug)).toBe(expected);
106+
});
107+
});
108+
109+
describe('when invalid URL slugs are given', () => {
110+
const testCases = [
111+
{ slug: '', expected: null },
112+
{ slug: ' ', expected: null },
113+
{ slug: 'bfs dfs', expected: null },
114+
{ slug: 'invalid@slug', expected: null },
115+
{ slug: 'invalid#slug', expected: null },
116+
{ slug: 'invalid/slug', expected: null },
117+
{ slug: 'UPPERCASE', expected: null },
118+
{ slug: 'slug_with_underscore', expected: null },
119+
{ slug: '-invalid-start', expected: null },
120+
{ slug: 'invalid-end-', expected: null },
121+
{ slug: 'double--dash', expected: null },
122+
];
123+
124+
runTests('parseWorkBookUrlSlug', testCases, ({ slug, expected }: TestCase) => {
125+
expect(parseWorkBookUrlSlug(slug)).toBe(expected);
126+
});
127+
});
128+
129+
function runTests(
130+
testName: string,
131+
testCases: TestCases,
132+
testFunction: (testCase: TestCase) => void,
133+
) {
134+
test.each(testCases)(`${testName}(slug: $slug)`, testFunction);
135+
}
136+
});

0 commit comments

Comments
 (0)