Skip to content

Commit d82fec4

Browse files
authored
add property portals to wrapito config (#155)
1 parent 9139184 commit d82fec4

5 files changed

Lines changed: 35 additions & 3 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export interface Config {
8585
changeRoute: (path: string) => void
8686
history?: BrowserHistory
8787
portal?: string
88+
portals?: string[]
8889
handleQueryParams?: boolean
8990
}
9091

src/wrap.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const debugRequests = () => {
115115
}
116116

117117
const getMount = () => {
118-
const { portal, changeRoute, history, mount } = getConfig()
118+
const { portal, portals, changeRoute, history, mount } = getConfig()
119119
const { Component, props, responses, path, hasPath, debug, historyState } =
120120
getOptions()
121121

@@ -125,6 +125,10 @@ const getMount = () => {
125125
setupPortal(portal)
126126
}
127127

128+
if (portals) {
129+
setupPortals(portals)
130+
}
131+
128132
if (hasPath && history) {
129133
console.warn(
130134
'wrapito WARNING: history is DEPRECATED. Pass a changeRoute function to the config instead.',
@@ -154,4 +158,10 @@ const setupPortal = (portalRootId: string) => {
154158
document.body.appendChild(portalRoot)
155159
}
156160

161+
const setupPortals = (portalRootIds: string[]) => {
162+
portalRootIds.forEach(portal => {
163+
setupPortal(portal)
164+
})
165+
}
166+
157167
export { wrap }

tests/components.mock.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export const MyComponentWithProps = props => (
2121
export const MyComponentWithPortal = ({ children }) =>
2222
createPortal(children, document.getElementById('portal-root-id'))
2323

24+
export const MyComponentWithPortals = ({ children, portalIds }) => {
25+
return portalIds.map((portalId, index) => {
26+
return createPortal(children[index], document.getElementById(portalId))
27+
})
28+
}
29+
2430
export class MyComponentMakingHttpCalls extends Component {
2531
state = {
2632
quantity: 0,

tests/lib/wrap.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MyComponent,
66
MyComponentWithProps,
77
MyComponentWithPortal,
8+
MyComponentWithPortals,
89
} from '../components.mock'
910
import { it, expect, afterEach } from 'vitest'
1011

@@ -49,6 +50,20 @@ it('should have an element where to place a portal defined in the config', () =>
4950
expect(document.body).toHaveTextContent(childrenText)
5051
})
5152

53+
it('should have elements where to place portals defined in the config', () => {
54+
const childrenTexts = ['I am a portal 1', 'I am a portal 2']
55+
const portalIds = ['portal-root-id1', 'portal-root-id2']
56+
57+
configure({ portals: portalIds })
58+
const props = { children: childrenTexts, portalIds }
59+
60+
wrap(MyComponentWithPortals).withProps(props).mount()
61+
62+
childrenTexts.forEach(childrenText => {
63+
expect(document.body).toHaveTextContent(childrenText)
64+
})
65+
})
66+
5267
it('should have unique portals', () => {
5368
configure({ mount: render, portal: portalRootId })
5469
const childrenText = 'I am a portal'

0 commit comments

Comments
 (0)