Testing
The hooks need <ParlaProvider>, and never suspend once their namespaces are loaded. With Vitest and
Testing Library, load them all once:
import { createParla, ParlaProvider } from '@parlajs/react';import type { Namespace } from '@parlajs/react';import { Fragment } from 'react';import type { JSXElementConstructor, ReactNode } from 'react';
import { catalog } from '../src/parla/catalog.js';import { functions, markup } from '../src/parlaOptions.js'; // the app's own, if the config declares any
const modules = import.meta.glob<Record<string, Namespace>>('../src/parla/namespaces/*.js', { eager: true,});
export const parla = createParla({ catalog, functions, markup });await parla.load(Object.values(modules).flatMap((mod) => Object.values(mod)));
type Wrapper = JSXElementConstructor<{ children: ReactNode }>;
/** `<ParlaProvider>`, around a test's own wrapper. */export const withParla = (Inner: Wrapper = Fragment): Wrapper => function Wrapped({ children }) { return ( <ParlaProvider parla={parla}> <Inner>{children}</Inner> </ParlaProvider> ); };render(<Cart />, { wrapper: withParla() });Every render at once
Section titled “Every render at once”For specs that call render themselves, wrap it in a setupFiles entry:
import type * as Rtl from '@testing-library/react';import type { ReactNode } from 'react';import { vi } from 'vitest';
vi.mock('@testing-library/react', async (importOriginal) => { const rtl = await importOriginal<typeof Rtl>(); const { withParla } = await import('./parla.js'); return { ...rtl, render: (ui: ReactNode, options: Rtl.RenderOptions = {}) => rtl.render(ui, { ...options, wrapper: withParla(options.wrapper) }), renderHook: (hook: () => unknown, options: Rtl.RenderHookOptions<unknown> = {}) => rtl.renderHook(hook, { ...options, wrapper: withParla(options.wrapper) }), };});A test that switches locale creates its own instance and renders its own <ParlaProvider>.