Skip to content

Commit 8008c8c

Browse files
Add context test
1 parent 4fbd9ac commit 8008c8c

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

__tests__/Context-test.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import { Record } from "immutable";
4+
5+
import {
6+
ReComponent,
7+
NoUpdate,
8+
Update,
9+
SideEffects,
10+
UpdateWithSideEffects
11+
} from "../";
12+
13+
import { click, withConsoleMock } from "./helpers";
14+
15+
describe("ReComponent", () => {
16+
let container;
17+
beforeEach(() => {
18+
container = document.createElement("div");
19+
document.body.appendChild(container);
20+
});
21+
22+
const { Provider, Consumer } = React.createContext();
23+
24+
class Counter extends React.Component {
25+
render() {
26+
return (
27+
<Consumer>
28+
{({ state, handleClick }) => (
29+
<button onClick={handleClick}>
30+
You’ve clicked this {state.count} times(s)
31+
</button>
32+
)}
33+
</Consumer>
34+
);
35+
}
36+
}
37+
38+
class DeepTree extends React.Component {
39+
render() {
40+
return <Counter />;
41+
}
42+
}
43+
44+
class Container extends ReComponent {
45+
constructor() {
46+
super();
47+
this.handleClick = this.createSender("CLICK");
48+
}
49+
50+
initialState(props) {
51+
return {
52+
count: 0
53+
};
54+
}
55+
56+
reducer(action, state) {
57+
switch (action.type) {
58+
case "CLICK":
59+
return Update({ count: state.count + 1 });
60+
}
61+
}
62+
63+
render() {
64+
return (
65+
<Provider value={{ state: this.state, handleClick: this.handleClick }}>
66+
<DeepTree />
67+
</Provider>
68+
);
69+
}
70+
}
71+
72+
it("renders the initial state", () => {
73+
const instance = ReactDOM.render(<Container />, container);
74+
expect(container.textContent).toEqual("You’ve clicked this 0 times(s)");
75+
});
76+
77+
it("increases the counter when clicked", () => {
78+
const instance = ReactDOM.render(<Container />, container);
79+
click(container.firstChild);
80+
expect(container.textContent).toEqual("You’ve clicked this 1 times(s)");
81+
});
82+
});

0 commit comments

Comments
 (0)