Skip to content

Commit d549187

Browse files
bra-i-amdcoa
authored andcommitted
test: fix issues with failing tests
1 parent 820052d commit d549187

4 files changed

Lines changed: 59 additions & 23 deletions

File tree

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

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

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

@@ -76,7 +84,15 @@ describe('useTeamMembers', () => {
7684
get: jest.fn().mockRejectedValue(new Error('API failure')),
7785
});
7886

79-
const { result } = renderHook(() => useTeamMembers('lib:123'), {
87+
const mockQuerySettings = {
88+
roles: null,
89+
search: null,
90+
ordering: null,
91+
pageSize: 10,
92+
pageIndex: 0,
93+
};
94+
95+
const { result } = renderHook(() => useTeamMembers('lib:123', mockQuerySettings), {
8096
wrapper: createWrapper(),
8197
});
8298

src/authz-module/data/hooks.ts

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

1212
const authzQueryKeys = {
1313
all: [appId, 'authz'] as const,
14-
teamMembers: (object: string, querySettings: QuerySettings) => [
14+
teamMembers: (object: string, querySettings?: QuerySettings) => [
1515
...authzQueryKeys.all,
1616
'teamMembers',
1717
object,
18-
querySettings.roles,
19-
querySettings.search,
20-
querySettings.ordering,
21-
querySettings.pageSize,
22-
querySettings.pageIndex,
18+
querySettings?.roles ?? null,
19+
querySettings?.search ?? null,
20+
querySettings?.ordering ?? null,
21+
querySettings?.pageSize ?? 10,
22+
querySettings?.pageIndex ?? 0,
2323
] as const,
2424
permissionsByRole: (scope: string) => [...authzQueryKeys.all, 'permissionsByRole', scope] as const,
2525
library: (libraryId: string) => [...authzQueryKeys.all, 'library', libraryId] as const,

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

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

4247
beforeEach(() => {

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';
@@ -18,16 +19,31 @@ jest.mock('@src/authz-module/data/hooks', () => ({
1819
data: [
1920
{
2021
role: 'library_author',
21-
permissions: [
22-
'view_library_team',
23-
'edit_library',
24-
],
22+
permissions: ['view_library_team', 'edit_library'],
2523
user_count: 12,
2624
},
2725
],
2826
}),
2927
}));
3028

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

138156
expect(() => {
139157
renderWrapper(
140-
<LibraryAuthZProvider>
141-
<TestComponent />
142-
</LibraryAuthZProvider>,
158+
<ErrorBoundary>
159+
<LibraryAuthZProvider>
160+
<TestComponent />
161+
</LibraryAuthZProvider>
162+
</ErrorBoundary>,
143163
);
144164
}).toThrow('MissingLibrary');
145165
});
146166

147167
it('throws error when useLibraryAuthZ is used outside provider', () => {
148-
const BrokenComponent = () => {
149-
useLibraryAuthZ();
150-
return null;
151-
};
152-
153168
expect(() => {
154-
renderWrapper(<BrokenComponent />);
169+
renderHook(() => useLibraryAuthZ());
155170
}).toThrow('useLibraryAuthZ must be used within an LibraryAuthZProvider');
156171
});
157172
});

0 commit comments

Comments
 (0)