|
| 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