|
1 | | -import React from "react" |
2 | | -import ReactDOM from "react-dom" |
3 | | -import { EventEmitter } from "events" |
4 | | -import { Broadcast, Subscriber } from "../index" |
| 1 | +import React from "react"; |
| 2 | +import ReactDOM from "react-dom"; |
| 3 | +import { EventEmitter } from "events"; |
| 4 | +import { Broadcast, Subscriber } from "../index"; |
5 | 5 |
|
6 | 6 | it("works", done => { |
7 | | - const steps = [] |
8 | | - const execNextStep = () => steps.shift()() |
9 | | - const div = document.createElement("div") |
| 7 | + const steps = []; |
| 8 | + const execNextStep = () => steps.shift()(); |
| 9 | + const div = document.createElement("div"); |
10 | 10 |
|
11 | 11 | // so we can trigger rerenders in ComponentWithStateForDescendants |
12 | | - const emitter = new EventEmitter() |
| 12 | + const emitter = new EventEmitter(); |
13 | 13 |
|
14 | 14 | // A component has some state it wants to make available to descendants |
15 | 15 | // 1. We create our Broadcast and Subscriber components |
16 | 16 | const CheeseBroadcast = ({ cheese, children }) => ( |
17 | 17 | <Broadcast channel="cheese" value={cheese} children={children} /> |
18 | | - ) |
| 18 | + ); |
19 | 19 |
|
20 | | - const CheeseSubscriber = ({ children }) => <Subscriber channel="cheese" children={children} /> |
| 20 | + const CheeseSubscriber = ({ children }) => ( |
| 21 | + <Subscriber channel="cheese" children={children} /> |
| 22 | + ); |
21 | 23 |
|
22 | 24 | class ComponentWithStateForDescendants extends React.Component { |
23 | 25 | constructor() { |
24 | | - super() |
25 | | - this.state = { cheese: "cheddar" } |
| 26 | + super(); |
| 27 | + this.state = { cheese: "cheddar" }; |
26 | 28 | emitter.on("CHEESE", cheese => { |
27 | | - this.setState({ cheese }) |
28 | | - }) |
| 29 | + this.setState({ cheese }); |
| 30 | + }); |
29 | 31 | } |
30 | 32 |
|
31 | | - componentDidMount = execNextStep |
| 33 | + componentDidMount = execNextStep; |
32 | 34 |
|
33 | | - componentDidUpdate = execNextStep |
| 35 | + componentDidUpdate = execNextStep; |
34 | 36 |
|
35 | 37 | render() { |
36 | 38 | // 2. render the Broadcast in the component w/ state and pass |
37 | 39 | // it the value we want accessible through context as a prop |
38 | 40 | // by the same name as when the Broadcast was created |
39 | | - return <CheeseBroadcast cheese={this.state.cheese}>{this.props.children}</CheeseBroadcast> |
| 41 | + return ( |
| 42 | + <CheeseBroadcast cheese={this.state.cheese}> |
| 43 | + {this.props.children} |
| 44 | + </CheeseBroadcast> |
| 45 | + ); |
40 | 46 | } |
41 | 47 | } |
42 | 48 |
|
43 | | - let actualCheese = null |
| 49 | + let actualCheese = null; |
44 | 50 |
|
45 | 51 | steps.push( |
46 | 52 | () => { |
47 | | - expect(actualCheese).toBe("cheddar") |
48 | | - emitter.emit("CHEESE", "gouda") |
| 53 | + expect(actualCheese).toBe("cheddar"); |
| 54 | + emitter.emit("CHEESE", "gouda"); |
49 | 55 | }, |
50 | 56 | () => { |
51 | | - expect(actualCheese).toBe("gouda") |
52 | | - done() |
| 57 | + expect(actualCheese).toBe("gouda"); |
| 58 | + done(); |
53 | 59 | } |
54 | | - ) |
| 60 | + ); |
55 | 61 |
|
56 | 62 | // 3. Render a <Subscriber> that calls back when the Broadcast |
57 | 63 | // gets a new value in its prop |
58 | 64 | ReactDOM.render( |
59 | 65 | <ComponentWithStateForDescendants> |
60 | 66 | <CheeseSubscriber> |
61 | 67 | {cheese => { |
62 | | - actualCheese = cheese |
63 | | - return null |
| 68 | + actualCheese = cheese; |
| 69 | + return null; |
64 | 70 | }} |
65 | 71 | </CheeseSubscriber> |
66 | 72 | </ComponentWithStateForDescendants>, |
67 | 73 | div |
68 | | - ) |
69 | | -}) |
| 74 | + ); |
| 75 | +}); |
0 commit comments