soapbox/src/api/hooks/groups/useGroupLookup.test.ts

42 wiersze
1.3 KiB
TypeScript
Czysty Zwykły widok Historia

2023-05-08 17:29:11 +00:00
import { __stub } from 'soapbox/api';
import { buildGroup } from 'soapbox/jest/factory';
2023-07-06 16:14:34 +00:00
import { renderHook, rootState, waitFor } from 'soapbox/jest/test-helpers';
2023-05-08 17:29:11 +00:00
import { useGroupLookup } from './useGroupLookup';
2023-05-08 17:29:11 +00:00
const group = buildGroup({ id: '1', slug: 'soapbox' });
2023-07-06 16:14:34 +00:00
const state = rootState.setIn(['instance', 'version'], '3.4.1 (compatible; TruthSocial 1.0.0)');
2023-05-08 17:29:11 +00:00
describe('useGroupLookup hook', () => {
describe('with a successful request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet(`/api/v1/groups/lookup?name=${group.slug}`).reply(200, group);
});
});
it('is successful', async () => {
2023-07-06 16:14:34 +00:00
const { result } = renderHook(() => useGroupLookup(group.slug), undefined, state);
2023-05-08 17:29:11 +00:00
await waitFor(() => expect(result.current.isFetching).toBe(false));
expect(result.current.entity?.id).toBe(group.id);
});
});
describe('with an unsuccessful query', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet(`/api/v1/groups/lookup?name=${group.slug}`).networkError();
});
});
it('is has error state', async() => {
2023-07-06 16:14:34 +00:00
const { result } = renderHook(() => useGroupLookup(group.slug), undefined, state);
2023-05-08 17:29:11 +00:00
await waitFor(() => expect(result.current.isFetching).toBe(false));
expect(result.current.entity).toBeUndefined();
});
});
});