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
115
Backend/tests/unit/validators/announcements.validator.test.ts
Normal file
115
Backend/tests/unit/validators/announcements.validator.test.ts
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import {
|
||||
CreateAnnouncementValidator,
|
||||
UpdateAnnouncementValidator,
|
||||
} from '@/validators/announcements.validator';
|
||||
|
||||
describe('CreateAnnouncementValidator', () => {
|
||||
const validPayload = {
|
||||
title: { th: 'ประกาศใหม่', en: 'New Announcement' },
|
||||
content: { th: 'เนื้อหา', en: 'Content' },
|
||||
status: 'DRAFT',
|
||||
is_pinned: false,
|
||||
};
|
||||
|
||||
it('should pass with all required fields', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate(validPayload);
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should pass with optional published_at as ISO date', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
published_at: '2026-03-01T00:00:00.000Z',
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should fail with invalid published_at format', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
published_at: '01-03-2026',
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/ISO date/i);
|
||||
});
|
||||
|
||||
it('should fail with invalid status', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
status: 'HIDDEN',
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/DRAFT, PUBLISHED, ARCHIVED/i);
|
||||
});
|
||||
|
||||
it('should pass with PUBLISHED status', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
status: 'PUBLISHED',
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should pass with ARCHIVED status', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
status: 'ARCHIVED',
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should fail without title', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
title: undefined,
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/Title is required/i);
|
||||
});
|
||||
|
||||
it('should fail without content', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
content: undefined,
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/Content is required/i);
|
||||
});
|
||||
|
||||
it('should fail without is_pinned', () => {
|
||||
const { error } = CreateAnnouncementValidator.validate({
|
||||
...validPayload,
|
||||
is_pinned: undefined,
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/is_pinned is required/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('UpdateAnnouncementValidator', () => {
|
||||
it('should pass with empty object (all optional)', () => {
|
||||
const { error } = UpdateAnnouncementValidator.validate({});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should pass with partial update', () => {
|
||||
const { error } = UpdateAnnouncementValidator.validate({
|
||||
status: 'PUBLISHED',
|
||||
is_pinned: true,
|
||||
});
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should fail with invalid status', () => {
|
||||
const { error } = UpdateAnnouncementValidator.validate({ status: 'DELETED' });
|
||||
expect(error).toBeDefined();
|
||||
});
|
||||
|
||||
it('should fail with invalid published_at format', () => {
|
||||
const { error } = UpdateAnnouncementValidator.validate({
|
||||
published_at: 'not-a-date',
|
||||
});
|
||||
expect(error).toBeDefined();
|
||||
expect(error?.details[0].message).toMatch(/ISO date/i);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue