Repository navigation
Expand file tree
/
Copy pathStudentProfile.test.ts
More file actions
107 lines (93 loc) · 3.74 KB
/
Copy pathStudentProfile.test.ts
File metadata and controls
107 lines (93 loc) · 3.74 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
import mongoose from 'mongoose';
import { describe, it, expect } from 'vitest';
import { StudentProfile } from './StudentProfile';
describe('StudentProfile Model', () => {
// Test Case 1: Model compilation and exposure
it('is compiled properly and exposed', () => {
expect(StudentProfile).toBeDefined();
expect(StudentProfile.modelName).toBe('StudentProfile');
});
// Test Case 2: Required name and email fields
it('enforces student name and email as required fields', () => {
const namePath = StudentProfile.schema.path('name') as mongoose.SchemaType & {
options: Record<string, unknown>;
};
const emailPath = StudentProfile.schema.path('email') as mongoose.SchemaType & {
options: Record<string, unknown>;
};
expect(namePath.options.required).toBe(true);
expect(emailPath.options.required).toBe(true);
});
// Test Case 3: githubUsername formats and constraints
it('enforces githubUsername formats and constraints', () => {
const githubUsernamePath = StudentProfile.schema.path(
'githubUsername'
) as mongoose.SchemaType & {
options: Record<string, unknown>;
};
expect(githubUsernamePath.options.required).toBe(true);
expect(githubUsernamePath.options.unique).toBe(true);
expect(githubUsernamePath.options.lowercase).toBe(true);
expect(githubUsernamePath.options.trim).toBe(true);
});
// Test Case 4: careerInterests as string array
it('enforces careerInterests to parse as a string array', () => {
const careerInterestsPath = StudentProfile.schema.path('careerInterests') as unknown as {
instance: string;
embeddedSchemaType?: { instance: string };
};
expect(careerInterestsPath).toBeDefined();
expect(careerInterestsPath.instance).toBe('Array');
expect(careerInterestsPath.embeddedSchemaType?.instance).toBe('String');
});
// Test Case 5: Custom validator on graduation years
it('enforces graduationYear validation constraints', () => {
const graduationYearPath = StudentProfile.schema.path(
'graduationYear'
) as mongoose.SchemaType & {
validators: Array<{ validator: (val: unknown) => boolean }>;
};
expect(graduationYearPath).toBeDefined();
expect(graduationYearPath.validators).toBeDefined();
expect(graduationYearPath.validators.length).toBeGreaterThan(0);
const validatorFn = graduationYearPath.validators[0].validator;
expect(validatorFn(2000)).toBe(true);
expect(validatorFn(2026)).toBe(true);
expect(validatorFn(2100)).toBe(true);
expect(validatorFn(1999)).toBe(false);
expect(validatorFn(2101)).toBe(false);
});
// Test Case 6: pre-save hook updates updatedAt
describe('pre-save hook', () => {
it('updates the updatedAt field to the current date/time on save', () => {
const schemaInternal = StudentProfile.schema as unknown as {
s?: {
hooks?: {
_pres?: Map<
string,
Array<{ fn: (this: mongoose.Document, ...args: unknown[]) => unknown }>
>;
};
};
};
const pres = schemaInternal.s?.hooks?._pres;
const saveHooks = pres?.get('save') || [];
const doc = new StudentProfile({
githubUsername: 'testuser',
name: 'Test User',
updatedAt: new Date(Date.now() - 100000),
});
for (const hook of saveHooks) {
if (typeof hook.fn === 'function') {
const fnWithMetadata = hook.fn as unknown as Record<string | symbol, unknown>;
const isBuiltIn = fnWithMetadata[Symbol.for('mongoose:built-in-middleware')];
if (!isBuiltIn) {
hook.fn.call(doc);
}
}
}
expect(doc.updatedAt.getTime()).toBeGreaterThan(Date.now() - 5000);
});
});
});