Skip to content

Commit 1d845d1

Browse files
authored
test(NotificationModel-type-compiler): verify TypeScript Compiler Validation & Schema Constraints Stability (Variation 10) (JhaSourav07#3103)
## Description Fixes JhaSourav07#2902 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [x] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support. closes JhaSourav07#2902
2 parents 6f10de5 + ca85612 commit 1d845d1

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, it, expect, expectTypeOf } from 'vitest';
2+
import { INotification, Notification } from './Notification';
3+
import { Document } from 'mongoose';
4+
5+
describe('Notification.ts - TypeScript Compiler Validation & Schema Constraints Stability', () => {
6+
it('Import the interfaces, types, or validation schemas associated with the file.', () => {
7+
// 1st condition: Ensure types and schemas are importable
8+
expect(Notification).toBeDefined();
9+
// Simply asserting that the exported mongoose Model exists and is a valid constructor
10+
expect(typeof Notification).toBe('function');
11+
});
12+
13+
it('Use type-testing assertions (expectTypeOf) to enforce field property configurations.', () => {
14+
// 2nd condition: Enforce field properties via vitest's expectTypeOf
15+
// This is largely a compile-time check but works in vitest runners natively.
16+
expectTypeOf<INotification>().toHaveProperty('username').toBeString();
17+
expectTypeOf<INotification>().toHaveProperty('email').toBeString();
18+
expectTypeOf<INotification>().toHaveProperty('notifyOnCommit').toBeBoolean();
19+
expectTypeOf<INotification>().toHaveProperty('createdAt').toEqualTypeOf<Date>();
20+
});
21+
22+
it('Assert that invalid prop parameters are blocked during static type checking.', () => {
23+
// 3rd condition: Block invalid props (runtime mongoose schematic equivalent verification)
24+
const invalidDoc = new Notification({
25+
username: 'testuser',
26+
// Intentionally omitting required 'email'
27+
});
28+
29+
// Mongoose synchronusly validates and returns an error for invalid constraints
30+
const validationError = invalidDoc.validateSync();
31+
expect(validationError).toBeDefined();
32+
expect(validationError!.errors.email.name).toBe('ValidatorError');
33+
expect(validationError!.errors.email.kind).toBe('required');
34+
});
35+
36+
it('Verify custom types accept optional values without compile errors.', () => {
37+
// 4th condition: Verify standard TS partiality/optional values
38+
// Using TS standard utility types to confirm flexible compatibility
39+
type OptionalNotification = Partial<INotification>;
40+
41+
// Testing object declaration won't fail static compile
42+
const strictlyPartialData: OptionalNotification = {
43+
email: 'partial@example.com',
44+
};
45+
46+
expectTypeOf(strictlyPartialData).toMatchTypeOf<Partial<INotification>>();
47+
expect(strictlyPartialData.email).toBe('partial@example.com');
48+
expect(strictlyPartialData.username).toBeUndefined(); // strictly isolated optional value
49+
});
50+
51+
it('Verify schema validation constraints return strict validation reports.', () => {
52+
// 5th condition: Schema constraints returning strict reporting objects
53+
const docWithInvalidEnum = new Notification({
54+
username: 'test',
55+
email: 'test@example.com',
56+
frequency: 'not-a-valid-frequency', // Must be 'realtime' | 'daily' | 'weekly'
57+
});
58+
59+
const errorReport = docWithInvalidEnum.validateSync();
60+
expect(errorReport).toBeDefined();
61+
expect(errorReport!.errors.frequency).toBeDefined();
62+
expect(errorReport!.errors.frequency.kind).toBe('enum');
63+
});
64+
});

0 commit comments

Comments
 (0)