11import { 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
511beforeAll ( ( ) => {
@@ -8,10 +14,11 @@ beforeAll(() => {
814
915afterEach ( ( ) => {
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+ } )
0 commit comments