Skip to content

Commit

Permalink
fix: fix the unit test and make sure it checks
Browse files Browse the repository at this point in the history
  • Loading branch information
zulfikarrosadi committed Sep 23, 2024
1 parent 70708ab commit ce27596
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
50 changes: 49 additions & 1 deletion src/auth/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hashSync } from 'bcrypt'
import { AuthCredentialError } from '../lib/Error'
import { AuthCredentialError, EmailAlreadyExistsError } from '../lib/Error'
import { createNewToken, refreshTokenMaxAge, verifyToken } from '../lib/token'
import type AuthRepository from './repository'
import AuthService from './service'
Expand All @@ -15,6 +15,8 @@ describe('auth service', () => {

beforeEach(() => {
authRepo = {
createUser: jest.fn(),
getUserById: jest.fn(),
getUserByEmail: jest.fn(),
saveTokenToDb: jest.fn(),
getTokenByUserId: jest.fn(),
Expand All @@ -23,6 +25,52 @@ describe('auth service', () => {
authService = new AuthService(authRepo)
})

describe('register user', () => {
it('should register new user', async () => {
authRepo.createUser.mockResolvedValue({ userId: 1 })
authRepo.saveTokenToDb.mockResolvedValue({ affectedRows: 1 })
authRepo.getUserById.mockResolvedValue({
id: 1,
fullname: FULLNAME,
email: VALID_EMAIL,
})
const newUser = await authService.registerUser({
fullname: FULLNAME,
email: VALID_EMAIL,
password: 'password',
})

expect(authRepo.createUser).toHaveBeenCalled()
expect(newUser.response.status).toBe('success')
expect(newUser).toHaveProperty('token')
expect(newUser.response).toEqual({
status: 'success',
data: {
user: { id: 1, email: VALID_EMAIL, fullname: FULLNAME },
},
})
expect(newUser.token?.accessToken).not.toBeNull()
expect(newUser.token?.refreshToken).not.toBeNull()
})

it('should fail: email already exists', async () => {
authRepo.createUser.mockRejectedValue(new EmailAlreadyExistsError())
const newUser = await authService.registerUser({
email: 'already_exist_email',
password: VALID_PASSWORD,
fullname: FULLNAME,
})

expect(authRepo.createUser).toHaveBeenCalled()
expect(newUser.response.status).toBe('fail')
if (newUser.response.status === 'fail') {
expect(newUser.response.errors.message).toBe(
'this email already exists',
)
}
})
})

describe('login', () => {
it('should fail caused none existent user', async () => {
authRepo.getUserByEmail.mockRejectedValue(new AuthCredentialError())
Expand Down
49 changes: 0 additions & 49 deletions src/user/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,22 @@
import { EmailAlreadyExistsError } from '../lib/Error'
import type UserRepository from './repository'
import UserSerivce from './service'

describe('user service', () => {
let userRepo: jest.Mocked<UserRepository>
let userService: UserSerivce
const VALID_EMAIL = 'testing@email.com'
const VALID_PASSWORD = 'password'
const FULLNAME = 'testing'

beforeEach(() => {
userRepo = {
USER_ALREADY_EXISTS: 1062,
createUser: jest.fn(),
getUserById: jest.fn(),
saveTokenToDb: jest.fn(),
} as unknown as jest.Mocked<UserRepository>

userService = new UserSerivce(userRepo)
})

describe('register user', () => {
it('should register new user', async () => {
userRepo.createUser.mockResolvedValue({ userId: 1 })
userRepo.saveTokenToDb.mockResolvedValue({ affectedRows: 1 })
userRepo.getUserById.mockResolvedValue({
id: 1,
fullname: FULLNAME,
email: VALID_EMAIL,
})
const newUser = await userService.registerUser({
fullname: FULLNAME,
email: VALID_EMAIL,
password: 'password',
})

expect(userRepo.createUser).toHaveBeenCalled()
expect(newUser.response.status).toBe('success')
expect(newUser).toHaveProperty('token')
expect(newUser.response).toEqual({
status: 'success',
data: {
user: { id: 1, email: VALID_EMAIL, fullname: FULLNAME },
},
})
expect(newUser.token?.accessToken).not.toBeNull()
expect(newUser.token?.refreshToken).not.toBeNull()
})

it('should fail: email already exists', async () => {
userRepo.createUser.mockRejectedValue(new EmailAlreadyExistsError())
const newUser = await userService.registerUser({
email: 'already_exist_email',
password: VALID_PASSWORD,
fullname: FULLNAME,
})

expect(userRepo.createUser).toHaveBeenCalled()
expect(newUser.response.status).toBe('fail')
if (newUser.response.status === 'fail') {
expect(newUser.response.errors.message).toBe(
'this email already exists',
)
}
})
})

describe('get user by id', () => {
it('should fail: user not found cause bad user id', async () => {
const user = await userService.getUserById('bad_user_id')
Expand Down

0 comments on commit ce27596

Please # to comment.