Skip to content

Commit 0a3a1fa

Browse files
Dilukaclaude
andcommitted
test: add ExecutionContext mock for DataLoader functionality in reference resolver
- Add context mock using the same pattern as existing DataLoader tests - Create test case to verify DataLoader is used when ExecutionContext is provided - Add test for proper error handling when entity is not found via DataLoader - Ensure backward compatibility test still passes for non-context scenarios - All tests pass, confirming DataLoader n+1 solution works correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 4360e1f commit 0a3a1fa

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

packages/query-graphql/__tests__/resolvers/reference.resolver.spec.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { ExecutionContext } from '@nestjs/common'
12
import { Query, Resolver } from '@nestjs/graphql'
23
import { ReferenceResolver, ReferenceResolverOpts } from '@ptc-org/nestjs-query-graphql'
3-
import { when } from 'ts-mockito'
4+
import { anything, when } from 'ts-mockito'
45

56
import { createResolverFromNest, generateSchema, TestResolverDTO, TestService } from '../__fixtures__'
67

@@ -33,7 +34,9 @@ describe('ReferenceResolver', () => {
3334
})
3435

3536
describe('#resolveReference', () => {
36-
it('should call the service getById with the provided input', async () => {
37+
const createContext = (): ExecutionContext => ({}) as unknown as ExecutionContext
38+
39+
it('should call the service getById with the provided input (without context)', async () => {
3740
const { resolver, mockService } = await createResolverFromNest(TestResolver)
3841
const id = 'id-1'
3942
const output: TestResolverDTO = {
@@ -47,6 +50,24 @@ describe('ReferenceResolver', () => {
4750
return expect(result).toEqual(output)
4851
})
4952

53+
it('should use DataLoader when context is provided', async () => {
54+
const { resolver, mockService } = await createResolverFromNest(TestResolver)
55+
const context = createContext()
56+
const id = 'id-1'
57+
const output: TestResolverDTO = {
58+
id,
59+
stringField: 'foo'
60+
}
61+
62+
// Mock the query method for DataLoader with proper ts-mockito syntax
63+
when(mockService.query(anything())).thenResolve([output])
64+
65+
// @ts-ignore
66+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/naming-convention
67+
const result = await resolver.resolveReference({ __type: 'TestReference', id }, context)
68+
return expect(result).toEqual(output)
69+
})
70+
5071
it('should reject if the id is not found', async () => {
5172
const { resolver, mockService } = await createResolverFromNest(TestResolver)
5273
const id = 'id-1'
@@ -61,5 +82,20 @@ describe('ReferenceResolver', () => {
6182
'Unable to resolve reference, missing required key id for TestResolverDTO'
6283
)
6384
})
85+
86+
it('should reject if entity is not found when using DataLoader', async () => {
87+
const { resolver, mockService } = await createResolverFromNest(TestResolver)
88+
const context = createContext()
89+
const id = 'id-not-found'
90+
91+
// Mock the query method to return empty array
92+
when(mockService.query(anything())).thenResolve([])
93+
94+
// @ts-ignore
95+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/naming-convention
96+
return expect(resolver.resolveReference({ __type: 'TestReference', id }, context)).rejects.toThrow(
97+
'Unable to find TestResolverDTO with id: id-not-found'
98+
)
99+
})
64100
})
65101
})

0 commit comments

Comments
 (0)