1 | import React from "react"
|
2 | import { PropTypes as T } from "prop-types"
|
3 | import createReactClass from "create-react-class"
|
4 | import Renderer from "react-test-renderer"
|
5 | import portalMixin from "../lib/react-layer-mixin"
|
6 |
|
7 |
|
8 |
|
9 | class 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 |
|
33 | class 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 |
|
44 | const 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 |
|
61 | beforeEach(() => {
|
62 | document.body.innerHTML = ""
|
63 | })
|
64 |
|
65 | it("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 | })
|