Skip to content

Commit 666aa4a

Browse files
committed
feat(useLocationHashQueryState): provide new exports useLocationHashQueryState and useLocationHashQu
BREAKING CHANGE: renamed exports, queryState and setQueryState are now all returned as array tuples fix #20
1 parent 9dc6d41 commit 666aa4a

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

src/examples/use-location-state/01-simple/src/QueryStateTest.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import React from 'react'
2-
import { useLocationQueryState, useLocationHashQueryStringInterface } from 'use-location-state'
2+
import { useLocationHashQueryState } from 'use-location-state'
33

44
interface Props {}
55

6-
const queryStateDefault = { name: 'Sarah', age: 25 }
7-
86
export default function QueryStateTest(props: Props) {
9-
const locationHashQueryStringInterface = useLocationHashQueryStringInterface()
10-
const [queryState, setQueryState] = useLocationQueryState(queryStateDefault, {
11-
queryStringInterface: locationHashQueryStringInterface,
12-
})
7+
const [queryState, setQueryState] = useLocationHashQueryState({ name: 'Sarah', age: 25 })
138

149
return (
1510
<div>
Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { act, renderHook } from 'react-hooks-testing-library'
2-
import { useLocationQueryState, useLocationHashQueryStringInterface } from './use-location-state'
2+
import {
3+
useLocationQueryState,
4+
useLocationHashQueryState,
5+
useLocationHashQueryStringInterface,
6+
useLocationHashQueryStateItem,
7+
} from './use-location-state'
8+
import { cleanup } from 'react-testing-library'
39

410
// reset jest mocked hash
511
beforeAll(() => {
@@ -8,10 +14,11 @@ beforeAll(() => {
814

915
afterEach(() => {
1016
location.hash = ''
17+
cleanup()
1118
})
1219

13-
describe('useLocationQueryState hook with hash GetSetQueryStringInterface', () => {
14-
it('TODO: ...', () => {
20+
describe('useLocationQueryState hook', () => {
21+
it('should work with passed HashQueryStringInterface', () => {
1522
const hashQSI = renderHook(() => useLocationHashQueryStringInterface())
1623
const { result } = renderHook(
1724
props => useLocationQueryState(props, { queryStringInterface: hashQSI.result.current }),
@@ -21,10 +28,56 @@ describe('useLocationQueryState hook with hash GetSetQueryStringInterface', () =
2128
)
2229

2330
expect(result.current[0]).toEqual({ name: 'Sarah' })
24-
2531
act(() => result.current[1]({ name: 'Kim' }))
32+
expect(location.hash).toEqual('#name=Kim')
33+
expect(result.current[0]).toEqual({ name: 'Kim' })
34+
})
35+
})
36+
37+
describe('useLocationHashQueryState hook', () => {
38+
it('should work with internal HashQueryStringInterface', () => {
39+
const { result } = renderHook(props => useLocationHashQueryState(props), {
40+
initialProps: { name: 'Sarah' },
41+
})
2642

43+
expect(result.current[0]).toEqual({ name: 'Sarah' })
44+
act(() => result.current[1]({ name: 'Kim' }))
2745
expect(location.hash).toEqual('#name=Kim')
2846
expect(result.current[0]).toEqual({ name: 'Kim' })
2947
})
3048
})
49+
50+
describe('useLocationHashQueryStateItem hook', () => {
51+
it('should work with internal HashQueryStringInterface', () => {
52+
const { result } = renderHook(
53+
({ itemName, defaultValue }) => useLocationHashQueryStateItem(itemName, defaultValue),
54+
{
55+
initialProps: { itemName: 'name', defaultValue: 'Sarah' },
56+
}
57+
)
58+
59+
expect(result.current[0]).toEqual('Sarah')
60+
act(() => result.current[1]('Kim'))
61+
expect(location.hash).toEqual('#name=Kim')
62+
expect(result.current[0]).toEqual('Kim')
63+
})
64+
65+
it('should reset hash when default', () => {
66+
const { result } = renderHook(
67+
({ itemName, defaultValue }) => useLocationHashQueryStateItem(itemName, defaultValue),
68+
{
69+
initialProps: { itemName: 'name', defaultValue: 'Sarah' },
70+
}
71+
)
72+
73+
expect(result.current[0]).toEqual('Sarah')
74+
75+
act(() => result.current[1]('Kim'))
76+
expect(location.hash).toEqual('#name=Kim')
77+
expect(result.current[0]).toEqual('Kim')
78+
79+
act(() => result.current[1]('Sarah'))
80+
expect(location.hash).toEqual('')
81+
expect(result.current[0]).toEqual('Sarah')
82+
})
83+
})

src/packages/use-location-state/src/use-location-state.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export { useLocationHashQueryStringInterface } from './hooks/useLocationHashQuer
55

66
type QueryString = string
77
type ExtendedQueryState<T> = Partial<T> & QueryState
8-
type SetQueryStateFn<T> = (newState: Partial<T> & QueryState, opts?: SetQueryStringOptions) => void
8+
type SetQueryStateFn<T> = (newState: ExtendedQueryState<T>, opts?: SetQueryStringOptions) => void
9+
type SetQueryStateItemFn<T> = (newValue: T, opts?: SetQueryStringOptions) => void
910

1011
export interface QueryStringInterface {
1112
getQueryString: () => QueryString
@@ -102,12 +103,11 @@ export function useLocationQueryStateItem<T>(
102103
itemName: string,
103104
defaultValue: T,
104105
queryStateOpts: QueryStateOpts
105-
): [T, (newValue: T, opts?: SetQueryStringOptions) => void] {
106+
): [T, SetQueryStateItemFn<T>] {
106107
const defaultQueryState = useMemo(() => ({ [itemName]: defaultValue }), [
107108
itemName,
108109
defaultValue,
109110
])
110-
// const { queryState, setQueryState } = useLocationQueryState(defaultQueryState, queryStateOpts)
111111
const [queryState, setQueryState] = useLocationQueryState(defaultQueryState, queryStateOpts)
112112
const setQueryStateItem = useCallback(
113113
(newValue: T, opts?: SetQueryStringOptions) => setQueryState({ [itemName]: newValue }, opts),
@@ -125,9 +125,8 @@ export function useLocationHashQueryStateItem<T>(
125125
itemName: string,
126126
defaultValue: T,
127127
queryStateOpts: QueryStateOptsSetInterface = {}
128-
): [T, SetQueryStateFn<T>] {
128+
): [T, SetQueryStateItemFn<T>] {
129129
const hashGSI = useLocationHashQueryStringInterface()
130-
131130
return useLocationQueryStateItem(itemName, defaultValue, {
132131
...queryStateOpts,
133132
queryStringInterface: hashGSI

0 commit comments

Comments
 (0)