Skip to content

Commit ca0d551

Browse files
committed
Add failing tests for coroutines
There are a few different issues: * Updates result in unnecessary duplicate placements because it can't find the current fiber for continuations. * When run together, coroutine update and unmounting tests appear to lock down in an infinite loop. They don't freeze in isolation. I don't have a solution for this but just leaving it for future fixes.
1 parent 356c3bd commit ca0d551

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/renderers/shared/fiber/__tests__/ReactCoroutine-test.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ describe('ReactCoroutine', () => {
2323
ReactCoroutine = require('ReactCoroutine');
2424
});
2525

26+
function div(...children) {
27+
children = children.map(c => typeof c === 'string' ? { text: c } : c);
28+
return { type: 'div', children, prop: undefined };
29+
}
30+
31+
function span(prop) {
32+
return { type: 'span', children: [], prop };
33+
}
34+
2635
it('should render a coroutine', () => {
2736
var ops = [];
2837

2938
function Continuation({ isSame }) {
3039
ops.push(['Continuation', isSame]);
31-
return <span>{isSame ? 'foo==bar' : 'foo!=bar'}</span>;
40+
return <span prop={isSame ? 'foo==bar' : 'foo!=bar'} />;
3241
}
3342

3443
// An alternative API could mark Continuation as something that needs
@@ -85,6 +94,67 @@ describe('ReactCoroutine', () => {
8594
['Continuation', true],
8695
['Continuation', false],
8796
]);
97+
expect(ReactNoop.getChildren()).toEqual([
98+
div(
99+
span('foo==bar'),
100+
span('foo!=bar'),
101+
),
102+
]);
103+
});
104+
105+
it('should update a coroutine', () => {
106+
function Continuation({ isSame }) {
107+
return <span prop={isSame ? 'foo==bar' : 'foo!=bar'} />;
108+
}
109+
110+
function Child({ bar }) {
111+
return ReactCoroutine.createYield({
112+
props: {
113+
bar: bar,
114+
},
115+
continuation: Continuation,
116+
});
117+
}
118+
119+
function Indirection() {
120+
return [<Child bar={true} />, <Child bar={false} />];
121+
}
122+
123+
function HandleYields(props, yields) {
124+
return yields.map(y =>
125+
<y.continuation isSame={props.foo === y.props.bar} />
126+
);
127+
}
128+
129+
function Parent(props) {
130+
return ReactCoroutine.createCoroutine(
131+
props.children,
132+
HandleYields,
133+
props
134+
);
135+
}
136+
137+
function App(props) {
138+
return <div><Parent foo={props.foo}><Indirection /></Parent></div>;
139+
}
140+
141+
ReactNoop.render(<App foo={true} />);
142+
ReactNoop.flush();
143+
expect(ReactNoop.getChildren()).toEqual([
144+
div(
145+
span('foo==bar'),
146+
span('foo!=bar'),
147+
),
148+
]);
149+
150+
ReactNoop.render(<App foo={false} />);
151+
ReactNoop.flush();
152+
expect(ReactNoop.getChildren()).toEqual([
153+
div(
154+
span('foo!=bar'),
155+
span('foo==bar'),
156+
),
157+
]);
88158
});
89159

90160
it('should unmount a composite in a coroutine', () => {

0 commit comments

Comments
 (0)