generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support for dynamic default values in useQueryStates (#762)
- Loading branch information
Showing
4 changed files
with
193 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('repro-760', () => { | ||
it('supports dynamic default values', () => { | ||
cy.visit('/app/repro-760') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#value-a').should('have.text', 'a') | ||
cy.get('#value-b').should('have.text', 'b') | ||
cy.get('#trigger-a').click() | ||
cy.get('#trigger-b').click() | ||
cy.get('#value-a').should('have.text', 'pass') | ||
cy.get('#value-b').should('have.text', 'pass') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use client' | ||
|
||
import { parseAsString, useQueryState, useQueryStates } from 'nuqs' | ||
import { Suspense, useState } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<DynamicUseQueryState /> | ||
<DynamicUseQueryStates /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
function DynamicUseQueryState() { | ||
const [defaultValue, setDefaultValue] = useState('a') | ||
const [value] = useQueryState('a', parseAsString.withDefault(defaultValue)) | ||
return ( | ||
<section> | ||
<button id="trigger-a" onClick={() => setDefaultValue('pass')}> | ||
Trigger | ||
</button> | ||
<span id="value-a">{value}</span> | ||
</section> | ||
) | ||
} | ||
|
||
function DynamicUseQueryStates() { | ||
const [defaultValue, setDefaultValue] = useState('b') | ||
const [{ value }] = useQueryStates( | ||
{ | ||
value: parseAsString.withDefault(defaultValue) | ||
}, | ||
{ urlKeys: { value: 'b' } } | ||
) | ||
return ( | ||
<section> | ||
<button id="trigger-b" onClick={() => setDefaultValue('pass')}> | ||
Trigger | ||
</button> | ||
<span id="value-b">{value}</span> | ||
</section> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { act, renderHook } from '@testing-library/react' | ||
import type { ReactNode } from 'react' | ||
import React from 'react' | ||
import { describe, expect, it } from 'vitest' | ||
import { NuqsTestingAdapter } from './adapters/testing' | ||
import { parseAsArrayOf, parseAsJson, parseAsString } from './parsers' | ||
import { useQueryStates } from './useQueryStates' | ||
|
||
function withSearchParams( | ||
searchParams?: string | URLSearchParams | Record<string, string> | ||
) { | ||
return (props: { children: ReactNode }) => ( | ||
<NuqsTestingAdapter searchParams={searchParams} {...props} /> | ||
) | ||
} | ||
|
||
const defaults = { | ||
str: 'foo', | ||
obj: { initial: 'state' }, | ||
arr: [ | ||
{ | ||
initial: 'state' | ||
} | ||
] | ||
} | ||
|
||
const hook = ({ defaultValue } = { defaultValue: defaults.str }) => { | ||
return useQueryStates({ | ||
str: parseAsString.withDefault(defaultValue), | ||
obj: parseAsJson<any>(x => x).withDefault(defaults.obj), | ||
arr: parseAsArrayOf(parseAsJson<any>(x => x)).withDefault(defaults.arr) | ||
}) | ||
} | ||
|
||
describe('useQueryStates', () => { | ||
it('should have referential equality on default values', () => { | ||
const { result } = renderHook(hook, { | ||
wrapper: NuqsTestingAdapter | ||
}) | ||
const [state] = result.current | ||
expect(state.str).toBe(defaults.str) | ||
expect(state.obj).toBe(defaults.obj) | ||
expect(state.arr).toBe(defaults.arr) | ||
expect(state.arr[0]).toBe(defaults.arr[0]) | ||
}) | ||
|
||
it('should keep referential equality when resetting to defaults', () => { | ||
const { result } = renderHook(hook, { | ||
wrapper: withSearchParams({ | ||
str: 'foo', | ||
obj: '{"hello":"world"}', | ||
arr: '{"obj":true},{"arr":true}' | ||
}) | ||
}) | ||
act(() => { | ||
result.current[1](null) | ||
}) | ||
const [state] = result.current | ||
expect(state.str).toBe(defaults.str) | ||
expect(state.obj).toBe(defaults.obj) | ||
expect(state.arr).toBe(defaults.arr) | ||
expect(state.arr[0]).toBe(defaults.arr[0]) | ||
}) | ||
|
||
it('should keep referential equality when unrelated keys change', () => { | ||
const { result } = renderHook(hook, { | ||
wrapper: withSearchParams({ | ||
str: 'foo', | ||
obj: '{"hello":"world"}' | ||
// Keep arr as default | ||
}) | ||
}) | ||
const [{ obj: initialObj, arr: initialArr }] = result.current | ||
act(() => { | ||
result.current[1]({ str: 'bar' }) | ||
}) | ||
const [{ str, obj, arr }] = result.current | ||
expect(str).toBe('bar') | ||
expect(obj).toBe(initialObj) | ||
expect(arr).toBe(initialArr) | ||
}) | ||
|
||
it('should keep referential equality when default changes for another key', () => { | ||
const { result, rerender } = renderHook(hook, { | ||
wrapper: withSearchParams() | ||
}) | ||
expect(result.current[0].str).toBe('foo') | ||
rerender({ defaultValue: 'b' }) | ||
const [state] = result.current | ||
expect(state.str).toBe('b') | ||
expect(state.obj).toBe(defaults.obj) | ||
expect(state.arr).toBe(defaults.arr) | ||
expect(state.arr[0]).toBe(defaults.arr[0]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters