Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
unit(route): ensure restore patch for chat message works
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo Magtoto committed Oct 10, 2022
1 parent faf695b commit 426fbe3
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions routes/api/chat_message/restore.patch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import ErrorBag from "$!/errors/error_bag"
import MockRequester from "~/setups/mock_requester"
import ChatMessageFactory from "~/factories/chat_message"

import Controller from "./restore.patch"

const BODY_VALIDATION_INDEX = 0

describe("Controller: PATCH /api/chat_message", () => {
const requester = new MockRequester()

it("can accept valid info", async() => {
const controller = new Controller()
const { validations } = controller
const bodyValidation = validations[BODY_VALIDATION_INDEX]
const bodyValidationFunction = bodyValidation.intermediate.bind(bodyValidation)
const chatMessage = await new ChatMessageFactory().insertOne()
await chatMessage.destroy({ "force": false })
requester.customizeRequest({
"body": {
"data": [
{
"type": "chat_message",
"id": String(chatMessage.id)
}
]
}
})

await requester.runMiddleware(bodyValidationFunction)

requester.expectSuccess()
})

it("cannot accept existing resources", async() => {
const controller = new Controller()
const { validations } = controller
const bodyValidation = validations[BODY_VALIDATION_INDEX]
const bodyValidationFunction = bodyValidation.intermediate.bind(bodyValidation)
const chatMessage = await new ChatMessageFactory().insertOne()
requester.customizeRequest({
"body": {
"data": [
{
"type": "chat_message",
"id": String(chatMessage.id)
}
]
}
})

await requester.runMiddleware(bodyValidationFunction)

const body = requester.expectFailure(ErrorBag).toJSON()
expect(body).toHaveLength(1)
expect(body).toHaveProperty("0.source.pointer", "data.0.id")
})

it("cannot accept non-existent resources", async() => {
const controller = new Controller()
const { validations } = controller
const bodyValidation = validations[BODY_VALIDATION_INDEX]
const bodyValidationFunction = bodyValidation.intermediate.bind(bodyValidation)
const chatMessage = await new ChatMessageFactory().insertOne()
await chatMessage.destroy({ "force": true })
requester.customizeRequest({
"body": {
"data": [
{
"type": "chat_message",
"id": String(chatMessage.id)
}
]
}
})

await requester.runMiddleware(bodyValidationFunction)

const body = requester.expectFailure(ErrorBag).toJSON()
expect(body).toHaveLength(1)
expect(body).toHaveProperty("0.source.pointer", "data.0.id")
})
})

0 comments on commit 426fbe3

Please sign in to comment.