|
| 1 | +function runTest(listItem, callback) { |
| 2 | + try { |
| 3 | + callback(); |
| 4 | + listItem.className = 'correct'; |
| 5 | + listItem.setAttribute('data-value', 'All checks pass'); |
| 6 | + } catch (error) { |
| 7 | + listItem.className = 'incorrect'; |
| 8 | + listItem.setAttribute('data-value', error); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +function runAllTests() { |
| 13 | + try { |
| 14 | + checkSchedulerAPI(); |
| 15 | + } finally { |
| 16 | + try { |
| 17 | + checkSchedulerTrackingAPI(); |
| 18 | + } finally { |
| 19 | + try { |
| 20 | + checkSchedulerTrackingSubscriptionsAPI(); |
| 21 | + } finally { |
| 22 | + checkEndToEndIntegration(); |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function checkSchedulerAPI() { |
| 29 | + runTest(document.getElementById('checkSchedulerAPI'), () => { |
| 30 | + if ( |
| 31 | + typeof ReactScheduler === 'undefined' || |
| 32 | + typeof ReactScheduler.unstable_now !== 'function' || |
| 33 | + typeof ReactScheduler.unstable_scheduleWork !== 'function' || |
| 34 | + typeof ReactScheduler.unstable_cancelScheduledWork !== 'function' |
| 35 | + ) { |
| 36 | + throw 'API is not defined'; |
| 37 | + } |
| 38 | + |
| 39 | + if (ReactScheduler.unstable_now() !== performance.now()) { |
| 40 | + throw 'API does not work'; |
| 41 | + } |
| 42 | + |
| 43 | + // There is no real way to verify that the two APIs are connected. |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function checkSchedulerTrackingAPI() { |
| 48 | + runTest(document.getElementById('checkSchedulerTrackingAPI'), () => { |
| 49 | + if ( |
| 50 | + typeof ReactSchedulerTracking === 'undefined' || |
| 51 | + typeof ReactSchedulerTracking.__getInteractionsRef !== 'function' || |
| 52 | + typeof ReactSchedulerTracking.__getSubscriberRef !== 'function' || |
| 53 | + typeof ReactSchedulerTracking.unstable_clear !== 'function' || |
| 54 | + typeof ReactSchedulerTracking.unstable_getCurrent !== 'function' || |
| 55 | + typeof ReactSchedulerTracking.unstable_getThreadID !== 'function' || |
| 56 | + typeof ReactSchedulerTracking.unstable_track !== 'function' || |
| 57 | + typeof ReactSchedulerTracking.unstable_wrap !== 'function' |
| 58 | + ) { |
| 59 | + throw 'API is not defined'; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + let interactionsSet; |
| 64 | + ReactSchedulerTracking.unstable_track('test', 123, () => { |
| 65 | + interactionsSet = ReactSchedulerTracking.__getInteractionsRef().current; |
| 66 | + }); |
| 67 | + if (interactionsSet.size !== 1) { |
| 68 | + throw null; |
| 69 | + } |
| 70 | + const interaction = Array.from(interactionsSet)[0]; |
| 71 | + if (interaction.name !== 'test' || interaction.timestamp !== 123) { |
| 72 | + throw null; |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + throw 'API does not work'; |
| 76 | + } |
| 77 | + |
| 78 | + const ForwardedSchedulerTracking = |
| 79 | + React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED |
| 80 | + .SchedulerTracking; |
| 81 | + |
| 82 | + if ( |
| 83 | + ReactSchedulerTracking.unstable_getThreadID() === |
| 84 | + ForwardedSchedulerTracking.unstable_getThreadID() |
| 85 | + ) { |
| 86 | + throw 'API forwarding is broken'; |
| 87 | + } |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function checkSchedulerTrackingSubscriptionsAPI() { |
| 92 | + runTest( |
| 93 | + document.getElementById('checkSchedulerTrackingSubscriptionsAPI'), |
| 94 | + () => { |
| 95 | + if ( |
| 96 | + typeof ReactSchedulerTrackingSubscriptions === 'undefined' || |
| 97 | + typeof ReactSchedulerTrackingSubscriptions.unstable_subscribe !== |
| 98 | + 'function' || |
| 99 | + typeof ReactSchedulerTrackingSubscriptions.unstable_unsubscribe !== |
| 100 | + 'function' |
| 101 | + ) { |
| 102 | + throw 'API is not defined'; |
| 103 | + } |
| 104 | + |
| 105 | + const onInteractionScheduledWorkCompletedCalls = []; |
| 106 | + const onInteractionTrackedCalls = []; |
| 107 | + const onWorkCanceledCalls = []; |
| 108 | + const onWorkScheduledCalls = []; |
| 109 | + const onWorkStartedCalls = []; |
| 110 | + const onWorkStoppedCalls = []; |
| 111 | + const subscriber = { |
| 112 | + onInteractionScheduledWorkCompleted: (...args) => |
| 113 | + onInteractionScheduledWorkCompletedCalls.push(args), |
| 114 | + onInteractionTracked: (...args) => onInteractionTrackedCalls.push(args), |
| 115 | + onWorkCanceled: (...args) => onWorkCanceledCalls.push(args), |
| 116 | + onWorkScheduled: (...args) => onWorkScheduledCalls.push(args), |
| 117 | + onWorkStarted: (...args) => onWorkStartedCalls.push(args), |
| 118 | + onWorkStopped: (...args) => onWorkStoppedCalls.push(args), |
| 119 | + }; |
| 120 | + |
| 121 | + try { |
| 122 | + ReactSchedulerTrackingSubscriptions.unstable_subscribe(subscriber); |
| 123 | + ReactSchedulerTracking.unstable_track('foo', 123, () => {}); |
| 124 | + ReactSchedulerTrackingSubscriptions.unstable_unsubscribe(subscriber); |
| 125 | + if (onInteractionTrackedCalls.length !== 1) { |
| 126 | + throw null; |
| 127 | + } |
| 128 | + const interaction = onInteractionTrackedCalls[0][0]; |
| 129 | + if (interaction.name !== 'foo' || interaction.timestamp !== 123) { |
| 130 | + throw null; |
| 131 | + } |
| 132 | + ReactSchedulerTracking.unstable_track('bar', 456, () => {}); |
| 133 | + if (onInteractionTrackedCalls.length !== 1) { |
| 134 | + throw null; |
| 135 | + } |
| 136 | + } catch (error) { |
| 137 | + throw 'API does not forward methods'; |
| 138 | + } |
| 139 | + |
| 140 | + const ForwardedSchedulerTrackingSubscriptions = |
| 141 | + React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED |
| 142 | + .SchedulerTrackingSubscriptions; |
| 143 | + const ForwardedSchedulerTracking = |
| 144 | + React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED |
| 145 | + .SchedulerTracking; |
| 146 | + |
| 147 | + try { |
| 148 | + ForwardedSchedulerTrackingSubscriptions.unstable_subscribe(subscriber); |
| 149 | + ReactSchedulerTracking.unstable_track('foo', 123, () => {}); |
| 150 | + ForwardedSchedulerTracking.unstable_track('bar', 456, () => {}); |
| 151 | + ReactSchedulerTrackingSubscriptions.unstable_unsubscribe(subscriber); |
| 152 | + if (onInteractionTrackedCalls.length !== 3) { |
| 153 | + throw null; |
| 154 | + } |
| 155 | + const interactionFoo = onInteractionTrackedCalls[1][0]; |
| 156 | + const interactionBar = onInteractionTrackedCalls[2][0]; |
| 157 | + if ( |
| 158 | + interactionFoo.name !== 'foo' || |
| 159 | + interactionFoo.timestamp !== 123 || |
| 160 | + interactionBar.name !== 'bar' || |
| 161 | + interactionBar.timestamp !== 456 |
| 162 | + ) { |
| 163 | + throw null; |
| 164 | + } |
| 165 | + ForwardedSchedulerTracking.unstable_track('baz', 789, () => {}); |
| 166 | + if (onInteractionTrackedCalls.length !== 3) { |
| 167 | + throw null; |
| 168 | + } |
| 169 | + } catch (error) { |
| 170 | + throw 'API forwarding is broken'; |
| 171 | + } |
| 172 | + } |
| 173 | + ); |
| 174 | +} |
| 175 | + |
| 176 | +function checkEndToEndIntegration() { |
| 177 | + runTest(document.getElementById('checkEndToEndIntegration'), () => { |
| 178 | + try { |
| 179 | + const onRenderCalls = []; |
| 180 | + const onRender = (...args) => onRenderCalls.push(args); |
| 181 | + const container = document.createElement('div'); |
| 182 | + |
| 183 | + ReactSchedulerTracking.unstable_track('render', 123, () => { |
| 184 | + ReactDOM.render( |
| 185 | + React.createElement( |
| 186 | + React.unstable_Profiler, |
| 187 | + {id: 'profiler', onRender}, |
| 188 | + React.createElement('div', null, 'hi') |
| 189 | + ), |
| 190 | + container |
| 191 | + ); |
| 192 | + }); |
| 193 | + |
| 194 | + if (container.textContent !== 'hi') { |
| 195 | + throw null; |
| 196 | + } |
| 197 | + |
| 198 | + if (onRenderCalls.length !== 1) { |
| 199 | + throw null; |
| 200 | + } |
| 201 | + const call = onRenderCalls[0]; |
| 202 | + if (call.length !== 7) { |
| 203 | + throw null; |
| 204 | + } |
| 205 | + const interaction = Array.from(call[6])[0]; |
| 206 | + if (interaction.name !== 'render' || interaction.timestamp !== 123) { |
| 207 | + throw null; |
| 208 | + } |
| 209 | + } catch (error) { |
| 210 | + throw 'End to end integration is broken'; |
| 211 | + } |
| 212 | + }); |
| 213 | +} |
0 commit comments