Skip to content

Commit cd24ce7

Browse files
committed
Fix tests that only notify most recent listener
These tests were written in such a way that when a wakeable has multiple listeners, only the most recent listener is notified. I switched them to use a promise instead of mutating a ref.
1 parent 333deb7 commit cd24ce7

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

packages/react-reconciler/src/__tests__/ReactSuspenseCallback-test.internal.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,20 @@ describe('ReactSuspense', () => {
3131

3232
function createThenable() {
3333
let completed = false;
34-
const resolveRef = {current: null};
35-
const promise = {
36-
then(resolve, reject) {
37-
resolveRef.current = () => {
38-
completed = true;
39-
resolve();
40-
};
41-
},
42-
};
43-
34+
let resolve;
35+
const promise = new Promise(res => {
36+
resolve = () => {
37+
completed = true;
38+
res();
39+
};
40+
});
4441
const PromiseComp = () => {
4542
if (!completed) {
4643
throw promise;
4744
}
4845
return 'Done';
4946
};
50-
return {promise, resolveRef, PromiseComp};
47+
return {promise, resolve, PromiseComp};
5148
}
5249

5350
it('check type', () => {
@@ -74,8 +71,8 @@ describe('ReactSuspense', () => {
7471
expect(() => Scheduler.unstable_flushAll()).toErrorDev([]);
7572
});
7673

77-
it('1 then 0 suspense callback', () => {
78-
const {promise, resolveRef, PromiseComp} = createThenable();
74+
it('1 then 0 suspense callback', async () => {
75+
const {promise, resolve, PromiseComp} = createThenable();
7976

8077
let ops = [];
8178
const suspenseCallback = thenables => {
@@ -94,21 +91,21 @@ describe('ReactSuspense', () => {
9491
expect(ops).toEqual([new Set([promise])]);
9592
ops = [];
9693

97-
resolveRef.current();
94+
await resolve();
9895
expect(Scheduler).toFlushWithoutYielding();
9996
expect(ReactNoop.getChildren()).toEqual([text('Done')]);
10097
expect(ops).toEqual([]);
10198
});
10299

103-
it('2 then 1 then 0 suspense callback', () => {
100+
it('2 then 1 then 0 suspense callback', async () => {
104101
const {
105102
promise: promise1,
106-
resolveRef: resolveRef1,
103+
resolve: resolve1,
107104
PromiseComp: PromiseComp1,
108105
} = createThenable();
109106
const {
110107
promise: promise2,
111-
resolveRef: resolveRef2,
108+
resolve: resolve2,
112109
PromiseComp: PromiseComp2,
113110
} = createThenable();
114111

@@ -132,14 +129,14 @@ describe('ReactSuspense', () => {
132129
expect(ops).toEqual([new Set([promise1, promise2])]);
133130
ops = [];
134131

135-
resolveRef1.current();
132+
await resolve1();
136133
ReactNoop.render(element);
137134
expect(Scheduler).toFlushWithoutYielding();
138135
expect(ReactNoop.getChildren()).toEqual([text('Waiting Tier 1')]);
139136
expect(ops).toEqual([new Set([promise2])]);
140137
ops = [];
141138

142-
resolveRef2.current();
139+
await resolve2();
143140
ReactNoop.render(element);
144141
expect(Scheduler).toFlushWithoutYielding();
145142
expect(ReactNoop.getChildren()).toEqual([text('Done'), text('Done')]);
@@ -177,15 +174,15 @@ describe('ReactSuspense', () => {
177174
expect(ops2).toEqual([new Set([promise])]);
178175
});
179176

180-
it('competing suspense promises', () => {
177+
it('competing suspense promises', async () => {
181178
const {
182179
promise: promise1,
183-
resolveRef: resolveRef1,
180+
resolve: resolve1,
184181
PromiseComp: PromiseComp1,
185182
} = createThenable();
186183
const {
187184
promise: promise2,
188-
resolveRef: resolveRef2,
185+
resolve: resolve2,
189186
PromiseComp: PromiseComp2,
190187
} = createThenable();
191188

@@ -219,7 +216,7 @@ describe('ReactSuspense', () => {
219216
ops1 = [];
220217
ops2 = [];
221218

222-
resolveRef1.current();
219+
await resolve1();
223220
ReactNoop.render(element);
224221
expect(Scheduler).toFlushWithoutYielding();
225222
expect(ReactNoop.getChildren()).toEqual([
@@ -231,7 +228,7 @@ describe('ReactSuspense', () => {
231228
ops1 = [];
232229
ops2 = [];
233230

234-
resolveRef2.current();
231+
await resolve2();
235232
ReactNoop.render(element);
236233
expect(Scheduler).toFlushWithoutYielding();
237234
expect(ReactNoop.getChildren()).toEqual([text('Done'), text('Done')]);

0 commit comments

Comments
 (0)