1 | import withRole from "./withRole";
|
2 | import { Role } from "../constants";
|
3 |
|
4 | describe("withRole test", () => {
|
5 | it("should throw err", async () => {
|
6 | const mock = {
|
7 | state: {
|
8 | jwt: {
|
9 | roles: [],
|
10 | },
|
11 | },
|
12 | };
|
13 | const noop = () => {};
|
14 | let message = false;
|
15 |
|
16 | try {
|
17 | await withRole(Role.PET_STORE_BREEDER)(mock, noop);
|
18 | } catch (err) {
|
19 | message = err.message;
|
20 | }
|
21 | expect(message).toBe(`Require roles ${Role.PET_STORE_BREEDER}`);
|
22 | });
|
23 |
|
24 | it("should not throw err", async () => {
|
25 | const mock = {
|
26 | state: {
|
27 | jwt: {
|
28 | roles: [Role.PET_STORE_OWNER],
|
29 | },
|
30 | },
|
31 | };
|
32 | const noop = () => {};
|
33 | let message = false;
|
34 |
|
35 | try {
|
36 | await withRole(Role.PET_STORE_OWNER)(mock, noop);
|
37 | } catch (err) {
|
38 | message = err.message;
|
39 | }
|
40 | expect(message).toBeFalsy();
|
41 | });
|
42 |
|
43 | it("should respect ADMIN", async () => {
|
44 | const mock = {
|
45 | state: {
|
46 | jwt: {
|
47 | roles: ["ADMIN"],
|
48 | },
|
49 | },
|
50 | };
|
51 |
|
52 | const noop = () => {};
|
53 | await withRole(Role.PET_STORE_OWNER)(mock, noop);
|
54 | expect(mock.state.jwt.roles).toEqual(Object.values(Role));
|
55 | });
|
56 | });
|