Skip to content

Commit

Permalink
add tests to introduced hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
aidynoJ committed Jun 13, 2024
1 parent db0c8b7 commit 9d07a1e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
71 changes: 71 additions & 0 deletions src/hooks/useUserSelfTenantPermissions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { renderHook, waitFor } from '@folio/jest-config-stripes/testing-library/react';
import {
QueryClient,
QueryClientProvider,
} from 'react-query';

import permissions from 'fixtures/permissions';
import useUserSelfTenantPermissions from './useUserSelfTenantPermissions';
import useOkapiKy from '../useOkapiKy';

jest.mock('../useOkapiKy');
jest.mock('../components', () => ({
useNamespace: () => ([]),
}));
jest.mock('../StripesContext', () => ({
useStripes: () => ({
user: {
user: {
id: 'userId'
}
},
hasInterface: () => true
}),
}));

const queryClient = new QueryClient();

// eslint-disable-next-line react/prop-types
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

const response = {
permissions: { permissions },
};

describe('useUserSelfTenantPermissions', () => {
const getMock = jest.fn(() => ({
json: () => Promise.resolve(response),
}));
const setHeaderMock = jest.fn();
const kyMock = {
extend: jest.fn(({ hooks: { beforeRequest } }) => {
beforeRequest.forEach(handler => handler({ headers: { set: setHeaderMock } }));

return {
get: getMock,
};
}),
};

beforeEach(() => {
getMock.mockClear();
useOkapiKy.mockClear().mockReturnValue(kyMock);
});

it('should fetch user permissions for specified tenant', async () => {
const options = {
userId: 'userId',
tenantId: 'tenantId',
};
const { result } = renderHook(() => useUserSelfTenantPermissions(options), { wrapper });

await waitFor(() => !result.current.isLoading);

expect(setHeaderMock).toHaveBeenCalledWith('X-Okapi-Tenant', options.tenantId);
expect(getMock).toHaveBeenCalledWith('users-keycloak/_self', expect.objectContaining({}));
});
});
5 changes: 3 additions & 2 deletions src/hooks/useUserTenantPermissionNames.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ jest.mock('../StripesContext', () => ({
user: {
id: 'userId'
}
}
},
hasInterface: () => false
}),
}));

Expand All @@ -36,7 +37,7 @@ const response = {
totalRecords: permissions.length,
};

describe('useUserTenantPermissions', () => {
describe('useUserTenantPermissionNames', () => {
const getMock = jest.fn(() => ({
json: () => Promise.resolve(response),
}));
Expand Down

0 comments on commit 9d07a1e

Please sign in to comment.