use-push-router
Version:
A custom hook that simplifies modifying the search params in Next.js
74 lines • 3.75 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { vi, describe, it, expect } from 'vitest';
const pushMock = vi.fn();
vi.mock('next/navigation', () => ({
useRouter: () => ({
push: pushMock,
replace: vi.fn(),
prefetch: vi.fn(),
}),
usePathname: () => '/test-path',
useSearchParams: () => new URLSearchParams([['foo', 'bar']]),
}));
import { renderHook, act } from '@testing-library/react';
import { usePushRoute } from './use-push-route';
describe('usePushRoute', () => {
it('should set the existing search params correctly', () => __awaiter(void 0, void 0, void 0, function* () {
const { result } = renderHook(() => usePushRoute());
act(() => {
result.current.pushSearchParams({ set: { foo: 'quz' } });
});
expect(pushMock).toHaveBeenCalledWith('/test-path?foo=quz');
}));
it('should set the new search params correctly', () => __awaiter(void 0, void 0, void 0, function* () {
const { result } = renderHook(() => usePushRoute());
act(() => {
result.current.pushSearchParams({ set: { man: 'dude' } });
});
expect(pushMock).toHaveBeenCalledWith('/test-path?foo=bar&man=dude');
}));
it('should set the new search params array correctly', () => __awaiter(void 0, void 0, void 0, function* () {
const { result } = renderHook(() => usePushRoute());
act(() => {
result.current.pushSearchParams({ set: { man: ['dude', 'dudette'] } });
});
expect(pushMock).toHaveBeenCalledWith('/test-path?foo=bar&man=dude&man=dudette');
}));
it('should remove the search params correctly', () => __awaiter(void 0, void 0, void 0, function* () {
const { result } = renderHook(() => usePushRoute());
act(() => {
result.current.pushSearchParams({ remove: { foo: 'bar' } });
});
expect(pushMock).toHaveBeenCalledWith('/test-path?');
}));
it('should remove the search params when the value is undefined', () => __awaiter(void 0, void 0, void 0, function* () {
const { result } = renderHook(() => usePushRoute());
act(() => {
result.current.pushSearchParams({ remove: { foo: undefined } });
});
expect(pushMock).toHaveBeenCalledWith('/test-path?');
}));
it('should add the search params correctly', () => __awaiter(void 0, void 0, void 0, function* () {
const { result } = renderHook(() => usePushRoute());
act(() => {
result.current.pushSearchParams({ add: { baz: 'qux' } });
});
expect(pushMock).toHaveBeenCalledWith('/test-path?foo=bar&baz=qux');
}));
it('should add the search params array correctly', () => __awaiter(void 0, void 0, void 0, function* () {
const { result } = renderHook(() => usePushRoute());
act(() => {
result.current.pushSearchParams({ add: { baz: ['qux', 'quux'] } });
});
expect(pushMock).toHaveBeenCalledWith('/test-path?foo=bar&baz=qux&baz=quux');
}));
});
//# sourceMappingURL=use-push-route.test.js.map