import React, { FC } from 'react';
import { render } from '@testing-library/react';
import 'regenerator-runtime';
import _fetchMock from 'isomorphic-unfetch';
const fetchMock = _fetchMock as any;
import { FetchProvider, useFetch } from '../FetchProvider';
const TestComponent: FC<{}> = () => {
try {
const { loading, error, data } = useFetch<{ value: string }>();
if (loading) {
return
Loading
;
}
if (error) {
return Error
;
}
return {data!.value}
;
} catch {
return Error
;
}
};
describe('', () => {
afterEach(() => {
fetchMock.restore();
fetchMock.reset();
});
const endpoint = 'http://localhost:123';
it('fetch', async () => {
const value = 'Fetched';
fetchMock.mock(endpoint, { status: 200, body: { value } });
const { asFragment, findByText } = render(
);
findByText('loading');
expect(asFragment()).toMatchSnapshot();
await findByText(value);
expect(asFragment()).toMatchSnapshot();
});
it('error', async () => {
fetchMock.mock(endpoint, { status: 500 });
const { asFragment, findByText } = render(
Children
);
await findByText('Error');
expect(asFragment()).toMatchSnapshot();
});
it('Unprovided', async () => {
const { asFragment, findByText } = render(
Children
);
await findByText('Error');
expect(asFragment()).toMatchSnapshot();
});
it('preview', async () => {
const value = 'Preloaded';
const { asFragment, findByText } = render(
);
await findByText(value);
expect(asFragment()).toMatchSnapshot();
});
});