|
| 1 | +import { render, screen, waitFor } from "@testing-library/react" |
| 2 | +import { App } from "app" |
| 3 | +import { Language } from "components/NavbarView/NavbarView" |
| 4 | +import { rest } from "msw" |
| 5 | +import { |
| 6 | + MockEntitlementsWithAuditLog, |
| 7 | + MockMemberPermissions, |
| 8 | + MockUser, |
| 9 | +} from "testHelpers/renderHelpers" |
| 10 | +import { server } from "testHelpers/server" |
| 11 | + |
| 12 | +/** |
| 13 | + * The LicenseBanner, mounted above the AppRouter, fetches entitlements. Thus, to test their |
| 14 | + * effects, we must test at the App level and `waitFor` the fetch to be done. |
| 15 | + */ |
| 16 | +describe("Navbar", () => { |
| 17 | + it("shows Audit Log link when permitted and entitled", async () => { |
| 18 | + // set entitlements to allow audit log |
| 19 | + server.use( |
| 20 | + rest.get("/api/v2/entitlements", (req, res, ctx) => { |
| 21 | + return res(ctx.status(200), ctx.json(MockEntitlementsWithAuditLog)) |
| 22 | + }), |
| 23 | + ) |
| 24 | + render(<App />) |
| 25 | + await waitFor( |
| 26 | + () => { |
| 27 | + const link = screen.getByText(Language.audit) |
| 28 | + expect(link).toBeDefined() |
| 29 | + }, |
| 30 | + { timeout: 2000 }, |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + it("does not show Audit Log link when not entitled", async () => { |
| 35 | + // by default, user is an Admin with permission to see the audit log, |
| 36 | + // but is unlicensed so not entitled to see the audit log |
| 37 | + render(<App />) |
| 38 | + await waitFor( |
| 39 | + () => { |
| 40 | + const link = screen.queryByText(Language.audit) |
| 41 | + expect(link).toBe(null) |
| 42 | + }, |
| 43 | + { timeout: 2000 }, |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + it("does not show Audit Log link when not permitted via role", async () => { |
| 48 | + // set permissions to Member (can't audit) |
| 49 | + server.use( |
| 50 | + rest.post(`/api/v2/users/${MockUser.id}/authorization`, async (req, res, ctx) => { |
| 51 | + return res(ctx.status(200), ctx.json(MockMemberPermissions)) |
| 52 | + }), |
| 53 | + ) |
| 54 | + // set entitlements to allow audit log |
| 55 | + server.use( |
| 56 | + rest.get("/api/v2/entitlements", (req, res, ctx) => { |
| 57 | + return res(ctx.status(200), ctx.json(MockEntitlementsWithAuditLog)) |
| 58 | + }), |
| 59 | + ) |
| 60 | + render(<App />) |
| 61 | + await waitFor( |
| 62 | + () => { |
| 63 | + const link = screen.queryByText(Language.audit) |
| 64 | + expect(link).toBe(null) |
| 65 | + }, |
| 66 | + { timeout: 2000 }, |
| 67 | + ) |
| 68 | + }) |
| 69 | +}) |
0 commit comments