forked from openedx/frontend-app-admin-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.tsx
More file actions
188 lines (161 loc) · 5.63 KB
/
index.test.tsx
File metadata and controls
188 lines (161 loc) · 5.63 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import AuditUserPage from './index';
jest.mock('@edx/frontend-platform/auth', () => ({
getAuthenticatedHttpClient: jest.fn(),
configure: jest.fn(),
}));
const mockUser = {
username: 'johndoe',
email: '[email protected]',
profile_image: { has_image: false },
};
const mockAssignments = {
count: 1,
results: [
{
id: '1',
role: 'library_admin',
org: 'Test Org',
scope: 'lib:test',
permissionCount: 5,
},
],
next: null,
previous: null,
};
const renderWithRouter = (route = '/audit/johndoe') => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
return render(
<QueryClientProvider client={queryClient}>
<IntlProvider locale="en">
<MemoryRouter initialEntries={[route]}>
<Routes>
<Route path="/audit/:username" element={<AuditUserPage />} />
<Route path="/authz" element={<div>Home Page</div>} />
</Routes>
</MemoryRouter>
</IntlProvider>
</QueryClientProvider>,
);
};
describe('AuditUserPage', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders user info and table when data is loaded', async () => {
(getAuthenticatedHttpClient as jest.Mock).mockReturnValue({
get: jest
.fn()
.mockResolvedValueOnce({ data: mockUser })
.mockResolvedValueOnce({ data: mockAssignments }),
});
renderWithRouter();
await waitFor(() => {
expect(screen.getByRole('heading', { name: 'johndoe' })).toBeInTheDocument();
expect(screen.getByText('[email protected]')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /assign role/i })).toBeInTheDocument();
expect(screen.getByText('Library Admin')).toBeInTheDocument();
expect(screen.getByText('Test Org')).toBeInTheDocument();
expect(screen.getByText('lib:test')).toBeInTheDocument();
expect(screen.getByText('5 permissions available')).toBeInTheDocument();
});
});
it('navigates to home if user is not found', async () => {
(getAuthenticatedHttpClient as jest.Mock).mockReturnValue({
get: jest
.fn()
.mockResolvedValueOnce({ data: null })
.mockResolvedValueOnce({ data: mockAssignments }),
});
renderWithRouter();
await waitFor(() => {
expect(screen.getByText('Home Page')).toBeInTheDocument();
});
});
it('allows user to interact with Assign Role button', async () => {
(getAuthenticatedHttpClient as jest.Mock).mockReturnValue({
get: jest
.fn()
.mockResolvedValueOnce({ data: mockUser })
.mockResolvedValueOnce({ data: mockAssignments }),
});
renderWithRouter();
await waitFor(() => {
expect(screen.getByRole('button', { name: /assign role/i })).toBeInTheDocument();
});
const user = userEvent.setup();
const button = screen.getByRole('button', { name: /assign role/i });
await user.click(button);
expect(button).not.toBeInTheDocument();
});
it('renders empty state when user has no assignments', async () => {
(getAuthenticatedHttpClient as jest.Mock).mockReturnValue({
get: jest
.fn()
.mockResolvedValueOnce({ data: mockUser })
.mockResolvedValueOnce({
data: {
count: 0, results: [], next: null, previous: null,
},
}),
});
renderWithRouter();
await waitFor(() => {
expect(screen.getByRole('heading', { name: 'johndoe' })).toBeInTheDocument();
expect(screen.queryByText('5 permissions available')).not.toBeInTheDocument();
expect(screen.getByRole('table')).toBeInTheDocument();
});
});
it('renders correct table headers', async () => {
(getAuthenticatedHttpClient as jest.Mock).mockReturnValue({
get: jest
.fn()
.mockResolvedValueOnce({ data: mockUser })
.mockResolvedValueOnce({ data: mockAssignments }),
});
renderWithRouter();
await waitFor(() => {
expect(screen.getByText('Role')).toBeInTheDocument();
expect(screen.getByText('Organization')).toBeInTheDocument();
expect(screen.getByText('Scope')).toBeInTheDocument();
expect(screen.getByText('Permissions')).toBeInTheDocument();
expect(screen.getByText('Actions')).toBeInTheDocument();
});
});
it('renders the pagination controls when assignments are present', async () => {
(getAuthenticatedHttpClient as jest.Mock).mockReturnValue({
get: jest
.fn()
.mockResolvedValueOnce({ data: mockUser })
.mockResolvedValueOnce({ data: mockAssignments }),
});
renderWithRouter();
await waitFor(() => {
expect(screen.getByText('Showing 1 of 1.')).toBeInTheDocument();
});
});
it('renders the breadcrumb navigation with home link', async () => {
(getAuthenticatedHttpClient as jest.Mock).mockReturnValue({
get: jest
.fn()
.mockResolvedValueOnce({ data: mockUser })
.mockResolvedValueOnce({ data: mockAssignments }),
});
renderWithRouter();
await waitFor(() => {
expect(screen.getByRole('link', { name: /roles and permissions management/i })).toBeInTheDocument();
expect(screen.getByText(mockUser.username, { selector: 'li[aria-current="page"]' })).toBeInTheDocument();
});
});
});