-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59baaef
commit e1697fa
Showing
2 changed files
with
86 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UsersResolver } from './users.resolver'; | ||
import { UsersService } from './users.service'; | ||
import { User } from './models/user.model'; | ||
|
||
describe('UsersResolver', () => { | ||
let resolver: UsersResolver; | ||
let service: UsersService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UsersResolver, | ||
{ | ||
provide: UsersService, | ||
useValue: { | ||
findById: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
resolver = module.get<UsersResolver>(UsersResolver); | ||
service = module.get<UsersService>(UsersService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
|
||
describe('getUser', () => { | ||
it('should return a user by id', () => { | ||
const user: User = { id: 1, name: 'John Doe' }; | ||
jest.spyOn(service, 'findById').mockReturnValue(user); | ||
|
||
expect(resolver.getUser(1)).toEqual(user); | ||
expect(service.findById).toHaveBeenCalledWith(1); | ||
}); | ||
|
||
it('should return undefined if user not found', () => { | ||
jest.spyOn(service, 'findById').mockReturnValue(undefined); | ||
|
||
expect(resolver.getUser(1)).toBeUndefined(); | ||
expect(service.findById).toHaveBeenCalledWith(1); | ||
}); | ||
}); | ||
|
||
describe('resolveReference', () => { | ||
it('should return a user by reference id', () => { | ||
const user: User = { id: 1, name: 'John Doe' }; | ||
jest.spyOn(service, 'findById').mockReturnValue(user); | ||
|
||
expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toEqual(user); | ||
expect(service.findById).toHaveBeenCalledWith(1); | ||
}); | ||
|
||
it('should return undefined if user not found by reference id', () => { | ||
jest.spyOn(service, 'findById').mockReturnValue(undefined); | ||
|
||
expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toBeUndefined(); | ||
expect(service.findById).toHaveBeenCalledWith(1); | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql'; | ||
|
||
import { User } from './models/user.model'; | ||
import { UsersService } from './users.service'; | ||
|
||
@Resolver(() => User) | ||
export class UsersResolver { | ||
constructor(private usersService: UsersService) {} | ||
|
||
@Query() | ||
getUser(@Args({ name: 'id', type: () => ID }) id: number): User | undefined { | ||
return this.usersService.findById(id); | ||
} | ||
|
||
@ResolveReference() | ||
resolveReference(reference: { | ||
__typename: string; | ||
id: number; | ||
}): User | undefined { | ||
return this.usersService.findById(reference.id); | ||
} | ||
} |