elearning/Backend/tests/unit/validators/CoursesStudent.validator.test.ts

97 lines
3.3 KiB
TypeScript
Raw Normal View History

import {
SaveVideoProgressValidator,
SubmitQuizValidator,
} from '@/validators/CoursesStudent.validator';
describe('SaveVideoProgressValidator', () => {
it('should pass with required field only', () => {
const { error } = SaveVideoProgressValidator.validate({
video_progress_seconds: 60,
});
expect(error).toBeUndefined();
});
it('should pass with all fields', () => {
const { error } = SaveVideoProgressValidator.validate({
video_progress_seconds: 120,
video_duration_seconds: 600,
});
expect(error).toBeUndefined();
});
it('should pass with progress = 0 (start of video)', () => {
const { error } = SaveVideoProgressValidator.validate({
video_progress_seconds: 0,
});
expect(error).toBeUndefined();
});
it('should fail without video_progress_seconds', () => {
const { error } = SaveVideoProgressValidator.validate({});
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/Video progress seconds is required/i);
});
it('should fail with negative progress', () => {
const { error } = SaveVideoProgressValidator.validate({
video_progress_seconds: -1,
});
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/at least 0/i);
});
it('should fail with negative video duration', () => {
const { error } = SaveVideoProgressValidator.validate({
video_progress_seconds: 10,
video_duration_seconds: -1,
});
expect(error).toBeDefined();
});
});
describe('SubmitQuizValidator', () => {
const validAnswer = { question_id: 1, choice_id: 2 };
it('should pass with valid answers', () => {
const { error } = SubmitQuizValidator.validate({
answers: [validAnswer, { question_id: 2, choice_id: 5 }],
});
expect(error).toBeUndefined();
});
it('should fail without answers', () => {
const { error } = SubmitQuizValidator.validate({});
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/Answers are required/i);
});
it('should fail with empty answers array', () => {
const { error } = SubmitQuizValidator.validate({ answers: [] });
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/At least one answer/i);
});
it('should fail if question_id is missing in an answer', () => {
const { error } = SubmitQuizValidator.validate({
answers: [{ choice_id: 2 }],
});
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/Question ID is required/i);
});
it('should fail if choice_id is missing in an answer', () => {
const { error } = SubmitQuizValidator.validate({
answers: [{ question_id: 1 }],
});
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/Choice ID is required/i);
});
it('should fail if question_id is not a positive integer', () => {
const { error } = SubmitQuizValidator.validate({
answers: [{ question_id: -1, choice_id: 1 }],
});
expect(error).toBeDefined();
});
});