-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(graphql): change info to nullable
- Loading branch information
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/graphql/tests/middleware/middleware-type-check.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { GraphQLResolveInfo } from 'graphql'; | ||
import { MiddlewareContext, NextFn } from '../../lib/interfaces'; | ||
|
||
export const testMiddleware = async (ctx: MiddlewareContext, next: NextFn) => { | ||
let logData: MiddlewareContext = { | ||
source: ctx.source, | ||
args: ctx.args, | ||
context: ctx.context, | ||
}; | ||
|
||
if (ctx.info) { | ||
logData = { | ||
...logData, | ||
info: ctx.info, | ||
}; | ||
} | ||
|
||
return next(); | ||
}; | ||
|
||
describe('testMiddleware', () => { | ||
let mockContext: MiddlewareContext; | ||
let mockNext: NextFn; | ||
|
||
beforeEach(async () => { | ||
mockContext = { | ||
source: {}, | ||
args: {}, | ||
context: {}, | ||
info: { | ||
path: { typename: 'TestType', key: 'testField' }, | ||
} as GraphQLResolveInfo, | ||
}; | ||
|
||
mockNext = jest.fn().mockResolvedValue('next result'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should log the context and call next function', async () => { | ||
const result = await testMiddleware(mockContext, mockNext); | ||
|
||
expect(mockNext).toHaveBeenCalled(); | ||
expect(result).toBe('next result'); | ||
}); | ||
|
||
it('should handle undefined info object', async () => { | ||
mockContext.info = undefined; | ||
|
||
await testMiddleware(mockContext, mockNext); | ||
|
||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle null info object', async () => { | ||
mockContext.info = null; | ||
|
||
await testMiddleware(mockContext, mockNext); | ||
|
||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
}); |