-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathRemodel.test.tsx
More file actions
132 lines (112 loc) · 3.61 KB
/
Remodel.test.tsx
File metadata and controls
132 lines (112 loc) · 3.61 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
import { BrowserRouter } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "react-query";
import { vi } from "vitest";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import Remodel from "../Remodel";
import { useRemodels, useModels, useSortTableData } from "../../../hooks";
import type { UseQueryResult } from "react-query";
import type {
ApiResponse,
RemodelResponse,
Model,
} from "../../../types/shared";
const mockFilterQuery = "test-model";
vi.mock("react-router-dom", async (importOriginal) => ({
...(await importOriginal()),
useSearchParams: () => [new URLSearchParams({ filter: mockFilterQuery })],
}));
vi.mock("../../Portals/Portals", async (importOriginal) => ({
...(await importOriginal()),
PortalEntrance: ({ children }: { children: React.ReactNode }) => (
<>{children}</>
),
}));
vi.mock("../../../hooks", () => ({
useRemodels: vi.fn(),
useModels: vi.fn(),
useSortTableData: vi.fn(),
}));
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchOnReconnect: false,
},
},
});
function renderComponent() {
return render(
<BrowserRouter>
<QueryClientProvider client={queryClient}>
<Remodel />
</QueryClientProvider>
</BrowserRouter>,
);
}
const useRemodelsNoPermissions = {
data: {
message: "There was a problem fetching remodels",
success: false,
},
isLoading: false,
isError: false,
} as UseQueryResult<ApiResponse<RemodelResponse>, Error>;
const useRemodelsPermissions = {
data: {
data: { allowlist: [], "next-cursor": null },
success: true,
},
isLoading: false,
isError: false,
} as unknown as UseQueryResult<ApiResponse<RemodelResponse>, Error>;
const mockUseModels = vi.mocked(useModels);
mockUseModels.mockReturnValue({
data: [],
} as unknown as UseQueryResult<Model[], Error>);
const mockUseSortTableData = vi.mocked(useSortTableData);
mockUseSortTableData.mockReturnValue({
rows: [],
updateSort: vi.fn(),
});
const mockUseRemodels = vi.mocked(useRemodels);
describe("Remodel", () => {
it("displays message if user has no remodels access", () => {
mockUseRemodels.mockReturnValue(useRemodelsNoPermissions);
renderComponent();
expect(
screen.getByText("There was a problem fetching remodels"),
).toBeInTheDocument();
});
it("doesn't display table if user has no remodels access", () => {
mockUseRemodels.mockReturnValue(useRemodelsNoPermissions);
renderComponent();
expect(screen.queryByTestId("remodel-table")).not.toBeInTheDocument();
});
it("doesn't display 'Configure' button if user has no remodels access", () => {
mockUseRemodels.mockReturnValue(useRemodelsNoPermissions);
renderComponent();
expect(
screen.queryByRole("link", { name: "Configure remodels" }),
).not.toBeInTheDocument();
});
it("doesn't display message if user has remodels access", () => {
mockUseRemodels.mockReturnValue(useRemodelsPermissions);
renderComponent();
expect(
screen.queryByText("There was a problem fetching remodels"),
).not.toBeInTheDocument();
});
it("displays table if user has remodels access", () => {
mockUseRemodels.mockReturnValue(useRemodelsPermissions);
renderComponent();
expect(screen.getByTestId("remodel-table")).toBeInTheDocument();
});
it("displays 'Configure' button if user has remodels access", () => {
mockUseRemodels.mockReturnValue(useRemodelsPermissions);
renderComponent();
expect(
screen.getByRole("link", { name: "Configure remodels" }),
).toBeInTheDocument();
});
});