|
| 1 | +import { screen, waitFor } from "@testing-library/react" |
| 2 | +import React from "react" |
| 3 | +import * as API from "../../api" |
| 4 | +import { renderWithAuth } from "../../testHelpers" |
| 5 | +import { checks } from "../../xServices/auth/authXService" |
| 6 | +import { Language as AdminDropdownLanguage } from "../AdminDropdown/AdminDropdown" |
| 7 | +import { Navbar } from "./Navbar" |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + jest.resetAllMocks() |
| 11 | +}) |
| 12 | + |
| 13 | +describe("Navbar", () => { |
| 14 | + describe("when user has permission to read all users", () => { |
| 15 | + it("displays the admin menu", async () => { |
| 16 | + const checkUserPermissionsSpy = jest.spyOn(API, "checkUserPermissions").mockResolvedValueOnce({ |
| 17 | + [checks.readAllUsers]: true, |
| 18 | + }) |
| 19 | + |
| 20 | + renderWithAuth(<Navbar />) |
| 21 | + |
| 22 | + // Wait for the request is done |
| 23 | + await waitFor(() => expect(checkUserPermissionsSpy).toBeCalledTimes(1)) |
| 24 | + await screen.findByRole("button", { name: AdminDropdownLanguage.menuTitle }) |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + describe("when user has NO permission to read all users", () => { |
| 29 | + it("does not display the admin menu", async () => { |
| 30 | + const checkUserPermissionsSpy = jest.spyOn(API, "checkUserPermissions").mockResolvedValueOnce({ |
| 31 | + [checks.readAllUsers]: false, |
| 32 | + }) |
| 33 | + renderWithAuth(<Navbar />) |
| 34 | + |
| 35 | + // Wait for the request is done |
| 36 | + await waitFor(() => expect(checkUserPermissionsSpy).toBeCalledTimes(1)) |
| 37 | + expect(screen.queryByRole("button", { name: AdminDropdownLanguage.menuTitle })).not.toBeInTheDocument() |
| 38 | + }) |
| 39 | + }) |
| 40 | +}) |
0 commit comments