Created
October 19, 2019 04:41
-
-
Save TechnotronicOz/4e5d4ac70ef8321711d44372aff8168d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Test, TestingModule } from '@nestjs/testing'; | |
import { AuthService } from './auth.service'; | |
import { User } from '../users/user.entity'; | |
import { UsersService } from '../users/users.service'; | |
import { JwtModule, JwtService } from '@nestjs/jwt'; | |
import { jwtConfig } from './test/jwt-test-config'; | |
import { getTestUser } from '../users/test/users-test-helper'; | |
import { LoginPayload } from './auth.interface'; | |
import { UsersServiceMock } from '../users/mocks/users.service.mock'; | |
describe('AuthService', () => { | |
let service: AuthService; | |
let jwtService: JwtService; | |
let usersService: UsersService; | |
let user: User; | |
beforeEach(async () => { | |
// provie our own UsersService mock otherwise | |
// we have to import all of it's dependencies | |
const UsersServiceProvider = { | |
provide: UsersService, | |
useClass: UsersServiceMock, | |
}; | |
const module: TestingModule = await Test | |
.createTestingModule({ | |
imports: [JwtModule.register(jwtConfig)], | |
providers: [AuthService, UsersServiceProvider], | |
}) | |
.compile(); | |
user = getTestUser(); | |
service = module.get<AuthService>(AuthService); | |
jwtService = module.get<JwtService>(JwtService); | |
usersService = module.get<UsersService>(UsersService); | |
}); | |
it('should be defined', () => { | |
expect(service).toBeDefined(); | |
}); | |
describe('.validateUser', () => { | |
it('should validate user and return User', async () => { | |
jest | |
.spyOn(usersService, 'findOne') | |
.mockImplementation(() => Promise.resolve(user)); | |
expect(await service.validateUser('test', '1234')) | |
.toBe(user); | |
}); | |
it('should return null if user not found', async () => { | |
jest | |
.spyOn(usersService, 'findOne') | |
.mockImplementation(() => Promise.resolve(user)); | |
expect(await service.validateUser('test', '9999')) | |
.toBe(null); | |
}); | |
it('should handle an error from the usersService', async () => { | |
jest | |
.spyOn(usersService, 'findOne') | |
.mockImplementation(() => undefined); | |
expect(await service.validateUser('test', '1234')) | |
.toBe(null); | |
}); | |
}); | |
describe('.login', () => { | |
it('should a tokenPayload', async () => { | |
jest | |
.spyOn(jwtService, 'sign') | |
.mockImplementation(() => 'test'); | |
const userPayload: LoginPayload = { | |
id: 1, | |
username: 'test', | |
}; | |
const { access_token: accessToken } = await service.login(userPayload); | |
expect(accessToken).toBe('test'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment