This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
generated from KennethTrecy/web_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit(route): ensure restore patch for chat message works
- Loading branch information
Angelo Magtoto
committed
Oct 10, 2022
1 parent
faf695b
commit 426fbe3
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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") | ||
}) | ||
}) |