import * as React from 'react'; import { Provider } from 'react-redux'; import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import { ReduxDevTools } from './devtools.mocks'; import { BaseQueryJestMockFunction, setupStore } from './rtk-query.mocks'; function Providers({ store, children, }: { store: ReturnType['store']; children?: React.ComponentProps['children']; }) { const AnyProvider = Provider as any; return (
{children}
); } describe('rtk-query-monitor standalone integration', () => { // Hushes symbol.observable warning // @see https://github.com/reduxjs/redux-devtools/issues/1002 jest.spyOn(console, 'warn'); // eslint-disable-next-line @typescript-eslint/no-empty-function (console.warn as jest.Mock).mockImplementation(() => {}); const dataPanelDomId = '#rtk-query-monitor-tab-panel-0'; const childrenTextContent = 'Renders children'; const fetchBaseQueryMock: BaseQueryJestMockFunction> = jest.fn((...fetchArgs) => Promise.resolve({ data: { name: fetchArgs[0], }, }) ); const { store, pokemonApi } = setupStore(fetchBaseQueryMock, ReduxDevTools); beforeAll(() => { // let's populate api (store.dispatch as any)( pokemonApi.endpoints.getPokemonByName.initiate('bulbasaur') ); }); beforeEach(() => { fetchBaseQueryMock.mockClear(); }); afterAll(() => { (console.warn as jest.Mock).mockRestore(); }); it('renders on a standalone app without crashing', () => { const { container } = render(
{childrenTextContent}
); expect(screen.getByTestId('children').textContent).toBe( childrenTextContent ); expect( screen .getByRole('tab', { name: /actions/i }) ?.textContent?.toLowerCase() .trim() ).toBe('actions'); expect( screen .getByRole('tab', { name: /data/i }) ?.textContent?.toLowerCase() .trim() ).toBe('data'); expect( screen .getByRole('tab', { name: /api/i }) ?.textContent?.toLowerCase() .trim() ).toBe('api'); expect( container.querySelector( 'form[id="rtk-query-monitor-query-selection-form"]' ) ).toBeDefined(); }); it('displays query data tab content', async () => { // `Promise.resolve()` hushes `@typescript-eslint/await-thenable` await Promise.resolve(pokemonApi.util.getRunningOperationPromises()); const { container } = render(
{childrenTextContent}
); // We need to select the query & the correct tab fireEvent.click(screen.getByRole('tab', { name: /data/i })); fireEvent.click(screen.getByText(/bulbasaur/i)); await waitFor(() => expect(container.querySelector(dataPanelDomId)).not.toBeNull() ); expect(container.querySelector(dataPanelDomId)?.textContent).toMatch( /name\W+pokemon\/bulbasaur/i ); }); });