| 
 | 1 | +import { GraphQLResolveInfo } from 'graphql';  | 
 | 2 | +import { MiddlewareContext, NextFn } from '../../lib/interfaces';  | 
 | 3 | + | 
 | 4 | +export const testMiddleware = async (ctx: MiddlewareContext, next: NextFn) => {  | 
 | 5 | +  let logData: MiddlewareContext = {  | 
 | 6 | +    source: ctx.source,  | 
 | 7 | +    args: ctx.args,  | 
 | 8 | +    context: ctx.context,  | 
 | 9 | +  };  | 
 | 10 | + | 
 | 11 | +  if (ctx.info) {  | 
 | 12 | +    logData = {  | 
 | 13 | +      ...logData,  | 
 | 14 | +      info: ctx.info,  | 
 | 15 | +    };  | 
 | 16 | +  }  | 
 | 17 | + | 
 | 18 | +  return next();  | 
 | 19 | +};  | 
 | 20 | + | 
 | 21 | +describe('testMiddleware', () => {  | 
 | 22 | +  let mockContext: MiddlewareContext;  | 
 | 23 | +  let mockNext: NextFn;  | 
 | 24 | + | 
 | 25 | +  beforeEach(() => {  | 
 | 26 | +    mockContext = {  | 
 | 27 | +      source: {},  | 
 | 28 | +      args: {},  | 
 | 29 | +      context: {},  | 
 | 30 | +      info: {  | 
 | 31 | +        path: { typename: 'TestType', key: 'testField' },  | 
 | 32 | +      } as GraphQLResolveInfo,  | 
 | 33 | +    };  | 
 | 34 | + | 
 | 35 | +    mockNext = jest.fn().mockResolvedValue('next result');  | 
 | 36 | +  });  | 
 | 37 | + | 
 | 38 | +  afterEach(() => {  | 
 | 39 | +    jest.clearAllMocks();  | 
 | 40 | +  });  | 
 | 41 | + | 
 | 42 | +  it('should call next when info is provided', () => {  | 
 | 43 | +    testMiddleware(mockContext, mockNext);  | 
 | 44 | +    expect(mockNext).toHaveBeenCalled();  | 
 | 45 | +  });  | 
 | 46 | + | 
 | 47 | +  it('should handle case when info is undefined', () => {  | 
 | 48 | +    mockContext.info = undefined;  | 
 | 49 | +    testMiddleware(mockContext, mockNext);  | 
 | 50 | +    expect(mockNext).toHaveBeenCalled();  | 
 | 51 | +  });  | 
 | 52 | + | 
 | 53 | +  it('should handle case when info is null', () => {  | 
 | 54 | +    mockContext.info = null;  | 
 | 55 | +    testMiddleware(mockContext, mockNext);  | 
 | 56 | +    expect(mockNext).toHaveBeenCalled();  | 
 | 57 | +  });  | 
 | 58 | +});  | 
0 commit comments