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

Commit

Permalink
unit(validator): make validator to check if role is the only role
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 16, 2022
1 parent 1c0def0 commit f3e5e4b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions server/validators/manager/is_the_only_role_to_any_user.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Request } from "!/types/dependent"

import "~/set-ups/database.set_up"
import Factory from "~/factories/role"
import Manager from "%/managers/role"
import UserFactory from "~/factories/user"
import makeInitialState from "!/validators/make_initial_state"
import IsTheOnlyRoleToAnyUser from "./is_the_only_role_to_any_user"

describe("Validator: Is the only role to any user", () => {
it("can accept valid input", async() => {
const model = await new Factory().insertOne()
await new UserFactory().attach(model).insertOne()
const value = Promise.resolve(makeInitialState(model.id))
const constraints = {
"field": "hello",
"manager": {
"className": Manager,
"columnName": "id"
},
"request": {} as Request,
"source": null
}

const sanitizeValue = (await IsTheOnlyRoleToAnyUser(value, constraints)).value

expect(sanitizeValue).toEqual(model.id)
})

it("cannot accept invalid value", async() => {
const model = await new Factory().insertOne()
const otherModel = await new Factory().insertOne()
await new UserFactory().attach(model).attach(otherModel).insertOne()
const value = Promise.resolve(makeInitialState(model.id))
const constraints = {
"field": "hello",
"manager": {
"className": Manager,
"columnName": "id"
},
"request": {} as Request,
"source": null
}

const error = IsTheOnlyRoleToAnyUser(value, constraints)

expect(error).rejects.toHaveProperty("field", "hello")
expect(error).rejects.toHaveProperty("messageMaker")
})
})

0 comments on commit f3e5e4b

Please sign in to comment.