Skip to content
Guide/Testing

Testing

Every test on this page runs in this site’s own build, with Vitest, jsdom and Testing Library.

RouterAdapter is a plain object type, so a test can give useQubeeList a router it controls: a fixed pathname and search, and a navigate spy. Fake timers step through a debounce without waiting for it.

src/articles/article-search.test.tsx
import { useQubeeList } from '@qubeejs/react';
import type { RouterAdapter } from '@qubeejs/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { articleList } from './article-list';
import { ArticleSearch } from './article-search';
/** The search box, wired to the article list and to a router the test controls. */
function Harness({ router }: { router: RouterAdapter }) {
return <ArticleSearch list={useQubeeList(articleList, router)} />;
}
/** Render the box at /articles, with the given query string (none by default). */
function renderSearch(search = '') {
const navigate = vi.fn<RouterAdapter['navigate']>();
render(<Harness router={{ navigate, pathname: '/articles', search }} />);
return {
input: screen.getByRole<HTMLInputElement>('searchbox', { name: 'Search articles' }),
navigate,
};
}
describe('ArticleSearch', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should keep the space the user just typed', () => {
const { input } = renderSearch();
fireEvent.change(input, { target: { value: 'hello ' } });
expect(input.value).toBe('hello ');
});
it('should navigate once, 300 ms after the last keystroke', () => {
const { input, navigate } = renderSearch();
fireEvent.change(input, { target: { value: 're' } });
fireEvent.change(input, { target: { value: 'react' } });
expect(input.value).toBe('react');
expect(navigate).not.toHaveBeenCalled();
act(() => {
vi.advanceTimersByTime(300);
});
expect(navigate).toHaveBeenCalledTimes(1);
expect(navigate).toHaveBeenCalledWith('/articles?q=react', { replace: true });
});
it('should clear at once, taking the waiting change with it', () => {
const { input, navigate } = renderSearch('?q=old');
fireEvent.change(input, { target: { value: 'react' } });
fireEvent.click(screen.getByRole('button', { name: 'Clear' }));
expect(navigate).toHaveBeenCalledTimes(1);
expect(navigate).toHaveBeenCalledWith('/articles', { replace: false });
expect(input.value).toBe('');
});
});
  • Assert on navigate: the href and { replace } are the whole contract between a list and its router.
  • A fake router never changes its search, so the URL never catches up — which is what lets the test see the committed navigation without a real history.

jsdom implements window.history, so <BrowserAdapter> and useBrowserAdapter() work in tests as they do in a browser. Set the starting URL before rendering, and reset it after each test — jsdom keeps one window for a whole test file:

window.history.replaceState(null, '', '/articles?q=react');

Components that take their adapter from a provider

Section titled “Components that take their adapter from a provider”

A component that calls useQubeeList(articleList) needs an adapter provider above it in the test too, or it throws MissingRouterAdapterError:

  • <BrowserAdapter>, with the History API as above;
  • <MemoryAdapter initialSearch="q=react">, when the test should not touch the URL at all;
  • your router’s adapter inside its memory router — createMemoryRouter for React Router, createMemoryHistory for TanStack Router.

A test that mocks Next’s navigation module must mock next/navigation.js, with the extension: that is the specifier the adapter imports.

Hand the provider an instance the test created, then assert on its store:

src/articles/articles-shell.test.tsx
import { createQubee, STRAPI_DRIVER } from '@qubeejs/core';
import { QubeeProvider } from '@qubeejs/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import type { ReactNode } from 'react';
import { describe, expect, it } from 'vitest';
import { Pager, StatusFilter } from './articles-shell';
/** Render under a provider holding an instance the test can inspect. */
function renderWithQubee(children: ReactNode) {
const qubee = createQubee({ driver: STRAPI_DRIVER });
render(<QubeeProvider value={qubee}>{children}</QubeeProvider>);
return qubee;
}
describe('StatusFilter', () => {
it('should narrow the shared query to published articles', () => {
const qubee = renderWithQubee(<StatusFilter />);
fireEvent.click(screen.getByRole('checkbox', { name: 'Published only' }));
expect(qubee.store.getSnapshot().filters['status']).toEqual(['published']);
});
it('should go back to page 1 when the filter changes', () => {
const qubee = renderWithQubee(<StatusFilter />);
act(() => {
qubee.builder.setPage(3);
});
expect(qubee.store.getSnapshot().page).toBe(3);
fireEvent.click(screen.getByRole('checkbox', { name: 'Published only' }));
expect(qubee.store.getSnapshot().page).toBe(1);
});
});
describe('Pager', () => {
it('should disable Previous on the first page', () => {
renderWithQubee(<Pager />);
expect(screen.getByRole<HTMLButtonElement>('button', { name: 'Previous' }).disabled).toBe(true);
});
it('should re-render when the shared instance moves on', () => {
renderWithQubee(<Pager />);
fireEvent.click(screen.getByRole('button', { name: 'Next' }));
expect(screen.getByText('Page 2')).toBeTruthy();
});
});

The fetching hooks go through fetch, or the fetcher you gave them, so there is nothing of the adapter to mock: mock the API instead. The Testing with MSW recipe answers the list’s requests with a Strapi payload and checks what the list asked for.