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

47 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';
import { renderHook, waitFor } from 'soapbox/jest/test-helpers';
2023-05-11 12:30:13 +00:00
import { normalizeInstance } from 'soapbox/normalizers';
2023-05-08 17:29:11 +00:00
import { useGroups } from './useGroups';
2023-05-08 17:29:11 +00:00
const group = buildGroup({ id: '1', display_name: 'soapbox' });
2023-05-11 12:30:13 +00:00
const store = {
instance: normalizeInstance({
version: '3.4.1 (compatible; TruthSocial 1.0.0+unreleased)',
}),
};
2023-05-08 17:29:11 +00:00
describe('useGroups hook', () => {
describe('with a successful request', () => {
beforeEach(() => {
__stub((mock) => {
2023-05-11 12:30:13 +00:00
mock.onGet('/api/v1/groups').reply(200, [group]);
2023-05-08 17:29:11 +00:00
});
});
it('is successful', async () => {
2023-05-11 12:30:13 +00:00
const { result } = renderHook(useGroups, undefined, store);
2023-05-08 17:29:11 +00:00
await waitFor(() => expect(result.current.isFetching).toBe(false));
2023-05-11 12:30:13 +00:00
expect(result.current.groups).toHaveLength(1);
2023-05-08 17:29:11 +00:00
});
});
2023-05-11 12:30:13 +00:00
describe('with an unsuccessful query', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v1/groups').networkError();
});
});
2023-05-08 17:29:11 +00:00
2023-05-11 12:30:13 +00:00
it('is has error state', async() => {
const { result } = renderHook(useGroups, undefined, store);
2023-05-08 17:29:11 +00:00
2023-05-11 12:30:13 +00:00
await waitFor(() => expect(result.current.isFetching).toBe(false));
2023-05-08 17:29:11 +00:00
2023-05-11 12:30:13 +00:00
expect(result.current.groups).toHaveLength(0);
});
});
2023-05-08 17:29:11 +00:00
});