263 lines
8.5 KiB
TypeScript
263 lines
8.5 KiB
TypeScript
import {
|
|
CreateChapterValidator,
|
|
UpdateChapterValidator,
|
|
ReorderChapterValidator,
|
|
CreateLessonValidator,
|
|
UpdateLessonValidator,
|
|
ReorderLessonsValidator,
|
|
AddQuestionValidator,
|
|
UpdateQuizValidator,
|
|
} from '@/validators/ChaptersLesson.validator';
|
|
|
|
|
|
// ============================================================
|
|
// Chapter Validators
|
|
// ============================================================
|
|
|
|
describe('CreateChapterValidator', () => {
|
|
it('should pass with valid data', () => {
|
|
const { error } = CreateChapterValidator.validate({
|
|
title: { th: 'บทที่ 1', en: 'Chapter 1' },
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with optional fields', () => {
|
|
const { error } = CreateChapterValidator.validate({
|
|
title: { th: 'บทที่ 1', en: 'Chapter 1' },
|
|
description: { th: 'คำอธิบาย', en: 'Description' },
|
|
sort_order: 0,
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail if title is missing', () => {
|
|
const { error } = CreateChapterValidator.validate({});
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail if title.th is missing', () => {
|
|
const { error } = CreateChapterValidator.validate({
|
|
title: { en: 'Chapter 1' },
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail if title.en is missing', () => {
|
|
const { error } = CreateChapterValidator.validate({
|
|
title: { th: 'บทที่ 1' },
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('UpdateChapterValidator', () => {
|
|
it('should pass with empty object (all fields optional)', () => {
|
|
const { error } = UpdateChapterValidator.validate({});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with partial fields', () => {
|
|
const { error } = UpdateChapterValidator.validate({
|
|
is_published: true,
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('ReorderChapterValidator', () => {
|
|
it('should pass with valid sort_order', () => {
|
|
const { error } = ReorderChapterValidator.validate({ sort_order: 0 });
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail without sort_order', () => {
|
|
const { error } = ReorderChapterValidator.validate({});
|
|
expect(error).toBeDefined();
|
|
expect(error?.details[0].message).toMatch(/Sort order is required/i);
|
|
});
|
|
|
|
it('should fail with negative sort_order', () => {
|
|
const { error } = ReorderChapterValidator.validate({ sort_order: -1 });
|
|
expect(error).toBeDefined();
|
|
});
|
|
});
|
|
|
|
// ============================================================
|
|
// Lesson Validators
|
|
// ============================================================
|
|
|
|
describe('CreateLessonValidator', () => {
|
|
it('should pass with VIDEO type', () => {
|
|
const { error } = CreateLessonValidator.validate({
|
|
title: { th: 'บทเรียน 1', en: 'Lesson 1' },
|
|
type: 'VIDEO',
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with QUIZ type', () => {
|
|
const { error } = CreateLessonValidator.validate({
|
|
title: { th: 'แบบทดสอบ', en: 'Quiz' },
|
|
type: 'QUIZ',
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail with invalid type', () => {
|
|
const { error } = CreateLessonValidator.validate({
|
|
title: { th: 'บทเรียน', en: 'Lesson' },
|
|
type: 'DOCUMENT',
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail without title', () => {
|
|
const { error } = CreateLessonValidator.validate({ type: 'VIDEO' });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail without type', () => {
|
|
const { error } = CreateLessonValidator.validate({
|
|
title: { th: 'บทเรียน', en: 'Lesson' },
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('UpdateLessonValidator — prerequisite_lesson_ids', () => {
|
|
it('should pass with valid array of ids', () => {
|
|
const { error } = UpdateLessonValidator.validate({
|
|
prerequisite_lesson_ids: [1, 2, 3],
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with null (clear prerequisites)', () => {
|
|
const { error } = UpdateLessonValidator.validate({
|
|
prerequisite_lesson_ids: null,
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with empty array (clear prerequisites)', () => {
|
|
const { error } = UpdateLessonValidator.validate({
|
|
prerequisite_lesson_ids: [],
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass without the field (no change)', () => {
|
|
const { error } = UpdateLessonValidator.validate({});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail with non-integer ids', () => {
|
|
const { error } = UpdateLessonValidator.validate({
|
|
prerequisite_lesson_ids: [1.5],
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail with negative ids', () => {
|
|
const { error } = UpdateLessonValidator.validate({
|
|
prerequisite_lesson_ids: [-1],
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail with string ids', () => {
|
|
const { error } = UpdateLessonValidator.validate({
|
|
prerequisite_lesson_ids: ['abc'],
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('ReorderLessonsValidator', () => {
|
|
it('should pass with valid data', () => {
|
|
const { error } = ReorderLessonsValidator.validate({
|
|
lesson_id: 1,
|
|
sort_order: 0,
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail without lesson_id', () => {
|
|
const { error } = ReorderLessonsValidator.validate({ sort_order: 0 });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail without sort_order', () => {
|
|
const { error } = ReorderLessonsValidator.validate({ lesson_id: 1 });
|
|
expect(error).toBeDefined();
|
|
});
|
|
});
|
|
|
|
// ============================================================
|
|
// Quiz Validators
|
|
// ============================================================
|
|
|
|
describe('AddQuestionValidator', () => {
|
|
it('should pass with MULTIPLE_CHOICE type + choices', () => {
|
|
const { error } = AddQuestionValidator.validate({
|
|
question: { th: 'ข้อที่ 1 คืออะไร?', en: 'What is question 1?' },
|
|
question_type: 'MULTIPLE_CHOICE',
|
|
choices: [
|
|
{ text: { th: 'ก', en: 'A' }, is_correct: true },
|
|
{ text: { th: 'ข', en: 'B' }, is_correct: false },
|
|
],
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with TRUE_FALSE type without choices', () => {
|
|
const { error } = AddQuestionValidator.validate({
|
|
question: { th: 'ถูกหรือผิด?', en: 'True or False?' },
|
|
question_type: 'TRUE_FALSE',
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail with invalid question_type', () => {
|
|
const { error } = AddQuestionValidator.validate({
|
|
question: { th: 'คำถาม', en: 'Question' },
|
|
question_type: 'ESSAY',
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail without question', () => {
|
|
const { error } = AddQuestionValidator.validate({
|
|
question_type: 'TRUE_FALSE',
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('UpdateQuizValidator', () => {
|
|
it('should pass with empty object (all optional)', () => {
|
|
const { error } = UpdateQuizValidator.validate({});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with valid passing_score', () => {
|
|
const { error } = UpdateQuizValidator.validate({ passing_score: 70 });
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail with passing_score > 100', () => {
|
|
const { error } = UpdateQuizValidator.validate({ passing_score: 101 });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail with passing_score < 0', () => {
|
|
const { error } = UpdateQuizValidator.validate({ passing_score: -1 });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should pass with time_limit 0 (no limit)', () => {
|
|
const { error } = UpdateQuizValidator.validate({ time_limit: 0 });
|
|
expect(error).toBeUndefined();
|
|
});
|
|
});
|