feat: Add unit tests for backend validators and configure Jest.
This commit is contained in:
parent
ebcae0b3e7
commit
9bb941b45e
16 changed files with 2071 additions and 381 deletions
|
|
@ -0,0 +1,150 @@
|
|||
import {
|
||||
CreateCourseValidator,
|
||||
UpdateCourseValidator,
|
||||
CloneCourseValidator,
|
||||
addInstructorCourseValidator,
|
||||
} from '@/validators/CoursesInstructor.validator';
|
||||
|
||||
// ============================================================
|
||||
// addInstructorCourseValidator
|
||||
// ============================================================
|
||||
|
||||
describe('addInstructorCourseValidator', () => {
|
||||
it('should pass with valid user_id and course_id', () => {
|
||||
const { error } = addInstructorCourseValidator.validate({
|
||||
user_id: 1,
|
||||
course_id: 2,
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should fail without user_id', () => {
|
||||
const { error } = addInstructorCourseValidator.validate({ course_id: 2 });
|
||||
expect(error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should fail without course_id', () => {
|
||||
const { error } = addInstructorCourseValidator.validate({ user_id: 1 });
|
||||
expect(error).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================
|
||||
// CreateCourseValidator
|
||||
// ============================================================
|
||||
|
||||
describe('CreateCourseValidator', () => {
|
||||
const validPayload = {
|
||||
category_id: 1,
|
||||
title: { th: 'คอร์สทดสอบ', en: 'Test Course' },
|
||||
slug: 'test-course',
|
||||
description: { th: 'คำอธิบาย', en: 'Description' },
|
||||
price: 500,
|
||||
is_free: false,
|
||||
have_certificate: true,
|
||||
};
|
||||
|
||||
it('should pass with all required fields', () => {
|
||||
const { error } = CreateCourseValidator.validate(validPayload);
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should fail if title.th is missing', () => {
|
||||
const { error } = CreateCourseValidator.validate({
|
||||
...validPayload,
|
||||
title: { en: 'Test Course' },
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should fail if slug is missing', () => {
|
||||
const { error } = CreateCourseValidator.validate({
|
||||
...validPayload,
|
||||
slug: undefined,
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should fail if price is missing', () => {
|
||||
const { error } = CreateCourseValidator.validate({
|
||||
...validPayload,
|
||||
price: undefined,
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should fail if is_free is missing', () => {
|
||||
const { error } = CreateCourseValidator.validate({
|
||||
...validPayload,
|
||||
is_free: undefined,
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should allow price = 0 (free course)', () => {
|
||||
const { error } = CreateCourseValidator.validate({
|
||||
...validPayload,
|
||||
price: 0,
|
||||
is_free: true,
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================
|
||||
// UpdateCourseValidator
|
||||
// ============================================================
|
||||
|
||||
describe('UpdateCourseValidator', () => {
|
||||
it('should pass with empty object (all optional)', () => {
|
||||
const { error } = UpdateCourseValidator.validate({});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should pass with partial update', () => {
|
||||
const { error } = UpdateCourseValidator.validate({ price: 999 });
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should pass with title partial update (th only)', () => {
|
||||
const { error } = UpdateCourseValidator.validate({
|
||||
title: { th: 'ชื่อใหม่' },
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================
|
||||
// CloneCourseValidator
|
||||
// ============================================================
|
||||
|
||||
describe('CloneCourseValidator', () => {
|
||||
it('should pass with valid title', () => {
|
||||
const { error } = CloneCourseValidator.validate({
|
||||
title: { th: 'คอร์ส Copy', en: 'Course Copy' },
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should fail without title.th', () => {
|
||||
const { error } = CloneCourseValidator.validate({
|
||||
title: { en: 'Course Copy' },
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/Thai title is required/i);
|
||||
});
|
||||
|
||||
it('should fail without title.en', () => {
|
||||
const { error } = CloneCourseValidator.validate({
|
||||
title: { th: 'คอร์ส Copy' },
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/English title is required/i);
|
||||
});
|
||||
|
||||
it('should fail without title entirely', () => {
|
||||
const { error } = CloneCourseValidator.validate({});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/Title is required/i);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue