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 comment create route works
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo Magtoto committed Oct 10, 2022
1 parent 5c36833 commit 42c837e
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions routes/api/comment/create.post.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import ErrorBag from "$!/errors/error_bag"
import CommentFactory from "~/factories/comment"
import MockRequester from "~/setups/mock_requester"

import Controller from "./create.post"

const BODY_VALIDATION_INDEX = 0

describe("Controller: POST /api/comment", () => {
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 comment = await new CommentFactory().makeOne()
requester.customizeRequest({
"body": {
"data": {
"attributes": {
"approvedAt": comment.approvedAt,
"content": comment.content
},
"relationships": {
"post": {
"data": {
"id": String(comment.postID),
"type": "post"
}
},
"user": {
"data": {
"id": String(comment.userID),
"type": "user"
}
}
},
"type": "comment"
}
}
})

await requester.runMiddleware(bodyValidationFunction)

requester.expectSuccess()
})

it("cannot accept invalid data", async() => {
const controller = new Controller()
const { validations } = controller
const bodyValidation = validations[BODY_VALIDATION_INDEX]
const bodyValidationFunction = bodyValidation.intermediate.bind(bodyValidation)
requester.customizeRequest({
"body": {
"data": {
"attributes": {
"approvedAt": 1,
"content": 1
},
"relationships": {
"post": {
"data": {
"id": 1,
"type": "post"
}
},
"user": {
"data": {
"id": 1,
"type": "user"
}
}
},
"type": "comment"
}
}
})

await requester.runMiddleware(bodyValidationFunction)

const body = requester.expectFailure(ErrorBag).toJSON()

expect(body).toHaveLength(4)
expect(body).toHaveProperty("0.source.pointer", "data.attributes.approvedAt")
expect(body).toHaveProperty("1.source.pointer", "data.attributes.content")
expect(body).toHaveProperty("2.source.pointer", "data.relationships.post.data.id")
expect(body).toHaveProperty("3.source.pointer", "data.relationships.user.data.id")
})
})

0 comments on commit 42c837e

Please sign in to comment.