UNPKG

1.23 kBJavaScriptView Raw
1import React from "react"
2import { PropTypes as T } from "prop-types"
3import createReactClass from "create-react-class"
4import Renderer from "react-test-renderer"
5import portalMixin from "../lib/react-layer-mixin"
6
7
8
9class ContextProvider extends React.Component {
10 static propTypes = {
11 children: T.node,
12 }
13 static defaultProps = {
14 children: null,
15 }
16 static childContextTypes = {
17 x: T.string,
18 }
19 getChildContext () {
20 return ({
21 x: "value-from-context",
22 })
23 }
24 render () {
25 return (
26 this.props.children
27 )
28 }
29}
30
31
32
33class ContextDependent extends React.Component {
34 static contextTypes = {
35 x: T.string,
36 }
37 render () {
38 return (
39 <span>{this.context.x}</span>
40 )
41 }
42}
43
44const Portal = createReactClass({
45 propTypes: {
46 children: T.node
47 },
48 mixins: [portalMixin()],
49 renderLayer () {
50 return (
51 this.props.children
52 )
53 },
54 render () {
55 return null
56 }
57})
58
59
60
61beforeEach(() => {
62 document.body.innerHTML = ""
63})
64
65it("Pass context through to layer", () => {
66 Renderer.create(
67 <ContextProvider>
68 <Portal>
69 <ContextDependent />
70 </Portal>
71 </ContextProvider>
72 )
73 expect(document.body.innerHTML).toMatchSnapshot()
74})