1+ import  {  CallHandler  }  from  "@nestjs/common" ; 
2+ import  {  ModuleRef  }  from  "@nestjs/core" ; 
3+ import  {  ExecutionContextHost  }  from  "@nestjs/core/helpers/execution-context-host" ; 
4+ import  {  Test  }  from  "@nestjs/testing" ; 
5+ import  {  DataLoaderInterceptor  }  from  "../index" ; 
6+ 
7+ describe ( DataLoaderInterceptor . name ,  ( )  =>  { 
8+   let  interceptor : DataLoaderInterceptor 
9+   let  handler : CallHandler 
10+ 
11+   beforeAll ( async  ( )  =>  { 
12+     const  testingModule  =  await  Test . createTestingModule ( { } ) . compile ( ) ; 
13+ 
14+     interceptor  =  new  DataLoaderInterceptor ( testingModule . get ( ModuleRef ) ) 
15+ 
16+     handler  =  { 
17+       handle : jest . fn ( ) 
18+     } 
19+   } ) 
20+ 
21+   afterEach ( ( )  =>  { 
22+     jest . clearAllMocks ( ) 
23+   } ) 
24+ 
25+   it ( 'should be defined' ,  ( )  =>  { 
26+     expect ( interceptor ) . toBeDefined ( ) 
27+   } ) 
28+ 
29+   describe ( 'when context is GraphQL' ,  ( )  =>  { 
30+     it ( 'should inject dataloader' ,  ( )  =>  { 
31+       const  gqlExecutionContext  =  { } 
32+       const  executionContextHost  =   new  ExecutionContextHost ( [ undefined ,  undefined ,  gqlExecutionContext ] ) 
33+       
34+       interceptor . intercept ( executionContextHost ,  handler )  
35+   
36+       expect ( handler . handle ) . toHaveBeenCalled ( ) 
37+       // expect(contextIdFactoryMock.create).toHaveBeenCalled() 
38+       expect ( gqlExecutionContext [ 'NEST_LOADER_CONTEXT_KEY' ] ) . not . toBeUndefined ( ) 
39+     } ) 
40+   } ) 
41+ 
42+   describe ( 'when context is not GraphQL' ,  ( )  =>  { 
43+     it ( 'should not inject dataloader' ,  ( )  =>  { 
44+       const  executionContextHost  =   new  ExecutionContextHost ( [ ] ) 
45+       
46+       interceptor . intercept ( executionContextHost ,  handler )  
47+   
48+       expect ( handler . handle ) . toHaveBeenCalled ( ) 
49+     } ) 
50+   } ) 
51+ } ) 
0 commit comments