Skip to content

Commit 4e4c797

Browse files
committed
🚨 Add tests for workbook schema (#2020)
1 parent be80b32 commit 4e4c797

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/lib/utils/url.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,23 @@ export function isValidUrl(rawUrl: string): boolean {
2222
*
2323
* A valid URL slug:
2424
* - Contains only lowercase letters (a-z), numbers (0-9), and hyphens (-)
25+
* - Must start with a lowercase letter or number
26+
* - Must end with a lowercase letter or number
27+
* - Cannot contain uppercase letters or special characters
28+
* - Cannot be entirely numeric
2529
* - Cannot start or end with a hyphen
2630
* - Cannot contain consecutive hyphens
2731
*/
2832
export function isValidUrlSlug(slug: string): boolean {
29-
const pattern = new RegExp(
30-
'^[a-z0-9]+(-[a-z0-9]+)*$', // a-z, 0-9, and hyphen (-) allowed
31-
);
33+
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(slug)) {
34+
return false;
35+
}
36+
37+
if (/^\d+$/.test(slug)) {
38+
return false;
39+
}
3240

33-
return !!pattern.test(slug);
41+
return true;
3442
}
3543

3644
export function sanitizeUrl(rawUrl: string) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ describe('URL', () => {
102102
{ rawUrl: '-' },
103103
{ rawUrl: '--' },
104104
{ rawUrl: 'ー' },
105+
{ rawUrl: '-1' },
106+
{ rawUrl: '0' },
107+
{ rawUrl: '1' },
108+
{ rawUrl: '2' },
109+
{ rawUrl: '9' },
110+
{ rawUrl: '10' },
111+
{ rawUrl: '99' },
112+
{ rawUrl: '100' },
105113
{ rawUrl: '1' },
106114
{ rawUrl: '2' },
107115
{ rawUrl: '9' },

src/test/lib/zod/workbook_schema.test.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe('workbook schema', () => {
8585
authorId: '1',
8686
isPublished: true,
8787
urlSlug: 'a',
88+
workBookType: WorkBookType.SOLUTION,
8889
});
8990
validateWorkBookSchema(workBookSchema, workbook);
9091
});
@@ -114,6 +115,7 @@ describe('workbook schema', () => {
114115
authorId: '1',
115116
isPublished: true,
116117
urlSlug: 'a'.repeat(30),
118+
workBookType: WorkBookType.SOLUTION,
117119
});
118120
validateWorkBookSchema(workBookSchema, workbook);
119121
});
@@ -123,6 +125,7 @@ describe('workbook schema', () => {
123125
authorId: '1',
124126
isPublished: true,
125127
urlSlug: '',
128+
workBookType: WorkBookType.SOLUTION,
126129
});
127130
validateWorkBookSchema(workBookSchema, workbook);
128131
});
@@ -132,6 +135,7 @@ describe('workbook schema', () => {
132135
authorId: '1',
133136
isPublished: true,
134137
urlSlug: null,
138+
workBookType: WorkBookType.SOLUTION,
135139
});
136140
validateWorkBookSchema(workBookSchema, workbook);
137141
});
@@ -141,6 +145,7 @@ describe('workbook schema', () => {
141145
authorId: '1',
142146
isPublished: true,
143147
urlSlug: undefined,
148+
workBookType: WorkBookType.SOLUTION,
144149
});
145150
validateWorkBookSchema(workBookSchema, workbook);
146151
});
@@ -299,6 +304,27 @@ describe('workbook schema', () => {
299304
authorId: '1',
300305
isPublished: true,
301306
urlSlug: 'a'.repeat(31),
307+
workBookType: WorkBookType.SOLUTION,
308+
});
309+
validateWorkBookSchema(workBookSchema, workbook);
310+
});
311+
312+
test('when an invalid url slug is given a numeric', () => {
313+
const workbook: WorkBook = createWorkBookBase({
314+
authorId: '1',
315+
isPublished: true,
316+
urlSlug: '1',
317+
workBookType: WorkBookType.SOLUTION,
318+
});
319+
validateWorkBookSchema(workBookSchema, workbook);
320+
});
321+
322+
test('when an invalid url slug is given numerics', () => {
323+
const workbook: WorkBook = createWorkBookBase({
324+
authorId: '1',
325+
isPublished: true,
326+
urlSlug: '10',
327+
workBookType: WorkBookType.SOLUTION,
302328
});
303329
validateWorkBookSchema(workBookSchema, workbook);
304330
});
@@ -308,24 +334,27 @@ describe('workbook schema', () => {
308334
authorId: '1',
309335
isPublished: true,
310336
urlSlug: 'directed acyclic graph',
337+
workBookType: WorkBookType.SOLUTION,
311338
});
312339
validateWorkBookSchema(workBookSchema, workbook);
313340
});
314341

315-
test('when an invalid url slug is given hyphen', () => {
342+
test('when an invalid url slug is given with hyphen', () => {
316343
const workbook: WorkBook = createWorkBookBase({
317344
authorId: '1',
318345
isPublished: true,
319346
urlSlug: '-',
347+
workBookType: WorkBookType.SOLUTION,
320348
});
321349
validateWorkBookSchema(workBookSchema, workbook);
322350
});
323351

324-
test('when an invalid url slug is given hyphens', () => {
352+
test('when an invalid url slug is given with hyphens', () => {
325353
const workbook: WorkBook = createWorkBookBase({
326354
authorId: '1',
327355
isPublished: true,
328356
urlSlug: '--',
357+
workBookType: WorkBookType.SOLUTION,
329358
});
330359
validateWorkBookSchema(workBookSchema, workbook);
331360
});

0 commit comments

Comments
 (0)