-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathapp-sidebar-content.test.tsx
More file actions
170 lines (141 loc) · 5.03 KB
/
Copy pathapp-sidebar-content.test.tsx
File metadata and controls
170 lines (141 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { CLOUD_UPGRADE_FEATURE } from "@/types/cloud-upgrade";
import { AppSidebarContent } from "./app-sidebar-content";
import { useAppSidebarMode } from "./app-sidebar-mode-store";
import { APP_SIDEBAR_MODE } from "./types";
const {
openCloudUpgradeMock,
openLaunchScanModalMock,
pathnameValue,
pushMock,
} = vi.hoisted(() => ({
openCloudUpgradeMock: vi.fn(),
openLaunchScanModalMock: vi.fn(),
pathnameValue: { current: "/findings" },
pushMock: vi.fn(),
}));
vi.mock("next/navigation", () => ({
usePathname: () => pathnameValue.current,
useRouter: () => ({ push: pushMock }),
}));
vi.mock("@/hooks", () => ({
useAuth: () => ({ permissions: {} }),
}));
vi.mock("@/hooks/use-runtime-config", () => ({
useRuntimeConfig: () => ({ apiDocsUrl: "https://local.example/docs" }),
}));
vi.mock("@/components/registry/registry-eligibility-provider", () => ({
useRegistryEligibility: () => ({ isEligible: true }),
}));
vi.mock("@/store", () => ({
useScansStore: (
selector: (state: {
openLaunchScanModal: typeof openLaunchScanModalMock;
}) => unknown,
) => selector({ openLaunchScanModal: openLaunchScanModalMock }),
useCloudUpgradeStore: (
selector: (state: {
openCloudUpgrade: typeof openCloudUpgradeMock;
}) => unknown,
) => selector({ openCloudUpgrade: openCloudUpgradeMock }),
}));
vi.mock("@/app/(prowler)/lighthouse/_components/navigation", () => ({
LighthouseV2SidebarChat: () => <div data-testid="lighthouse-chat-sidebar" />,
}));
describe("AppSidebarContent", () => {
beforeEach(() => {
pathnameValue.current = "/findings";
pushMock.mockClear();
openCloudUpgradeMock.mockClear();
openLaunchScanModalMock.mockClear();
useAppSidebarMode.setState({ mode: APP_SIDEBAR_MODE.BROWSE });
});
afterEach(() => {
vi.unstubAllEnvs();
});
it("shares the brand, Launch Scan action and Local Server Cloud affordances", async () => {
// Given
vi.stubEnv("UI_CLOUD_ENABLED", "false");
vi.stubEnv("NEXT_PUBLIC_PROWLER_RELEASE_VERSION", "5.8.0");
const user = userEvent.setup();
// When
render(<AppSidebarContent />);
// Then
const homeLink = screen.getByRole("link", { name: "Prowler home" });
expect(homeLink).toBeVisible();
expect(screen.getByRole("link", { name: "Launch Scan" })).toHaveAttribute(
"href",
"/scans?launchScan=true",
);
expect(screen.getByText("5.8.0")).toBeVisible();
await user.click(screen.getByRole("button", { name: "Chat" }));
expect(openCloudUpgradeMock).toHaveBeenCalledWith(
CLOUD_UPGRADE_FEATURE.LIGHTHOUSE_AI,
undefined,
);
expect(screen.getAllByText("Cloud").length).toBeGreaterThan(0);
});
it("uses the eligibility lease for Registry navigation", () => {
// Given / When
vi.stubEnv("UI_CLOUD_ENABLED", "true");
render(<AppSidebarContent />);
// Then
expect(screen.getByRole("link", { name: /Registry/ })).toHaveAttribute(
"href",
"/registry",
);
});
it("keeps the existing Lighthouse chat sidebar in Cloud Chat mode", () => {
// Given
vi.stubEnv("UI_CLOUD_ENABLED", "true");
useAppSidebarMode.setState({ mode: APP_SIDEBAR_MODE.CHAT });
// When
render(<AppSidebarContent />);
// Then
expect(screen.getByTestId("lighthouse-chat-sidebar")).toBeVisible();
expect(
screen.getByRole("link", { name: "Service status" }),
).toHaveAttribute("href", "https://status.prowler.com");
expect(
screen.queryByText("All systems operational"),
).not.toBeInTheDocument();
});
it("preserves the current full-page Lighthouse session when Chat is selected again", async () => {
// Given
vi.stubEnv("UI_CLOUD_ENABLED", "true");
pathnameValue.current = "/lighthouse";
useAppSidebarMode.setState({ mode: APP_SIDEBAR_MODE.CHAT });
const user = userEvent.setup();
render(<AppSidebarContent />);
// When
await user.click(screen.getByRole("button", { name: "Chat" }));
// Then
expect(pushMock).not.toHaveBeenCalled();
});
it("navigates to Lighthouse when Chat is selected from another page", async () => {
// Given
vi.stubEnv("UI_CLOUD_ENABLED", "true");
const user = userEvent.setup();
render(<AppSidebarContent />);
// When
await user.click(screen.getByRole("button", { name: "Chat" }));
// Then
expect(pushMock).toHaveBeenCalledWith("/lighthouse");
});
it("opens the current scan modal instead of navigating from the scans route", async () => {
// Given
vi.stubEnv("UI_CLOUD_ENABLED", "true");
pathnameValue.current = "/scans";
const user = userEvent.setup();
// When
render(<AppSidebarContent />);
await user.click(screen.getByRole("button", { name: "Launch Scan" }));
// Then
expect(openLaunchScanModalMock).toHaveBeenCalledOnce();
expect(
screen.queryByRole("link", { name: "Launch Scan" }),
).not.toBeInTheDocument();
});
});