Skip to content

Commit

Permalink
test(j-s): Civil claimant exists guard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
unakb committed Nov 15, 2024
1 parent 2999722 commit 5c6680e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CivilClaimantExistsGuard implements CanActivate {
const civilClaimantId = request.params.civilClaimantId

if (!civilClaimantId) {
throw new BadRequestException('Missing civil id')
throw new BadRequestException('Missing civil claimant id')
}

const civilClaimant = theCase.civilClaimants?.find(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { uuid } from 'uuidv4'

import {
BadRequestException,
ExecutionContext,
NotFoundException,
} from '@nestjs/common'

import { CivilClaimantExistsGuard } from '../civilClaimantExists.guard'

interface Then {
result: boolean
error: Error
}

type GivenWhenThen = () => Promise<Then>

describe('Civil Claimant Exists Guard', () => {
const mockRequest = jest.fn()
let givenWhenThen: GivenWhenThen

beforeEach(async () => {
givenWhenThen = async (): Promise<Then> => {
const guard = new CivilClaimantExistsGuard()
const then = {} as Then

try {
then.result = guard.canActivate({
switchToHttp: () => ({ getRequest: mockRequest }),
} as unknown as ExecutionContext)
} catch (error) {
then.error = error as Error
}

return then
}
})

describe('civil claimant exists', () => {
const caseId = uuid()
const civilClaimantId = uuid()
const civilClaimant = { id: civilClaimantId, caseId }
const theCase = { id: caseId, civilClaimants: [civilClaimant] }
const request = {
params: { caseId, civilClaimantId },
case: theCase,
civilClaimant: undefined,
}
let then: Then

beforeEach(async () => {
mockRequest.mockReturnValueOnce(request)

then = await givenWhenThen()
})

it('should activate', () => {
expect(then.result).toBe(true)
expect(request.civilClaimant).toBe(civilClaimant)
})
})

describe('civil claimant does not exist', () => {
const caseId = uuid()
const civilClaimantId = uuid()
const theCase = { id: caseId, civilClaimants: [] }
let then: Then

beforeEach(async () => {
mockRequest.mockReturnValueOnce({
params: { caseId, civilClaimantId },
case: theCase,
})

then = await givenWhenThen()
})

it('should throw NotFoundException', () => {
expect(then.error).toBeInstanceOf(NotFoundException)
expect(then.error.message).toBe(
`Civil claimant ${civilClaimantId} of case ${caseId} does not exist`,
)
})
})

describe('missing case', () => {
let then: Then

beforeEach(async () => {
mockRequest.mockReturnValueOnce({ params: {} })

then = await givenWhenThen()
})

it('should throw BadRequestException', () => {
expect(then.error).toBeInstanceOf(BadRequestException)
expect(then.error.message).toBe('Missing case')
})
})

describe('missing civil claimant id', () => {
const caseId = uuid()
const theCase = { id: caseId, civilClaimants: [] }
let then: Then

beforeEach(async () => {
mockRequest.mockReturnValueOnce({ params: { caseId }, case: theCase })

then = await givenWhenThen()
})

it('should throw BadRequestException', () => {
expect(then.error).toBeInstanceOf(BadRequestException)
expect(then.error.message).toBe('Missing civil claimant id')
})
})
})

0 comments on commit 5c6680e

Please sign in to comment.