|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -// TODO: Move `internalAct` and other test helpers to this package |
| 8 | +// TODO: Move `internalAct` and other test helpers to this package, too |
| 9 | + |
| 10 | +import * as SchedulerMock from 'scheduler/unstable_mock'; |
| 11 | +import {diff} from 'jest-diff'; |
| 12 | +import {equals} from '@jest/expect-utils'; |
| 13 | + |
| 14 | +function assertYieldsWereCleared(Scheduler) { |
| 15 | + const actualYields = Scheduler.unstable_clearYields(); |
| 16 | + if (actualYields.length !== 0) { |
| 17 | + const error = Error( |
| 18 | + 'Log of yielded values is not empty. ' + |
| 19 | + 'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.', |
| 20 | + ); |
| 21 | + Error.captureStackTrace(error, assertYieldsWereCleared); |
| 22 | + throw error; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +export async function waitFor(expectedLog) { |
| 27 | + assertYieldsWereCleared(SchedulerMock); |
| 28 | + |
| 29 | + // Create the error object before doing any async work, to get a better |
| 30 | + // stack trace. |
| 31 | + const error = new Error(); |
| 32 | + Error.captureStackTrace(error, waitFor); |
| 33 | + |
| 34 | + const actualLog = []; |
| 35 | + do { |
| 36 | + // Wait until end of current task/microtask. |
| 37 | + await null; |
| 38 | + if (SchedulerMock.unstable_hasPendingWork()) { |
| 39 | + SchedulerMock.unstable_flushNumberOfYields( |
| 40 | + expectedLog.length - actualLog.length, |
| 41 | + ); |
| 42 | + actualLog.push(...SchedulerMock.unstable_clearYields()); |
| 43 | + if (expectedLog.length > actualLog.length) { |
| 44 | + // Continue flushing until we've logged the expected number of items. |
| 45 | + } else { |
| 46 | + // Once we've reached the expected sequence, wait one more microtask to |
| 47 | + // flush any remaining synchronous work. |
| 48 | + await null; |
| 49 | + actualLog.push(...SchedulerMock.unstable_clearYields()); |
| 50 | + break; |
| 51 | + } |
| 52 | + } else { |
| 53 | + // There's no pending work, even after a microtask. |
| 54 | + break; |
| 55 | + } |
| 56 | + } while (true); |
| 57 | + |
| 58 | + if (equals(actualLog, expectedLog)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + error.message = ` |
| 63 | +Expected sequence of events did not occur. |
| 64 | +
|
| 65 | +${diff(expectedLog, actualLog)} |
| 66 | +`; |
| 67 | + throw error; |
| 68 | +} |
| 69 | + |
| 70 | +export async function waitForAll(expectedLog) { |
| 71 | + assertYieldsWereCleared(SchedulerMock); |
| 72 | + |
| 73 | + // Create the error object before doing any async work, to get a better |
| 74 | + // stack trace. |
| 75 | + const error = new Error(); |
| 76 | + Error.captureStackTrace(error, waitFor); |
| 77 | + |
| 78 | + do { |
| 79 | + // Wait until end of current task/microtask. |
| 80 | + await null; |
| 81 | + if (!SchedulerMock.unstable_hasPendingWork()) { |
| 82 | + // There's no pending work, even after a microtask. Stop flushing. |
| 83 | + break; |
| 84 | + } |
| 85 | + SchedulerMock.unstable_flushAllWithoutAsserting(); |
| 86 | + } while (true); |
| 87 | + |
| 88 | + const actualLog = SchedulerMock.unstable_clearYields(); |
| 89 | + if (equals(actualLog, expectedLog)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + error.message = ` |
| 94 | +Expected sequence of events did not occur. |
| 95 | +
|
| 96 | +${diff(expectedLog, actualLog)} |
| 97 | +`; |
| 98 | + throw error; |
| 99 | +} |
| 100 | + |
| 101 | +// TODO: This name is a bit misleading currently because it will stop as soon as |
| 102 | +// React yields for any reason, not just for a paint. I've left it this way for |
| 103 | +// now because that's how untable_flushUntilNextPaint already worked, but maybe |
| 104 | +// we should split these use cases into separate APIs. |
| 105 | +export async function waitForPaint(expectedLog) { |
| 106 | + assertYieldsWereCleared(SchedulerMock); |
| 107 | + |
| 108 | + // Create the error object before doing any async work, to get a better |
| 109 | + // stack trace. |
| 110 | + const error = new Error(); |
| 111 | + Error.captureStackTrace(error, waitFor); |
| 112 | + |
| 113 | + // Wait until end of current task/microtask. |
| 114 | + await null; |
| 115 | + if (SchedulerMock.unstable_hasPendingWork()) { |
| 116 | + // Flush until React yields. |
| 117 | + SchedulerMock.unstable_flushUntilNextPaint(); |
| 118 | + // Wait one more microtask to flush any remaining synchronous work. |
| 119 | + await null; |
| 120 | + } |
| 121 | + |
| 122 | + const actualLog = SchedulerMock.unstable_clearYields(); |
| 123 | + if (equals(actualLog, expectedLog)) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + error.message = ` |
| 128 | +Expected sequence of events did not occur. |
| 129 | +
|
| 130 | +${diff(expectedLog, actualLog)} |
| 131 | +`; |
| 132 | + throw error; |
| 133 | +} |
0 commit comments