115 lines
3.7 KiB
TypeScript
115 lines
3.7 KiB
TypeScript
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);
|
|
});
|
|
});
|