Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions src/features/workbooks/zod/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect, test } from 'vitest';
import { type ZodSchema } from 'zod';
import { superValidate } from 'sveltekit-superforms/server';
import { zod4 } from 'sveltekit-superforms/adapters';

import { TaskGrade } from '$lib/types/task';
import { WorkBookType, type WorkBookTasks } from '$features/workbooks/types/workbook';
Expand Down Expand Up @@ -154,20 +156,9 @@ describe('workbook schema', () => {
});

test('when workbook tasks are given 200 tasks', () => {
const workBookTasks = [];

for (let index = 1; index <= 200; index++) {
workBookTasks.push({
workBookId: index,
taskId: generateRandomTaskId(),
priority: index,
comment: '',
});
}

const workbook: WorkBook = createWorkBookBase({
isPublished: true,
workBookTasks: workBookTasks,
workBookTasks: createWorkBookTasksWithCount(200),
});
validateWorkBookSchema(workBookSchema, workbook);
});
Expand Down Expand Up @@ -370,21 +361,19 @@ describe('workbook schema', () => {
});

test('when workbook tasks are given more than 200 tasks', () => {
const workBookTasks = [];

for (let index = 1; index <= 201; index++) {
workBookTasks.push({
workBookId: index,
taskId: generateRandomTaskId(),
priority: index,
comment: '',
});
}

const workbook: WorkBook = createWorkBookBase({
workBookTasks: workBookTasks,
workBookTasks: createWorkBookTasksWithCount(201),
});
validateWorkBookSchema(workBookSchema, workbook);
const result = workBookSchema.safeParse(workbook);

expect(result.success).toBe(false);
expect(result.error?.issues).toEqual([
expect.objectContaining({
code: 'too_big',
path: ['workBookTasks'],
message: '200問以下になるまで削除してください',
}),
]);
});

test('when a negative value is given as the workbook id', () => {
Expand Down Expand Up @@ -460,6 +449,27 @@ describe('workbook schema', () => {
}
});

describe('with the Superforms adapter', () => {
test('returns the task count error in workBookTasks._errors when more than 200 tasks are given', async () => {
const workbook: WorkBook = createWorkBookBase({
workBookTasks: createWorkBookTasksWithCount(201),
});
const form = await superValidate(workbook, zod4(workBookSchema));

expect(form.valid).toBe(false);
expect(form.errors.workBookTasks?._errors).toEqual(['200問以下になるまで削除してください']);
});
});

function createWorkBookTasksWithCount(count: number): WorkBookTasks {
return Array.from({ length: count }, (_, index) => ({
workBookId: index + 1,
taskId: generateRandomTaskId(),
priority: index + 1,
comment: '',
}));
}

// abcXXX_Y
function generateRandomTaskId(): string {
// Note: A random 3-digit number, prefixed with 0 if it is less than or equal to 2 digits.
Expand Down
Loading