forked from String-dxd/teacher-workspace-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePostView.validation.test.tsx
More file actions
127 lines (106 loc) · 4.17 KB
/
Copy pathCreatePostView.validation.test.tsx
File metadata and controls
127 lines (106 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { describe, expect, it } from 'vitest';
import type { SelectedEntity } from '~/components/comms/entity-selector';
import type { FormQuestion, ReminderConfig } from '~/data/mock-pg-announcements';
import { isCreatePostFormValid } from './createPostValidation';
import type { PostFormState } from './CreatePostView';
const recipient: SelectedEntity = {
id: '1',
label: 'P1 A',
type: 'group',
count: 30,
};
const validBase: PostFormState = {
kind: 'announcement',
title: 'Hello',
description: 'Body text',
descriptionDoc: {
type: 'doc',
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Body text' }] }],
},
selectedRecipients: [recipient],
responseType: 'view-only',
questions: [],
selectedStaff: [],
enquiryEmail: 'a@b.sg',
dueDate: '',
reminder: { type: 'NONE' },
websiteLinks: [],
shortcuts: [],
attachments: [],
photos: [],
};
describe('isCreatePostFormValid — announcement', () => {
it('passes with all required fields', () => {
expect(isCreatePostFormValid(validBase, 'post')).toBe(true);
});
it('fails when title is empty', () => {
expect(isCreatePostFormValid({ ...validBase, title: '' }, 'post')).toBe(false);
});
it('fails when description is empty', () => {
// PGTW-11 new rule
expect(isCreatePostFormValid({ ...validBase, description: '' }, 'post')).toBe(false);
});
it('fails when description exceeds 2000 characters', () => {
const longDesc = 'a'.repeat(2001);
expect(isCreatePostFormValid({ ...validBase, description: longDesc }, 'post')).toBe(false);
});
it('passes when description is exactly 2000 characters', () => {
const maxDesc = 'a'.repeat(2000);
expect(isCreatePostFormValid({ ...validBase, description: maxDesc }, 'post')).toBe(true);
});
it('fails when enquiry email is empty', () => {
expect(isCreatePostFormValid({ ...validBase, enquiryEmail: '' }, 'post')).toBe(false);
});
it('fails when recipients are empty', () => {
expect(isCreatePostFormValid({ ...validBase, selectedRecipients: [] }, 'post')).toBe(false);
});
});
describe('isCreatePostFormValid — post-with-response (form)', () => {
const formBase: PostFormState = {
...validBase,
kind: 'form',
responseType: 'acknowledge',
dueDate: '2099-12-31',
};
it('passes with all required fields for acknowledge', () => {
expect(isCreatePostFormValid(formBase, 'post-with-response')).toBe(true);
});
it('fails when due date is empty', () => {
expect(isCreatePostFormValid({ ...formBase, dueDate: '' }, 'post-with-response')).toBe(false);
});
it('passes when reminder.type is NONE (PGTW-11 drop the over-strict check)', () => {
// PGW allows NONE as a valid reminder choice.
const reminder: ReminderConfig = { type: 'NONE' };
expect(isCreatePostFormValid({ ...formBase, reminder }, 'post-with-response')).toBe(true);
});
const question: FormQuestion = { id: '1', text: 'Favourite colour?', type: 'free-text' };
it('passes with valid custom questions', () => {
expect(
isCreatePostFormValid({ ...formBase, questions: [question] }, 'post-with-response'),
).toBe(true);
});
it('fails when question text exceeds 120 characters', () => {
const long: FormQuestion = { ...question, text: 'a'.repeat(121) };
expect(isCreatePostFormValid({ ...formBase, questions: [long] }, 'post-with-response')).toBe(
false,
);
});
it('passes when question text is exactly 120 characters', () => {
const atLimit: FormQuestion = { ...question, text: 'a'.repeat(120) };
expect(isCreatePostFormValid({ ...formBase, questions: [atLimit] }, 'post-with-response')).toBe(
true,
);
});
it('fails when question description exceeds 250 characters', () => {
const long: FormQuestion = { ...question, description: 'a'.repeat(251) };
expect(isCreatePostFormValid({ ...formBase, questions: [long] }, 'post-with-response')).toBe(
false,
);
});
it('passes when question description is exactly 250 characters', () => {
const atLimit: FormQuestion = { ...question, description: 'a'.repeat(250) };
expect(isCreatePostFormValid({ ...formBase, questions: [atLimit] }, 'post-with-response')).toBe(
true,
);
});
});