Skip to content

Commit f39331c

Browse files
committed
test: fix issues with failing tests
1 parent ccc61bc commit f39331c

4 files changed

Lines changed: 60 additions & 26 deletions

File tree

src/authz-module/data/hooks.test.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ describe('useTeamMembers', () => {
5959
get: jest.fn().mockResolvedValue({ data: { results: mockMembers } }),
6060
});
6161

62-
const { result } = renderHook(() => useTeamMembers('lib:123'), {
62+
const mockQuerySettings = {
63+
roles: null,
64+
search: null,
65+
ordering: null,
66+
pageSize: 10,
67+
pageIndex: 0,
68+
};
69+
70+
const { result } = renderHook(() => useTeamMembers('lib:123', mockQuerySettings), {
6371
wrapper: createWrapper(),
6472
});
6573

@@ -74,7 +82,15 @@ describe('useTeamMembers', () => {
7482
get: jest.fn().mockRejectedValue(new Error('API failure')),
7583
});
7684

77-
const { result } = renderHook(() => useTeamMembers('lib:123'), {
85+
const mockQuerySettings = {
86+
roles: null,
87+
search: null,
88+
ordering: null,
89+
pageSize: 10,
90+
pageIndex: 0,
91+
};
92+
93+
const { result } = renderHook(() => useTeamMembers('lib:123', mockQuerySettings), {
7894
wrapper: createWrapper(),
7995
});
8096

src/authz-module/data/hooks.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import {
77

88
const authzQueryKeys = {
99
all: [appId, 'authz'] as const,
10-
teamMembers: (object: string, querySettings: QuerySettings) => [
10+
teamMembers: (object: string, querySettings?: QuerySettings) => [
1111
...authzQueryKeys.all,
1212
'teamMembers',
1313
object,
14-
querySettings.roles,
15-
querySettings.search,
16-
querySettings.ordering,
17-
querySettings.pageSize,
18-
querySettings.pageIndex,
14+
querySettings?.roles ?? null,
15+
querySettings?.search ?? null,
16+
querySettings?.ordering ?? null,
17+
querySettings?.pageSize ?? 10,
18+
querySettings?.pageIndex ?? 0,
1919
] as const,
2020
permissionsByRole: (scope: string) => [...authzQueryKeys.all, 'permissionsByRole', scope] as const,
2121
library: (libraryId: string) => [...authzQueryKeys.all, 'library', libraryId] as const,

src/authz-module/libraries-manager/components/TeamTable.test.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ describe('TeamTable', () => {
3838
libraryId: 'lib:123',
3939
canManageTeam: true,
4040
username: 'alice',
41+
roles: [
42+
{ name: 'Admin', role: 'admin', userCount: 1 },
43+
{ name: 'Editor', role: 'editor', userCount: 1 },
44+
{ name: 'Viewer', role: 'viewer', userCount: 1 },
45+
],
4146
};
4247

4348
beforeEach(() => {
@@ -90,9 +95,7 @@ describe('TeamTable', () => {
9095
expect(editButtons).toHaveLength(1);
9196

9297
await userEvent.click(editButtons[0]);
93-
expect(mockNavigate).toHaveBeenCalledWith(
94-
`/authz/${ROUTES.LIBRARIES_USER_PATH.replace(':username', 'bob')}`,
95-
);
98+
expect(mockNavigate).toHaveBeenCalledWith(`/authz/${ROUTES.LIBRARIES_USER_PATH.replace(':username', 'bob')}`);
9699
});
97100

98101
it('does not render Edit button if canManageTeam is false', () => {

src/authz-module/libraries-manager/context.test.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { screen } from '@testing-library/react';
1+
import { Component, ReactNode } from 'react';
2+
import { screen, renderHook } from '@testing-library/react';
23
import { useParams } from 'react-router-dom';
34
import { useValidateUserPermissions } from '@src/data/hooks';
45
import { renderWrapper } from '@src/setupTest';
@@ -17,16 +18,31 @@ jest.mock('@src/authz-module/data/hooks', () => ({
1718
data: [
1819
{
1920
role: 'library_author',
20-
permissions: [
21-
'view_library_team',
22-
'edit_library',
23-
],
21+
permissions: ['view_library_team', 'edit_library'],
2422
user_count: 12,
2523
},
2624
],
2725
}),
2826
}));
2927

28+
class ErrorBoundary extends Component<{ children: ReactNode }, { hasError: boolean; error?: Error }> {
29+
constructor(props: { children: ReactNode }) {
30+
super(props);
31+
this.state = { hasError: false };
32+
}
33+
34+
static getDerivedStateFromError(error: Error) {
35+
return { hasError: true, error };
36+
}
37+
38+
render() {
39+
if (this.state.hasError && this.state.error) {
40+
throw this.state.error;
41+
}
42+
return this.props.children;
43+
}
44+
}
45+
3046
const TestComponent = () => {
3147
const context = useLibraryAuthZ();
3248
return (
@@ -35,7 +51,9 @@ const TestComponent = () => {
3551
<div data-testid="libraryId">{context.libraryId}</div>
3652
<div data-testid="canManageTeam">{context.canManageTeam ? 'true' : 'false'}</div>
3753
<div data-testid="roles">{Array.isArray(context.roles) ? context.roles.length : 'undefined'}</div>
38-
<div data-testid="permissions">{Array.isArray(context.permissions) ? context.permissions.length : 'undefined'}</div>
54+
<div data-testid="permissions">
55+
{Array.isArray(context.permissions) ? context.permissions.length : 'undefined'}
56+
</div>
3957
<div data-testid="resources">{Array.isArray(context.resources) ? context.resources.length : 'undefined'}</div>
4058
</div>
4159
);
@@ -108,21 +126,18 @@ describe('LibraryAuthZProvider', () => {
108126

109127
expect(() => {
110128
renderWrapper(
111-
<LibraryAuthZProvider>
112-
<TestComponent />
113-
</LibraryAuthZProvider>,
129+
<ErrorBoundary>
130+
<LibraryAuthZProvider>
131+
<TestComponent />
132+
</LibraryAuthZProvider>
133+
</ErrorBoundary>,
114134
);
115135
}).toThrow('MissingLibrary');
116136
});
117137

118138
it('throws error when useLibraryAuthZ is used outside provider', () => {
119-
const BrokenComponent = () => {
120-
useLibraryAuthZ();
121-
return null;
122-
};
123-
124139
expect(() => {
125-
renderWrapper(<BrokenComponent />);
140+
renderHook(() => useLibraryAuthZ());
126141
}).toThrow('useLibraryAuthZ must be used within an LibraryAuthZProvider');
127142
});
128143
});

0 commit comments

Comments
 (0)