|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 3 | +import { Repository, Between } from 'typeorm'; |
| 4 | +import { AnalyticsEvent } from '../entities/analytics-event.entity'; |
| 5 | +import { GetOnboardingFunnelProvider } from './get-onboarding-funnel.provider'; |
| 6 | + |
| 7 | +describe('GetOnboardingFunnelProvider', () => { |
| 8 | + let provider: GetOnboardingFunnelProvider; |
| 9 | + let mockRepository: jest.Mocked<Pick<Repository<AnalyticsEvent>, 'count'>>; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + mockRepository = { count: jest.fn() }; |
| 13 | + |
| 14 | + const module: TestingModule = await Test.createTestingModule({ |
| 15 | + providers: [ |
| 16 | + GetOnboardingFunnelProvider, |
| 17 | + { |
| 18 | + provide: getRepositoryToken(AnalyticsEvent), |
| 19 | + useValue: mockRepository, |
| 20 | + }, |
| 21 | + ], |
| 22 | + }).compile(); |
| 23 | + |
| 24 | + provider = module.get<GetOnboardingFunnelProvider>(GetOnboardingFunnelProvider); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return funnel stages with counts for happy path', async () => { |
| 32 | + mockRepository.count |
| 33 | + .mockResolvedValueOnce(100) |
| 34 | + .mockResolvedValueOnce(80) |
| 35 | + .mockResolvedValueOnce(65) |
| 36 | + .mockResolvedValueOnce(50) |
| 37 | + .mockResolvedValueOnce(40); |
| 38 | + |
| 39 | + const startDate = new Date('2026-01-01'); |
| 40 | + const endDate = new Date('2026-06-30'); |
| 41 | + const result = await provider.getFunnel(startDate, endDate); |
| 42 | + |
| 43 | + expect(result.startDate).toEqual(startDate); |
| 44 | + expect(result.endDate).toEqual(endDate); |
| 45 | + expect(result.totalUsers).toBe(100); |
| 46 | + expect(result.stages).toHaveLength(5); |
| 47 | + |
| 48 | + expect(result.stages[0]).toEqual({ |
| 49 | + name: 'Onboarding Started', |
| 50 | + eventName: 'onboarding_started', |
| 51 | + count: 100, |
| 52 | + }); |
| 53 | + expect(result.stages[1]).toEqual({ |
| 54 | + name: 'Profile Created', |
| 55 | + eventName: 'profile_created', |
| 56 | + count: 80, |
| 57 | + }); |
| 58 | + expect(result.stages[2]).toEqual({ |
| 59 | + name: 'Tutorial Viewed', |
| 60 | + eventName: 'tutorial_viewed', |
| 61 | + count: 65, |
| 62 | + }); |
| 63 | + expect(result.stages[3]).toEqual({ |
| 64 | + name: 'First Puzzle Attempted', |
| 65 | + eventName: 'first_puzzle_attempted', |
| 66 | + count: 50, |
| 67 | + }); |
| 68 | + expect(result.stages[4]).toEqual({ |
| 69 | + name: 'Onboarding Completed', |
| 70 | + eventName: 'onboarding_completed', |
| 71 | + count: 40, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(mockRepository.count).toHaveBeenCalledTimes(5); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle empty data gracefully', async () => { |
| 78 | + mockRepository.count.mockResolvedValue(0); |
| 79 | + |
| 80 | + const result = await provider.getFunnel(); |
| 81 | + |
| 82 | + expect(result.totalUsers).toBe(0); |
| 83 | + expect(result.stages).toHaveLength(5); |
| 84 | + result.stages.forEach((stage) => { |
| 85 | + expect(stage.count).toBe(0); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should apply date range filtering', async () => { |
| 90 | + mockRepository.count.mockResolvedValue(10); |
| 91 | + |
| 92 | + const startDate = new Date('2026-03-01'); |
| 93 | + const endDate = new Date('2026-03-31'); |
| 94 | + await provider.getFunnel(startDate, endDate); |
| 95 | + |
| 96 | + expect(mockRepository.count).toHaveBeenCalledTimes(5); |
| 97 | + |
| 98 | + for (const call of mockRepository.count.mock.calls) { |
| 99 | + const where = call[0].where; |
| 100 | + expect(where.eventName).toBeDefined(); |
| 101 | + expect(where.timestamp).toBeDefined(); |
| 102 | + expect(where.timestamp._value instanceof Date).toBe(true); |
| 103 | + } |
| 104 | + }); |
| 105 | +}); |
0 commit comments