1+ import { Test , TestingModule } from '@nestjs/testing' ;
2+ import { getRepositoryToken } from '@nestjs/typeorm' ;
3+ import { ConfigService } from '@nestjs/config' ;
4+ import { CacheWarmingModule } from './cache-warming.module' ;
5+ import { CacheWarmingService } from './cache-warming.service' ;
6+ import { CacheWarmingScheduler } from './cache-warming.scheduler' ;
7+ import { RedisCacheService } from './redis-cache.service' ;
8+ import { CacheWarmingController } from './cache-warming.controller' ;
9+ import { CacheJob } from './entities/cache-job.entity' ;
10+ import { Metric } from './entities/metric.entity' ;
11+ import { PreloadData } from './entities/preload-data.entity' ;
12+
13+ describe ( 'CacheWarmingModule' , ( ) => {
14+ let module : TestingModule ;
15+
16+ const mockRepository = {
17+ find : jest . fn ( ) . mockResolvedValue ( [ ] ) ,
18+ create : jest . fn ( ) . mockReturnValue ( { } ) ,
19+ save : jest . fn ( ) . mockResolvedValue ( { } ) ,
20+ } ;
21+
22+ const mockRedisCacheService = {
23+ get : jest . fn ( ) . mockResolvedValue ( null ) ,
24+ set : jest . fn ( ) . mockResolvedValue ( 'OK' ) ,
25+ del : jest . fn ( ) . mockResolvedValue ( 1 ) ,
26+ getStats : jest . fn ( ) . mockResolvedValue ( { hits : 0 , misses : 0 , hitRate : 0 } ) ,
27+ } ;
28+
29+ const mockConfigService = {
30+ get : jest . fn ( ) . mockReturnValue ( 'test-value' ) ,
31+ } ;
32+
33+ beforeEach ( async ( ) => {
34+ module = await Test . createTestingModule ( {
35+ imports : [ ] ,
36+ providers : [
37+ CacheWarmingModule ,
38+ CacheWarmingService ,
39+ CacheWarmingScheduler ,
40+ RedisCacheService ,
41+ CacheWarmingController ,
42+ {
43+ provide : getRepositoryToken ( CacheJob ) ,
44+ useValue : mockRepository ,
45+ } ,
46+ {
47+ provide : getRepositoryToken ( Metric ) ,
48+ useValue : mockRepository ,
49+ } ,
50+ {
51+ provide : getRepositoryToken ( PreloadData ) ,
52+ useValue : mockRepository ,
53+ } ,
54+ {
55+ provide : RedisCacheService ,
56+ useValue : mockRedisCacheService ,
57+ } ,
58+ {
59+ provide : ConfigService ,
60+ useValue : mockConfigService ,
61+ } ,
62+ ] ,
63+ } ) . compile ( ) ;
64+ } ) ;
65+
66+ it ( 'should export all required providers' , ( ) => {
67+ // Verify that the module defines all necessary providers
68+ const moduleMetadata = Reflect . getMetadata ( 'providers' , CacheWarmingModule ) ;
69+ expect ( moduleMetadata ) . toContain ( CacheWarmingService ) ;
70+ expect ( moduleMetadata ) . toContain ( CacheWarmingScheduler ) ;
71+ expect ( moduleMetadata ) . toContain ( RedisCacheService ) ;
72+ } ) ;
73+
74+ it ( 'should have all controllers defined' , ( ) => {
75+ const moduleMetadata = Reflect . getMetadata ( 'controllers' , CacheWarmingModule ) ;
76+ expect ( moduleMetadata ) . toContain ( CacheWarmingController ) ;
77+ } ) ;
78+
79+ it ( 'should import TypeOrmModule with correct entities' , ( ) => {
80+ const moduleMetadata = Reflect . getMetadata ( 'imports' , CacheWarmingModule ) ;
81+ const typeOrmModuleImport = moduleMetadata . find ( imp => imp . imports && imp . imports [ 0 ] ?. name === 'TypeOrmModule' ) ;
82+ expect ( typeOrmModuleImport ) . toBeDefined ( ) ;
83+ } ) ;
84+
85+ it ( 'should export CacheWarmingService' , ( ) => {
86+ const moduleMetadata = Reflect . getMetadata ( 'exports' , CacheWarmingModule ) ;
87+ expect ( moduleMetadata ) . toContain ( CacheWarmingService ) ;
88+ } ) ;
89+ } ) ;
0 commit comments