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(validator): make validator to check if role is the only role
- Loading branch information
1 parent
1c0def0
commit f3e5e4b
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
server/validators/manager/is_the_only_role_to_any_user.spec.ts
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,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") | ||
}) | ||
}) |