Skip to content

Commit 08c8a8e

Browse files
[Added] location.state support in .atPath (#138)
* [Added] location.state support in .atPath * improve naming with historyState
1 parent 5809a50 commit 08c8a8e

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

src/models.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface WrapResponse extends Response {
1818

1919
interface Wrap {
2020
withNetwork: (responses: WrapResponse[]) => Wrap
21-
atPath: (path: string) => Wrap
21+
atPath: (path: string, historyState: object) => Wrap
2222
withProps: (props: object) => Wrap
2323
debugRequests: () => Wrap
2424
mount: () => object
@@ -29,6 +29,7 @@ interface WrapOptions {
2929
responses: WrapResponse[]
3030
props: object
3131
path: string
32+
historyState?: object
3233
hasPath: boolean
3334
debug: boolean
3435
}
@@ -57,7 +58,7 @@ interface Config {
5758
}
5859

5960
interface BrowserHistory extends History {
60-
push: (path: string) => void
61+
push: (path: string, historyState?: object) => void
6162
}
6263

6364
export {

src/wrap.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ const withNetwork = (responses: Response[] = []) => {
9898
return wrapWith()
9999
}
100100

101-
const atPath = (path: string) => {
101+
const atPath = (path: string, historyState: object) => {
102102
const options = getOptions()
103-
updateOptions({ ...options, path, hasPath: true })
103+
updateOptions({ ...options, historyState, path, hasPath: true })
104104
return wrapWith()
105105
}
106106

@@ -112,7 +112,7 @@ const debugRequests = () => {
112112

113113
const getMount = () => {
114114
const { portal, changeRoute, history, mount } = getConfig()
115-
const { Component, props, responses, path, hasPath, debug } = getOptions()
115+
const { Component, props, responses, path, hasPath, debug, historyState } = getOptions()
116116

117117
if (portal) {
118118
setupPortal(portal)
@@ -125,7 +125,7 @@ const getMount = () => {
125125
console.warn(
126126
'Read about changeRoute in: https://github.com/mercadona/wrapito#changeRoute',
127127
)
128-
history.push(path)
128+
history.push(path, historyState)
129129
}
130130

131131
if (hasPath && !history) {

tests/atPath.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,10 @@ it('should render an app with a routing logic between pages', () => {
6262

6363
expect(container).toHaveTextContent('Categories')
6464
})
65+
66+
it('should render an app with a location state', async () =>{
67+
configure({ history })
68+
wrap(MyAppWithRouting).atPath('/page-using-location-state', {title: "title"}).mount()
69+
70+
expect(await screen.findByText('title')).toBeInTheDocument()
71+
})

tests/components.mock.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component, Fragment, useState, useEffect } from 'react'
22
import { createPortal } from 'react-dom'
33
import { Provider, useDispatch, useSelector } from 'react-redux'
4-
import { Router, BrowserRouter, Switch, Route } from 'react-router-dom'
4+
import { Router, BrowserRouter, Switch, Route, useLocation } from 'react-router-dom'
55
import { createBrowserHistory } from 'history'
66
import { createStore, applyMiddleware } from 'redux'
77
import thunk from 'redux-thunk'
@@ -73,6 +73,17 @@ const Categories = () => {
7373
return <div>Categories</div>
7474
}
7575

76+
const PageUsingLocationState = () => {
77+
const [title, setTitle] = useState()
78+
const location = useLocation()
79+
80+
useEffect(() => {
81+
if (location.state) setTitle(location.state.title)
82+
},[location.state])
83+
84+
return <div>{title}</div>
85+
}
86+
7687
export const history = createBrowserHistory()
7788

7889
export const MyAppWithRouting = () => {
@@ -86,6 +97,12 @@ export const MyAppWithRouting = () => {
8697
component={ Categories }
8798
exact={ true }
8899
/>
100+
<Route
101+
component={ PageUsingLocationState }
102+
key="page-using-location-state"
103+
path="/page-using-location-state"
104+
exact={true}
105+
/>
89106
</Switch>
90107
</Router>
91108
)

0 commit comments

Comments
 (0)