@@ -23,6 +23,15 @@ test("should support initial path", () => {
2323 unmount ( ) ;
2424} ) ;
2525
26+ test ( "should support initial state" , ( ) => {
27+ const memory = memoryLocation ( {
28+ path : "/test-case" ,
29+ state : { from : "test" } ,
30+ } ) ;
31+
32+ expect ( memory . state ) . toStrictEqual ( { from : "test" } ) ;
33+ } ) ;
34+
2635test ( "should support initial path with query" , ( ) => {
2736 const { searchHook } = memoryLocation ( { path : "/test-case?foo=bar" } ) ;
2837
@@ -78,6 +87,28 @@ test("should return standalone `navigate` method", () => {
7887 unmount ( ) ;
7988} ) ;
8089
90+ test ( "should update state through standalone `navigate`" , ( ) => {
91+ const memory = memoryLocation ( {
92+ path : "/test-case" ,
93+ state : { from : "test" } ,
94+ } ) ;
95+
96+ memory . navigate ( "/standalone" , { state : { from : "navigate" } } ) ;
97+
98+ expect ( memory . state ) . toStrictEqual ( { from : "navigate" } ) ;
99+ } ) ;
100+
101+ test ( "should preserve state when navigating without `state` option" , ( ) => {
102+ const memory = memoryLocation ( {
103+ path : "/test-case" ,
104+ state : { from : "test" } ,
105+ } ) ;
106+
107+ memory . navigate ( "/standalone" ) ;
108+
109+ expect ( memory . state ) . toStrictEqual ( { from : "test" } ) ;
110+ } ) ;
111+
81112test ( "should return location hook that supports navigation" , ( ) => {
82113 const { hook } = memoryLocation ( ) ;
83114
@@ -90,6 +121,20 @@ test("should return location hook that supports navigation", () => {
90121 unmount ( ) ;
91122} ) ;
92123
124+ test ( "should update state through hook navigation" , ( ) => {
125+ const memory = memoryLocation ( {
126+ path : "/test-case" ,
127+ state : { from : "test" } ,
128+ } ) ;
129+
130+ const { result, unmount } = renderHook ( ( ) => memory . hook ( ) ) ;
131+
132+ act ( ( ) => result . current [ 1 ] ( "/location" , { state : { from : "hook" } } ) ) ;
133+
134+ expect ( memory . state ) . toStrictEqual ( { from : "hook" } ) ;
135+ unmount ( ) ;
136+ } ) ;
137+
93138test ( "should record all history when `record` option is provided" , ( ) => {
94139 const {
95140 hook,
@@ -161,3 +206,18 @@ test("should have reset method that reset hook location", () => {
161206
162207 unmount ( ) ;
163208} ) ;
209+
210+ test ( "should reset state to its initial value" , ( ) => {
211+ const memory = memoryLocation ( {
212+ record : true ,
213+ path : "/test" ,
214+ state : { from : "initial" } ,
215+ } ) ;
216+
217+ memory . navigate ( "/location" , { state : { from : "navigate" } } ) ;
218+ expect ( memory . state ) . toStrictEqual ( { from : "navigate" } ) ;
219+
220+ memory . reset ( ) ;
221+
222+ expect ( memory . state ) . toStrictEqual ( { from : "initial" } ) ;
223+ } ) ;
0 commit comments