Skip to content

Commit

Permalink
update test cases for user controller
Browse files Browse the repository at this point in the history
  • Loading branch information
KenLSM committed Jun 4, 2024
1 parent 5bd7089 commit d6a822d
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/app/modules/user/__tests__/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import {
MissingUserError,
} from 'src/app/modules/user/user.errors'
import * as UserService from 'src/app/modules/user/user.service'
import { SmsSendError } from 'src/app/services/sms/sms.errors'
import { SmsFactory } from 'src/app/services/sms/sms.factory'
import { SmsSendError } from 'src/app/services/postman-sms/postman-sms.errors'
import PostmanSmsService from 'src/app/services/postman-sms/postman-sms.service'
import { HashingError } from 'src/app/utils/hash'
import { IPopulatedUser } from 'src/types'

import { DatabaseError } from '../../core/core.errors'
import { UNAUTHORIZED_USER_MESSAGE } from '../user.constant'

jest.mock('src/app/modules/user/user.service')
jest.mock('src/app/services/sms/sms.factory')
jest.mock('src/app/services/postman-sms/postman-sms.service')
const MockUserService = jest.mocked(UserService)
const MockSmsFactory = jest.mocked(SmsFactory)
const MockPostmanSmsService = jest.mocked(PostmanSmsService)

describe('user.controller', () => {
afterEach(() => {
Expand Down Expand Up @@ -48,7 +48,9 @@ describe('user.controller', () => {

// Mock UserService and SmsFactory to pass without errors.
MockUserService.createContactOtp.mockReturnValueOnce(okAsync(expectedOtp))
MockSmsFactory.sendAdminContactOtp.mockReturnValueOnce(okAsync(true))
MockPostmanSmsService.sendAdminContactOtp.mockReturnValueOnce(
okAsync(true),
)

// Act
await UserController._handleContactSendOtp(MOCK_REQ, mockRes, jest.fn())
Expand All @@ -59,7 +61,7 @@ describe('user.controller', () => {
MOCK_REQ.body.userId,
MOCK_REQ.body.contact,
)
expect(MockSmsFactory.sendAdminContactOtp).toHaveBeenCalledWith(
expect(MockPostmanSmsService.sendAdminContactOtp).toHaveBeenCalledWith(
MOCK_REQ.body.contact,
expectedOtp,
MOCK_REQ.body.userId,
Expand Down Expand Up @@ -133,7 +135,7 @@ describe('user.controller', () => {
// Mock UserService to pass without errors.
MockUserService.createContactOtp.mockReturnValueOnce(okAsync('123456'))
// Mock SmsFactory to return error.
MockSmsFactory.sendAdminContactOtp.mockReturnValueOnce(
MockPostmanSmsService.sendAdminContactOtp.mockReturnValueOnce(
errAsync(new SmsSendError(mockErrorString)),
)

Expand Down

0 comments on commit d6a822d

Please sign in to comment.