-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters