Fix react-query tests

This commit is contained in:
Alex Gleason 2022-08-09 11:00:44 -05:00
parent 8eec8f3a09
commit 28d5461295
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 55 additions and 49 deletions

View file

@ -2,7 +2,9 @@ import userEvent from '@testing-library/user-event';
import { Map as ImmutableMap } from 'immutable'; import { Map as ImmutableMap } from 'immutable';
import React from 'react'; import React from 'react';
import { mock, render, screen, waitFor } from '../../../jest/test-helpers'; import { __stub } from 'soapbox/api';
import { render, screen, waitFor } from '../../../jest/test-helpers';
import FeedCarousel from '../feed-carousel'; import FeedCarousel from '../feed-carousel';
jest.mock('../../../hooks/useDimensions', () => ({ jest.mock('../../../hooks/useDimensions', () => ({
@ -56,6 +58,7 @@ describe('<FeedCarousel />', () => {
describe('with avatars', () => { describe('with avatars', () => {
beforeEach(() => { beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v1/truth/carousels/avatars') mock.onGet('/api/v1/truth/carousels/avatars')
.reply(200, [ .reply(200, [
{ account_id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' }, { account_id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' },
@ -64,6 +67,7 @@ describe('<FeedCarousel />', () => {
{ account_id: '4', acct: 'd', account_avatar: 'https://example.com/some.jpg' }, { account_id: '4', acct: 'd', account_avatar: 'https://example.com/some.jpg' },
]); ]);
}); });
});
it('should render the Carousel', async() => { it('should render the Carousel', async() => {
render(<FeedCarousel />, undefined, store); render(<FeedCarousel />, undefined, store);
@ -76,7 +80,7 @@ describe('<FeedCarousel />', () => {
describe('with 0 avatars', () => { describe('with 0 avatars', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet('/api/v1/truth/carousels/avatars').reply(200, []); __stub((mock) => mock.onGet('/api/v1/truth/carousels/avatars').reply(200, []));
}); });
it('renders nothing', async() => { it('renders nothing', async() => {
@ -90,7 +94,7 @@ describe('<FeedCarousel />', () => {
describe('with a failed request to the API', () => { describe('with a failed request to the API', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet('/api/v1/truth/carousels/avatars').networkError(); __stub((mock) => mock.onGet('/api/v1/truth/carousels/avatars').networkError());
}); });
it('renders the error message', async() => { it('renders the error message', async() => {
@ -104,6 +108,7 @@ describe('<FeedCarousel />', () => {
describe('with multiple pages of avatars', () => { describe('with multiple pages of avatars', () => {
beforeEach(() => { beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v1/truth/carousels/avatars') mock.onGet('/api/v1/truth/carousels/avatars')
.reply(200, [ .reply(200, [
{ account_id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' }, { account_id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' },
@ -111,6 +116,7 @@ describe('<FeedCarousel />', () => {
{ account_id: '3', acct: 'c', account_avatar: 'https://example.com/some.jpg' }, { account_id: '3', acct: 'c', account_avatar: 'https://example.com/some.jpg' },
{ account_id: '4', acct: 'd', account_avatar: 'https://example.com/some.jpg' }, { account_id: '4', acct: 'd', account_avatar: 'https://example.com/some.jpg' },
]); ]);
});
Element.prototype.getBoundingClientRect = jest.fn(() => { Element.prototype.getBoundingClientRect = jest.fn(() => {
return { return {

View file

@ -1,7 +1,7 @@
import { configureMockStore } from '@jedmao/redux-mock-store'; import { configureMockStore } from '@jedmao/redux-mock-store';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, RenderOptions } from '@testing-library/react'; import { render, RenderOptions } from '@testing-library/react';
import MockAdapter from 'axios-mock-adapter'; import { renderHook, RenderHookOptions } from '@testing-library/react-hooks';
import { merge } from 'immutable'; import { merge } from 'immutable';
import React, { FC, ReactElement } from 'react'; import React, { FC, ReactElement } from 'react';
import { IntlProvider } from 'react-intl'; import { IntlProvider } from 'react-intl';
@ -27,7 +27,7 @@ const applyActions = (state: any, actions: any, reducer: any) => {
return actions.reduce((state: any, action: any) => reducer(state, action), state); return actions.reduce((state: any, action: any) => reducer(state, action), state);
}; };
const mock = new MockAdapter(undefined as any, { onNoMatch: 'throwException' }); /** React Query client for tests. */
const queryClient = new QueryClient({ const queryClient = new QueryClient({
logger: { logger: {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
@ -42,10 +42,6 @@ const queryClient = new QueryClient({
}, },
}); });
beforeEach(() => {
mock.reset();
});
const createTestStore = (initialState: any) => createStore(rootReducer, initialState, applyMiddleware(thunk)); const createTestStore = (initialState: any) => createStore(rootReducer, initialState, applyMiddleware(thunk));
const TestApp: FC<any> = ({ children, storeProps, routerProps = {} }) => { const TestApp: FC<any> = ({ children, storeProps, routerProps = {} }) => {
let store: ReturnType<typeof createTestStore>; let store: ReturnType<typeof createTestStore>;
@ -88,11 +84,17 @@ const customRender = (
...options, ...options,
}); });
const queryWrapper: React.FC = ({ children }) => ( /** Like renderHook, but with access to the Redux store. */
<QueryClientProvider client={queryClient}> const customRenderHook = <T extends {}>(
{children} callback: (props?: any) => any,
</QueryClientProvider> options?: Omit<RenderHookOptions<T>, 'wrapper'>,
); store?: any,
) => {
return renderHook(callback, {
wrapper: ({ children }) => <TestApp children={children} storeProps={store} />,
...options,
});
};
const mockWindowProperty = (property: any, value: any) => { const mockWindowProperty = (property: any, value: any) => {
const { [property]: originalProperty } = window; const { [property]: originalProperty } = window;
@ -114,12 +116,11 @@ const mockWindowProperty = (property: any, value: any) => {
export * from '@testing-library/react'; export * from '@testing-library/react';
export { export {
customRender as render, customRender as render,
customRenderHook as renderHook,
mockStore, mockStore,
applyActions, applyActions,
rootState, rootState,
rootReducer, rootReducer,
mockWindowProperty, mockWindowProperty,
createTestStore, createTestStore,
mock,
queryWrapper,
}; };

View file

@ -1,12 +1,12 @@
import { renderHook } from '@testing-library/react-hooks'; import { __stub } from 'soapbox/api';
import { renderHook, waitFor } from 'soapbox/jest/test-helpers';
import { mock, queryWrapper, waitFor } from 'soapbox/jest/test-helpers';
import useCarouselAvatars from '../carousels'; import useCarouselAvatars from '../carousels';
describe('useCarouselAvatars', () => { describe('useCarouselAvatars', () => {
describe('with a successul query', () => { describe('with a successful query', () => {
beforeEach(() => { beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v1/truth/carousels/avatars') mock.onGet('/api/v1/truth/carousels/avatars')
.reply(200, [ .reply(200, [
{ account_id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' }, { account_id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' },
@ -15,11 +15,10 @@ describe('useCarouselAvatars', () => {
{ account_id: '4', acct: 'd', account_avatar: 'https://example.com/some.jpg' }, { account_id: '4', acct: 'd', account_avatar: 'https://example.com/some.jpg' },
]); ]);
}); });
});
it('is successful', async() => { it('is successful', async() => {
const { result } = renderHook(() => useCarouselAvatars(), { const { result } = renderHook(() => useCarouselAvatars());
wrapper: queryWrapper,
});
await waitFor(() => expect(result.current.isFetching).toBe(false)); await waitFor(() => expect(result.current.isFetching).toBe(false));
@ -27,15 +26,15 @@ describe('useCarouselAvatars', () => {
}); });
}); });
describe('with an unsuccessul query', () => { describe('with an unsuccessful query', () => {
beforeEach(() => { beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v1/truth/carousels/avatars').networkError(); mock.onGet('/api/v1/truth/carousels/avatars').networkError();
}); });
});
it('is successful', async() => { it('is successful', async() => {
const { result } = renderHook(() => useCarouselAvatars(), { const { result } = renderHook(() => useCarouselAvatars());
wrapper: queryWrapper,
});
await waitFor(() => expect(result.current.isFetching).toBe(false)); await waitFor(() => expect(result.current.isFetching).toBe(false));