|
| 1 | +import mongoose from 'mongoose'; |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | +import { Notification } from './Notification'; |
| 4 | + |
| 5 | +describe('Notification Model', () => { |
| 6 | + it('is compiled properly and exposed', () => { |
| 7 | + expect(Notification).toBeDefined(); |
| 8 | + expect(Notification.modelName).toBe('Notification'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('enforces required, unique, and lowercase fields on username', () => { |
| 12 | + const usernamePath = Notification.schema.path('username') as mongoose.SchemaType & { |
| 13 | + options: Record<string, unknown>; |
| 14 | + }; |
| 15 | + |
| 16 | + expect(usernamePath).toBeDefined(); |
| 17 | + expect(usernamePath.options.required).toBe(true); |
| 18 | + expect(usernamePath.options.unique).toBe(true); |
| 19 | + expect(usernamePath.options.lowercase).toBe(true); |
| 20 | + expect(usernamePath.options.trim).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('enforces required, lowercase, and trim fields on email', () => { |
| 24 | + const emailPath = Notification.schema.path('email') as mongoose.SchemaType & { |
| 25 | + options: Record<string, unknown>; |
| 26 | + }; |
| 27 | + |
| 28 | + expect(emailPath).toBeDefined(); |
| 29 | + expect(emailPath.options.required).toBe(true); |
| 30 | + expect(emailPath.options.lowercase).toBe(true); |
| 31 | + expect(emailPath.options.trim).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it('validates frequency field and its defaults and enums', () => { |
| 35 | + const frequencyPath = Notification.schema.path('frequency') as mongoose.SchemaType & { |
| 36 | + options: Record<string, unknown>; |
| 37 | + enumValues: string[]; |
| 38 | + }; |
| 39 | + |
| 40 | + expect(frequencyPath).toBeDefined(); |
| 41 | + expect(frequencyPath.options.default).toBe('daily'); |
| 42 | + expect(frequencyPath.enumValues).toContain('realtime'); |
| 43 | + expect(frequencyPath.enumValues).toContain('daily'); |
| 44 | + expect(frequencyPath.enumValues).toContain('weekly'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('checks boolean notification switches have default value as true', () => { |
| 48 | + const notifyOnCommitPath = Notification.schema.path('notifyOnCommit') as mongoose.SchemaType & { |
| 49 | + options: Record<string, unknown>; |
| 50 | + }; |
| 51 | + const notifyOnStreakPath = Notification.schema.path('notifyOnStreak') as mongoose.SchemaType & { |
| 52 | + options: Record<string, unknown>; |
| 53 | + }; |
| 54 | + const notifyOnMilestonePath = Notification.schema.path( |
| 55 | + 'notifyOnMilestone' |
| 56 | + ) as mongoose.SchemaType & { |
| 57 | + options: Record<string, unknown>; |
| 58 | + }; |
| 59 | + |
| 60 | + expect(notifyOnCommitPath.options.default).toBe(true); |
| 61 | + expect(notifyOnStreakPath.options.default).toBe(true); |
| 62 | + expect(notifyOnMilestonePath.options.default).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it('checks isActive default value is true', () => { |
| 66 | + const isActivePath = Notification.schema.path('isActive') as mongoose.SchemaType & { |
| 67 | + options: Record<string, unknown>; |
| 68 | + }; |
| 69 | + expect(isActivePath.options.default).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('checks createdAt and updatedAt have default values', () => { |
| 73 | + const createdAtPath = Notification.schema.path('createdAt') as mongoose.SchemaType & { |
| 74 | + options: Record<string, unknown>; |
| 75 | + }; |
| 76 | + const updatedAtPath = Notification.schema.path('updatedAt') as mongoose.SchemaType & { |
| 77 | + options: Record<string, unknown>; |
| 78 | + }; |
| 79 | + |
| 80 | + expect(createdAtPath.options.default).toBeDefined(); |
| 81 | + expect(updatedAtPath.options.default).toBeDefined(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments