|
1 |
| -import { screen } from "@testing-library/react" |
| 1 | +import { render, screen, waitFor } from "@testing-library/react" |
2 | 2 | import { rest } from "msw"
|
3 | 3 | import {
|
4 | 4 | MockEntitlementsWithAuditLog,
|
5 | 5 | MockMemberPermissions,
|
6 | 6 | MockUser,
|
7 |
| - render, |
8 | 7 | } from "testHelpers/renderHelpers"
|
9 | 8 | import { server } from "testHelpers/server"
|
10 |
| -import { Navbar } from "./Navbar" |
| 9 | +import { App } from "app" |
11 | 10 |
|
| 11 | +/** |
| 12 | + * The LicenseBanner, mounted above the AppRouter, fetches entitlements. Thus, to test their |
| 13 | + * effects, we must test at the App level and `waitFor` the fetch to be done. |
| 14 | + */ |
12 | 15 | describe("Navbar", () => {
|
13 | 16 | it("shows Audit Log link when permitted and entitled", async () => {
|
14 | 17 | server.use(
|
15 |
| - rest.get("/api/entitlements", (req, res, ctx) => { |
| 18 | + rest.get("/api/v2/entitlements", (req, res, ctx) => { |
16 | 19 | return res(ctx.status(200), ctx.json(MockEntitlementsWithAuditLog))
|
17 | 20 | }),
|
18 | 21 | )
|
19 |
| - render(<Navbar />) |
20 |
| - const link = await screen.findByText("Audit") |
21 |
| - expect(link).toBeDefined() |
| 22 | + render(<App />) |
| 23 | + await waitFor(() => { |
| 24 | + const link = screen.getByText("Audit") |
| 25 | + expect(link).toBeDefined() |
| 26 | + }) |
22 | 27 | })
|
23 | 28 |
|
24 |
| - it("does not show Audit Log link when not entitled", () => { |
25 |
| - render(<Navbar />) |
26 |
| - const link = screen.getByText("Audit") |
27 |
| - expect(link).not.toBeDefined() |
| 29 | + it("does not show Audit Log link when not entitled", async () => { |
| 30 | + render(<App />) |
| 31 | + await waitFor(() => { |
| 32 | + const link = screen.queryByText("Audit") |
| 33 | + expect(link).toBe(null) |
| 34 | + }) |
28 | 35 | })
|
29 | 36 |
|
30 |
| - it("does not show Audit Log link when not permitted via role", () => { |
| 37 | + it("does not show Audit Log link when not permitted via role", async () => { |
31 | 38 | server.use(
|
32 | 39 | rest.post(`/api/v2/users/${MockUser.id}/authorization`, async (req, res, ctx) => {
|
33 | 40 | return res(ctx.status(200), ctx.json(MockMemberPermissions))
|
34 | 41 | }),
|
35 | 42 | )
|
36 |
| - render(<Navbar />) |
37 |
| - const link = screen.getByText("Audit") |
38 |
| - expect(link).not.toBeDefined() |
| 43 | + server.use( |
| 44 | + rest.get("/api/v2/entitlements", (req, res, ctx) => { |
| 45 | + return res(ctx.status(200), ctx.json(MockEntitlementsWithAuditLog)) |
| 46 | + }), |
| 47 | + ) |
| 48 | + render(<App />) |
| 49 | + await waitFor(() => { |
| 50 | + const link = screen.queryByText("Audit") |
| 51 | + expect(link).toBe(null) |
| 52 | + }) |
39 | 53 | })
|
40 | 54 | })
|
0 commit comments