Repository navigation
Expand file tree
/
Copy pathNotification.empty-fallback.test.ts
More file actions
86 lines (69 loc) · 3.29 KB
/
Copy pathNotification.empty-fallback.test.ts
File metadata and controls
86 lines (69 loc) · 3.29 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
import { describe, it, expect } from 'vitest';
import { Notification } from './Notification';
describe('Notification Model - Edge Cases & Empty/Missing Inputs Verification', () => {
// 1. Missing required fields (Empty Object)
it('throws validation errors when instantiated with completely empty parameters', () => {
const notification = new Notification({});
// validateSync() allows us to check schema rules without needing a real MongoDB connection
const err = notification.validateSync();
expect(err).toBeDefined();
expect(err?.errors.username).toBeDefined();
expect(err?.errors.username.message).toContain('Path `username` is required');
expect(err?.errors.email).toBeDefined();
expect(err?.errors.email.message).toContain('Path `email` is required');
});
// 2. Missing optional fields (Verify Default Fallbacks)
it('applies default fallback configuration values when optional fields are completely missing', () => {
const notification = new Notification({
username: 'testuser',
});
const err = notification.validateSync();
// Should have no validation errors since required fields are present
expect(err).toBeUndefined();
// Verify all the unconfigured object fields safely fell back to schema defaults
expect(notification.frequency).toBe('daily');
expect(notification.notifyOnCommit).toBe(true);
expect(notification.notifyOnStreak).toBe(true);
expect(notification.notifyOnMilestone).toBe(true);
expect(notification.isActive).toBe(true);
expect(notification.createdAt).toBeDefined();
expect(notification.updatedAt).toBeDefined();
});
// 3. Empty string/whitespace handling
it('fails validation safely if required strings contain only empty spaces', () => {
const notification = new Notification({
username: ' ', // Schema has `trim: true`, so this reduces to an empty string
email: ' ',
});
const err = notification.validateSync();
expect(err).toBeDefined();
// Mongoose recognizes the trimmed empty string as a missing required field
expect(err?.errors.username).toBeDefined();
expect(err?.errors.email).toBeDefined();
});
// 4. Invalid Enum Inputs (Missing standard layout state)
it('rejects invalid inputs for enumerated fields and prevents database corruption', () => {
const notification = new Notification({
username: 'testuser',
frequency: 'hourly', // Invalid! Valid options are: realtime, daily, weekly
});
const err = notification.validateSync();
expect(err).toBeDefined();
expect(err?.errors.frequency).toBeDefined();
expect(err?.errors.frequency.message).toContain('is not a valid enum value');
});
// 5. Type Casting Failures (Hydration/Runtime Error Resilience)
it('gracefully handles invalid types by throwing standard cast errors instead of crashing', () => {
const notification = new Notification({
username: 'testuser',
notifyOnCommit: 'not-a-boolean-value', // Injecting a raw string into a boolean field
});
const err = notification.validateSync();
expect(err).toBeDefined();
expect(err?.errors.notifyOnCommit).toBeDefined();
expect(err?.errors.notifyOnCommit.name).toBe('CastError');
});
});