Skip to content

Commit

Permalink
Fixed inline bundling problem and added more fixture tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Aug 31, 2018
1 parent 70dfb61 commit c8d5899
Show file tree
Hide file tree
Showing 14 changed files with 476 additions and 237 deletions.
128 changes: 33 additions & 95 deletions fixtures/tracking/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@
<meta charset="utf-8">
<title>Test tracking UMD</title>
<style>
body {
font-family: sans-serif;
}
ol {
display: inline-flex;
flex-direction: column;
align-items: flex-start;
}
li {
background-color: #F7F7F7;
border: solid #CCC 0.125rem;
margin-bottom: 0.5rem;
border-radius: 0.25rem;
padding: 0.25rem;
padding: 0.5rem;
}
li:after {
content: attr(data-value);
margin-left: 0.25rem;
}
.correct {
border-color: #0C0;
border-style: solid;
background: #EFE;
}
.incorrect {
border-color: #F00;
border-style: dashed;
background-color: #FEE;
}
</style>
</head>
Expand All @@ -29,107 +41,33 @@ <h1>Test tracking UMD</h1>
This fixture tests that the new tracking API is accessible via UMD build using the UMD shim.
It does not exhaustively test API functionality, only that the forwarded methods can be called.
</p>
<h2>Tests:</h2>
<p>
Before running the tests below, check the console to make sure there are no errors.
</p>
<h3>
Tests
<button onClick="runAllTests()">Run all tests</button>
</h3>
<ol>
<li id="checkSchedulerAPI">
<button onClick="checkSchedulerAPI()">Check scheduler API</button>
<li id="checkSchedulerAPI" data-value="...">
<strong>Test scheduler API</strong>
</li>
<li id="checkSchedulerTrackingAPI" data-value="...">
<strong>Test tracking API</strong>
</li>
<li id="checkSchedulerTrackingAPI">
<button onClick="checkSchedulerTrackingAPI()">Check tracking API</button>
<li id="checkSchedulerTrackingSubscriptionsAPI" data-value="...">
<strong>Test tracking subscriptions API</strong>
</li>
<li id="checkSchedulerTrackingSubscriptionsAPI">
<button onClick="checkSchedulerTrackingSubscriptionsAPI()">Check tracking subscriptions API</button>
<li id="checkEndToEndIntegration" data-value="...">
<strong>Test end-to-end integration</strong>
</li>
</ol>
<!-- Load the tracking API before react to test that it's lazily evaluated -->
<script src="../../build/node_modules/react-scheduler/umd/react-scheduler.development.js"></script>
<script src="../../build/node_modules/react-scheduler/umd/react-scheduler-tracking.development.js"></script>
<script src="../../build/node_modules/react-scheduler/umd/react-scheduler-tracking-subscriptions.development.js"></script>
<script src="../../build/node_modules/react-scheduler/umd/react-scheduler.js"></script>
<script src="../../build/node_modules/react-scheduler/umd/react-scheduler-tracking.js"></script>
<script src="../../build/node_modules/react-scheduler/umd/react-scheduler-tracking-subscriptions.js"></script>
<script src="../../build/node_modules/react/umd/react.development.js"></script>
<script src="../../build/node_modules/react-dom/umd/react-dom.development.js"></script>
<script>
function runTest(listItem, callback) {
try {
callback();
listItem.className = "correct";
listItem.setAttribute("data-value", "All checks pass");
} catch (error) {
listItem.className = "incorrect";
listItem.setAttribute("data-value", error.message);
}
}

function checkSchedulerAPI() {
runTest(
document.getElementById('checkSchedulerAPI'),
() => {
if (
typeof ReactScheduler.unstable_now !== 'function' ||
typeof ReactScheduler.unstable_scheduleWork !== 'function' ||
typeof ReactScheduler.unstable_cancelScheduledWork !== 'function'
) {
throw "API is not defined";
}

if (ReactScheduler.unstable_now() !== performance.now()) {
throw "API does not work";
}
});
}

function checkSchedulerTrackingAPI() {
runTest(
document.getElementById('checkSchedulerTrackingAPI'),
() => {
if (
typeof ReactSchedulerTracking.__getInteractionsRef !== 'function' ||
typeof ReactSchedulerTracking.__getSubscriberRef !== 'function' ||
typeof ReactSchedulerTracking.unstable_clear !== 'function' ||
typeof ReactSchedulerTracking.unstable_getCurrent !== 'function' ||
typeof ReactSchedulerTracking.unstable_getThreadID !== 'function' ||
typeof ReactSchedulerTracking.unstable_track !== 'function' ||
typeof ReactSchedulerTracking.unstable_wrap !== 'function'
) {
throw "API is not defined";
}

try {
let interactionsSet;
ReactSchedulerTracking.unstable_track('test', 123, () => {
interactionsSet = ReactSchedulerTracking.__getInteractionsRef().current;
});
if (interactionsSet.size !== 1) {
throw null;
}
const interaction = Array.from(interactionsSet)[0];
if (interaction.name !== 'test' || interaction.timestamp !== 123) {
throw null;
}
} catch (error) {
throw "API does not work";
}
});
}

function checkSchedulerTrackingSubscriptionsAPI() {
runTest(
document.getElementById('checkSchedulerTrackingSubscriptionsAPI'),
() => {
if (
typeof ReactSchedulerTrackingSubscriptions.unstable_subscribe !== 'function' ||
typeof ReactSchedulerTrackingSubscriptions.unstable_unsubscribe !== 'function'
) {
throw "API is not defined";
}

try {
ReactSchedulerTrackingSubscriptions.unstable_subscribe({});
ReactSchedulerTrackingSubscriptions.unstable_unsubscribe({});
} catch (error) {
throw "API does not forward methods";
}
});
}
</script>
<script src="./script.js"></script>
</body>
</html>
213 changes: 213 additions & 0 deletions fixtures/tracking/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
function runTest(listItem, callback) {
try {
callback();
listItem.className = 'correct';
listItem.setAttribute('data-value', 'All checks pass');
} catch (error) {
listItem.className = 'incorrect';
listItem.setAttribute('data-value', error);
}
}

function runAllTests() {
try {
checkSchedulerAPI();
} finally {
try {
checkSchedulerTrackingAPI();
} finally {
try {
checkSchedulerTrackingSubscriptionsAPI();
} finally {
checkEndToEndIntegration();
}
}
}
}

function checkSchedulerAPI() {
runTest(document.getElementById('checkSchedulerAPI'), () => {
if (
typeof ReactScheduler === 'undefined' ||
typeof ReactScheduler.unstable_now !== 'function' ||
typeof ReactScheduler.unstable_scheduleWork !== 'function' ||
typeof ReactScheduler.unstable_cancelScheduledWork !== 'function'
) {
throw 'API is not defined';
}

if (ReactScheduler.unstable_now() !== performance.now()) {
throw 'API does not work';
}

// There is no real way to verify that the two APIs are connected.
});
}

function checkSchedulerTrackingAPI() {
runTest(document.getElementById('checkSchedulerTrackingAPI'), () => {
if (
typeof ReactSchedulerTracking === 'undefined' ||
typeof ReactSchedulerTracking.__getInteractionsRef !== 'function' ||
typeof ReactSchedulerTracking.__getSubscriberRef !== 'function' ||
typeof ReactSchedulerTracking.unstable_clear !== 'function' ||
typeof ReactSchedulerTracking.unstable_getCurrent !== 'function' ||
typeof ReactSchedulerTracking.unstable_getThreadID !== 'function' ||
typeof ReactSchedulerTracking.unstable_track !== 'function' ||
typeof ReactSchedulerTracking.unstable_wrap !== 'function'
) {
throw 'API is not defined';
}

try {
let interactionsSet;
ReactSchedulerTracking.unstable_track('test', 123, () => {
interactionsSet = ReactSchedulerTracking.__getInteractionsRef().current;
});
if (interactionsSet.size !== 1) {
throw null;
}
const interaction = Array.from(interactionsSet)[0];
if (interaction.name !== 'test' || interaction.timestamp !== 123) {
throw null;
}
} catch (error) {
throw 'API does not work';
}

const ForwardedSchedulerTracking =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.SchedulerTracking;

if (
ReactSchedulerTracking.unstable_getThreadID() ===
ForwardedSchedulerTracking.unstable_getThreadID()
) {
throw 'API forwarding is broken';
}
});
}

function checkSchedulerTrackingSubscriptionsAPI() {
runTest(
document.getElementById('checkSchedulerTrackingSubscriptionsAPI'),
() => {
if (
typeof ReactSchedulerTrackingSubscriptions === 'undefined' ||
typeof ReactSchedulerTrackingSubscriptions.unstable_subscribe !==
'function' ||
typeof ReactSchedulerTrackingSubscriptions.unstable_unsubscribe !==
'function'
) {
throw 'API is not defined';
}

const onInteractionScheduledWorkCompletedCalls = [];
const onInteractionTrackedCalls = [];
const onWorkCanceledCalls = [];
const onWorkScheduledCalls = [];
const onWorkStartedCalls = [];
const onWorkStoppedCalls = [];
const subscriber = {
onInteractionScheduledWorkCompleted: (...args) =>
onInteractionScheduledWorkCompletedCalls.push(args),
onInteractionTracked: (...args) => onInteractionTrackedCalls.push(args),
onWorkCanceled: (...args) => onWorkCanceledCalls.push(args),
onWorkScheduled: (...args) => onWorkScheduledCalls.push(args),
onWorkStarted: (...args) => onWorkStartedCalls.push(args),
onWorkStopped: (...args) => onWorkStoppedCalls.push(args),
};

try {
ReactSchedulerTrackingSubscriptions.unstable_subscribe(subscriber);
ReactSchedulerTracking.unstable_track('foo', 123, () => {});
ReactSchedulerTrackingSubscriptions.unstable_unsubscribe(subscriber);
if (onInteractionTrackedCalls.length !== 1) {
throw null;
}
const interaction = onInteractionTrackedCalls[0][0];
if (interaction.name !== 'foo' || interaction.timestamp !== 123) {
throw null;
}
ReactSchedulerTracking.unstable_track('bar', 456, () => {});
if (onInteractionTrackedCalls.length !== 1) {
throw null;
}
} catch (error) {
throw 'API does not forward methods';
}

const ForwardedSchedulerTrackingSubscriptions =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.SchedulerTrackingSubscriptions;
const ForwardedSchedulerTracking =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.SchedulerTracking;

try {
ForwardedSchedulerTrackingSubscriptions.unstable_subscribe(subscriber);
ReactSchedulerTracking.unstable_track('foo', 123, () => {});
ForwardedSchedulerTracking.unstable_track('bar', 456, () => {});
ReactSchedulerTrackingSubscriptions.unstable_unsubscribe(subscriber);
if (onInteractionTrackedCalls.length !== 3) {
throw null;
}
const interactionFoo = onInteractionTrackedCalls[1][0];
const interactionBar = onInteractionTrackedCalls[2][0];
if (
interactionFoo.name !== 'foo' ||
interactionFoo.timestamp !== 123 ||
interactionBar.name !== 'bar' ||
interactionBar.timestamp !== 456
) {
throw null;
}
ForwardedSchedulerTracking.unstable_track('baz', 789, () => {});
if (onInteractionTrackedCalls.length !== 3) {
throw null;
}
} catch (error) {
throw 'API forwarding is broken';
}
}
);
}

function checkEndToEndIntegration() {
runTest(document.getElementById('checkEndToEndIntegration'), () => {
try {
const onRenderCalls = [];
const onRender = (...args) => onRenderCalls.push(args);
const container = document.createElement('div');

ReactSchedulerTracking.unstable_track('render', 123, () => {
ReactDOM.render(
React.createElement(
React.unstable_Profiler,
{id: 'profiler', onRender},
React.createElement('div', null, 'hi')
),
container
);
});

if (container.textContent !== 'hi') {
throw null;
}

if (onRenderCalls.length !== 1) {
throw null;
}
const call = onRenderCalls[0];
if (call.length !== 7) {
throw null;
}
const interaction = Array.from(call[6])[0];
if (interaction.name !== 'render' || interaction.timestamp !== 123) {
throw null;
}
} catch (error) {
throw 'End to end integration is broken';
}
});
}
Loading

0 comments on commit c8d5899

Please sign in to comment.