-
-
Notifications
You must be signed in to change notification settings - Fork 635
/
props-context.spec.server.tsx
120 lines (104 loc) · 2.85 KB
/
props-context.spec.server.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { renderToStaticMarkup } from 'inferno-server';
import { Component } from 'inferno';
describe('SSR render() arguments', () => {
class TestProvider extends Component {
getChildContext() {
return { testContext: 'context-works' };
}
render({ children }) {
return children;
}
}
it('should have props as 1st argument', () => {
interface TestChildProps {
testProps: string
}
class TestChild extends Component<TestChildProps> {
render(props) {
return <p>{props.testProps}</p>;
}
}
const output = renderToStaticMarkup(<TestChild testProps="props-works" />);
expect(output).toBe('<p>props-works</p>');
});
it('should have state as 2nd argument', () => {
class TestChild extends Component {
constructor() {
super();
this.state = { testState: 'state-works' };
}
render(_props, state) {
return <p>{state.testState}</p>;
}
}
const output = renderToStaticMarkup(<TestChild />);
expect(output).toBe('<p>state-works</p>');
});
it('statefull has context as 3rd argument', () => {
class TestChild extends Component {
render(_props, _state, context) {
return <p>{context.testContext}</p>;
}
}
const output = renderToStaticMarkup(
<TestProvider>
<TestChild />
</TestProvider>,
);
expect(output).toBe('<p>context-works</p>');
});
it('stateless has context as 2nd argument', () => {
function TestChild(_props, context) {
return <p>{context.testContext}</p>;
}
const output = renderToStaticMarkup(
<TestProvider>
<TestChild />
</TestProvider>,
);
expect(output).toBe('<p>context-works</p>');
});
it('nested stateless has context as 2nd argument', () => {
function ChildWrapper(props) {
return props.children;
}
function TestChild(_props, context) {
return <p>{context.testContext}</p>;
}
const output = renderToStaticMarkup(
<TestProvider>
<ChildWrapper>
<ChildWrapper>
<TestChild />
</ChildWrapper>
</ChildWrapper>
</TestProvider>,
);
expect(output).toBe('<p>context-works</p>');
});
it('nested providers should have merged context', () => {
class TestContext extends Component {
getChildContext() {
return { testContextWrap: 'context-wrap-works' };
}
render({ children }) {
return children;
}
}
function TestChild(_props: unknown, context) {
return (
<p>
{context.testContext}|{context.testContextWrap}
</p>
);
}
const output = renderToStaticMarkup(
<TestProvider>
<TestContext>
<TestChild />
</TestContext>
</TestProvider>,
);
expect(output).toBe('<p>context-works|context-wrap-works</p>');
});
});