import '@testing-library/jest-dom/extend-expect'; import { cleanup, render } from '@testing-library/react'; import * as React from 'react'; import { describe, it } from 'vitest'; import { DecodedValueMap, EncodedQuery, NumberParam, StringParam, objectToSearchString, } from 'serialize-query-params'; import { QueryParamAdapter, QueryParamProvider, SetQuery, withQueryParams, } from '../index'; import { calledPushQuery, makeMockAdapter } from './helpers'; // helper to setup tests function setupWrapper(query: EncodedQuery) { const Adapter = makeMockAdapter({ search: objectToSearchString(query) }); const adapter = (Adapter as any).adapter as QueryParamAdapter; const wrapper = ({ children }: any) => ( {children} ); return { wrapper, adapter }; } const queryConfig = { foo: NumberParam, bar: StringParam }; interface Props { query: DecodedValueMap; setQuery: SetQuery; other: string; } const MockComponent: React.FC = ({ query, setQuery, other }) => { return (
other = {other}
foo = {query.foo}
bar = {query.bar}
); }; const MockWithHoc = withQueryParams(queryConfig, MockComponent); describe('withQueryParams', () => { afterEach(cleanup); it('works', () => { const { wrapper, adapter } = setupWrapper({ foo: '123', bar: 'xxx', }); const { getByText } = render(, { wrapper, }); // @ts-ignore expect(getByText(/other = zing/)).toBeInTheDocument(); // @ts-ignore expect(getByText(/foo = 123/)).toBeInTheDocument(); // @ts-ignore expect(getByText(/bar = xxx/)).toBeInTheDocument(); getByText(/change foo/).click(); expect(calledPushQuery(adapter, 0)).toEqual({ foo: '99', bar: 'xxx' }); }); });