Skip to content

Commit a590998

Browse files
mjacksonMichael Jackson
authored and
Michael Jackson
committed
Final pass
I think I've got it now. I'm now interpreting it as defaultValue instead of initialValue, which helps a lot. I also added deprecation warnings for <Broadcast> and <Subscriber> which should help people upgrade. Only remaining question for me is: can I ship code that uses Symbol() to the browser? Do I need a shim for that?
1 parent 70c6269 commit a590998

File tree

6 files changed

+4244
-100
lines changed

6 files changed

+4244
-100
lines changed

modules/Broadcast.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from "react";
22
import PropTypes from "prop-types";
33
import invariant from "invariant";
4+
import createDeprecationWarning from "./createDeprecationWarning";
5+
6+
const deprecationWarning = createDeprecationWarning();
47

58
function createBroadcast(initialValue) {
69
let currentValue = initialValue;
@@ -68,6 +71,13 @@ class Broadcast extends React.Component {
6871
};
6972
}
7073

74+
componentWillMount() {
75+
deprecationWarning(
76+
"<Broadcast> is deprecated and will be removed in the next major release. " +
77+
"Please use createBroadcast instead. See https://goo.gl/QAF37J for more info."
78+
);
79+
}
80+
7181
componentWillReceiveProps(nextProps) {
7282
invariant(
7383
this.props.channel === nextProps.channel,

modules/Subscriber.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from "react";
22
import PropTypes from "prop-types";
33
import invariant from "invariant";
4+
import createDeprecationWarning from "./createDeprecationWarning";
5+
6+
const deprecationWarning = createDeprecationWarning();
47

58
/**
69
* A <Subscriber> pulls the value for a channel off of context.broadcasts
@@ -40,6 +43,11 @@ class Subscriber extends React.Component {
4043
}
4144

4245
componentWillMount() {
46+
deprecationWarning(
47+
"<Subscriber> is deprecated and will be removed in the next major release. " +
48+
"Please use createBroadcast instead. See https://goo.gl/QAF37J for more info."
49+
);
50+
4351
const broadcast = this.getBroadcast();
4452

4553
if (broadcast) {
+74-40
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,64 @@
1-
import React from "react"
2-
import ReactDOM from "react-dom"
3-
import { Simulate } from "react-dom/test-utils"
4-
import createBroadcast from "../createBroadcast"
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import { Simulate } from "react-dom/test-utils";
4+
import createBroadcast from "../createBroadcast";
55

66
describe("createBroadcast", () => {
77
it("creates a Broadcast component", () => {
8-
const { Broadcast } = createBroadcast()
9-
expect(typeof Broadcast).toBe("function")
10-
})
8+
const { Broadcast } = createBroadcast();
9+
expect(typeof Broadcast).toBe("function");
10+
});
1111

1212
it("creates a Subscriber component", () => {
13-
const { Subscriber } = createBroadcast()
14-
expect(typeof Subscriber).toBe("function")
15-
})
16-
})
13+
const { Subscriber } = createBroadcast();
14+
expect(typeof Subscriber).toBe("function");
15+
});
16+
});
17+
18+
describe("A <Broadcast>", () => {
19+
it("knows its default value", () => {
20+
const defaultValue = "bubblegum";
21+
const { Broadcast } = createBroadcast(defaultValue);
22+
expect(defaultValue).toEqual(Broadcast.defaultValue);
23+
});
24+
});
1725

1826
describe("A <Subscriber>", () => {
19-
let node
27+
let node;
2028
beforeEach(() => {
21-
node = document.createElement("div")
22-
})
29+
node = document.createElement("div");
30+
});
2331

2432
it("gets the initial broadcast value on the initial render", done => {
25-
const initialValue = "cupcakes"
26-
const { Broadcast, Subscriber } = createBroadcast(initialValue)
33+
const defaultValue = "cupcakes";
34+
const { Broadcast, Subscriber } = createBroadcast(defaultValue);
2735

28-
let actualValue
36+
let actualValue;
2937

3038
ReactDOM.render(
3139
<Broadcast>
3240
<Subscriber
3341
children={value => {
34-
actualValue = value
35-
return null
42+
actualValue = value;
43+
return null;
3644
}}
3745
/>
3846
</Broadcast>,
3947
node,
4048
() => {
41-
expect(actualValue).toBe(initialValue)
42-
done()
49+
expect(actualValue).toBe(defaultValue);
50+
done();
4351
}
44-
)
45-
})
52+
);
53+
});
4654

4755
it("gets the updated broadcast value as it changes", done => {
48-
const { Broadcast, Subscriber } = createBroadcast("cupcakes")
56+
const { Broadcast, Subscriber } = createBroadcast("cupcakes");
4957

5058
class Parent extends React.Component {
5159
state = {
52-
value: Broadcast.initialValue
53-
}
60+
value: Broadcast.defaultValue
61+
};
5462

5563
render() {
5664
return (
@@ -61,40 +69,66 @@ describe("A <Subscriber>", () => {
6169
/>
6270
<Child />
6371
</Broadcast>
64-
)
72+
);
6573
}
6674
}
6775

68-
let childDidRender = false
76+
let childDidRender = false;
6977

7078
class Child extends React.Component {
71-
// Make sure we can bypass a sCU=false!
79+
// Make sure we can bypass sCU=false!
7280
shouldComponentUpdate() {
73-
return false
81+
return false;
7482
}
7583

7684
render() {
7785
return (
7886
<Subscriber
7987
children={value => {
8088
if (childDidRender) {
81-
expect(value).toBe("bubblegum")
82-
done()
89+
expect(value).toBe("bubblegum");
90+
done();
8391
} else {
84-
expect(value).toBe(Broadcast.initialValue)
92+
expect(value).toBe(Broadcast.defaultValue);
8593
}
8694

87-
childDidRender = true
95+
childDidRender = true;
8896

89-
return null
97+
return null;
9098
}}
9199
/>
92-
)
100+
);
93101
}
94102
}
95103

96104
ReactDOM.render(<Parent />, node, function() {
97-
Simulate.click(this.button)
98-
})
99-
})
100-
})
105+
Simulate.click(this.button);
106+
});
107+
});
108+
109+
describe("under a <Broadcast> with a value different from the default", () => {
110+
it("gets the broadcast value on the initial render", () => {
111+
const defaultValue = "bubblegum";
112+
const { Broadcast, Subscriber } = createBroadcast(defaultValue);
113+
114+
const node = document.createElement("div");
115+
116+
let actualValue;
117+
118+
ReactDOM.render(
119+
<Broadcast value="cupcakes">
120+
<Subscriber
121+
children={value => {
122+
actualValue = value;
123+
return null;
124+
}}
125+
/>
126+
</Broadcast>,
127+
node,
128+
() => {
129+
expect(actualValue).toEqual("cupcakes");
130+
}
131+
);
132+
});
133+
});
134+
});

0 commit comments

Comments
 (0)