Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "unstable_" prefix to react-cache and jest-react #13929

Merged
merged 3 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React, {Fragment} from 'react';
import {createResource} from 'react-cache';
import {unstable_createResource} from 'react-cache';
import {cache} from '../cache';
import Spinner from './Spinner';
import {fetchCoreContributorListJSON} from '../api';

const ContributorListResource = createResource(fetchCoreContributorListJSON);
const ContributorListResource = unstable_createResource(
fetchCoreContributorListJSON
);

const ContributorListPage = ({loadingId, onUserClick}) => (
<Fragment>
Expand Down
10 changes: 6 additions & 4 deletions fixtures/unstable-async/suspense/src/components/UserPage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Suspense} from 'react';
import {createResource} from 'react-cache';
import {unstable_createResource} from 'react-cache';
import Spinner from './Spinner';
import {cache} from '../cache';
import {fetchUserProfileJSON, fetchUserRepositoriesListJSON} from '../api';
Expand All @@ -21,7 +21,7 @@ export default function UserPage({id}) {
);
}

const UserDetailsResource = createResource(fetchUserProfileJSON);
const UserDetailsResource = unstable_createResource(fetchUserProfileJSON);

function UserDetails({id}) {
const user = UserDetailsResource.read(cache, id);
Expand Down Expand Up @@ -103,7 +103,7 @@ const Email = ({email}) => (
</div>
);

const ImageResource = createResource(
const ImageResource = unstable_createResource(
src =>
new Promise(resolve => {
const img = new Image();
Expand Down Expand Up @@ -132,7 +132,9 @@ function UserPicture({source}) {
);
}

const UserRepositoriesResource = createResource(fetchUserRepositoriesListJSON);
const UserRepositoriesResource = unstable_createResource(
fetchUserRepositoriesListJSON
);

function Repositories({id}) {
const repos = UserRepositoriesResource.read(cache, id);
Expand Down
20 changes: 10 additions & 10 deletions packages/jest-react/src/JestReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ function assertYieldsWereCleared(root) {
invariant(
actualYields.length === 0,
'Log of yielded values is not empty. ' +
'Call expect(ReactTestRenderer).toHaveYielded(...) first.',
'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.',
);
}

export function toFlushAndYield(root, expectedYields) {
export function unstable_toFlushAndYield(root, expectedYields) {
assertYieldsWereCleared(root);
const actualYields = root.unstable_flushAll();
return captureAssertion(() => {
expect(actualYields).toEqual(expectedYields);
});
}

export function toFlushAndYieldThrough(root, expectedYields) {
export function unstable_toFlushAndYieldThrough(root, expectedYields) {
assertYieldsWereCleared(root);
const actualYields = root.unstable_flushNumberOfYields(expectedYields.length);
return captureAssertion(() => {
expect(actualYields).toEqual(expectedYields);
});
}

export function toFlushWithoutYielding(root) {
return toFlushAndYield(root, []);
export function unstable_toFlushWithoutYielding(root) {
return unstable_toFlushAndYield(root, []);
}

export function toHaveYielded(ReactTestRenderer, expectedYields) {
export function unstable_toHaveYielded(ReactTestRenderer, expectedYields) {
return captureAssertion(() => {
if (
ReactTestRenderer === null ||
Expand All @@ -63,17 +63,17 @@ export function toHaveYielded(ReactTestRenderer, expectedYields) {
) {
invariant(
false,
'The matcher `toHaveYielded` expects an instance of React Test ' +
'The matcher `unstable_toHaveYielded` expects an instance of React Test ' +
'Renderer.\n\nTry: ' +
'expect(ReactTestRenderer).toHaveYielded(expectedYields)',
'expect(ReactTestRenderer).unstable_toHaveYielded(expectedYields)',
);
}
const actualYields = ReactTestRenderer.unstable_clearYields();
expect(actualYields).toEqual(expectedYields);
});
}

export function toFlushAndThrow(root, ...rest) {
export function unstable_toFlushAndThrow(root, ...rest) {
assertYieldsWereCleared(root);
return captureAssertion(() => {
expect(() => {
Expand All @@ -82,7 +82,7 @@ export function toFlushAndThrow(root, ...rest) {
});
}

export function toMatchRenderedOutput(root, expectedJSX) {
export function unstable_toMatchRenderedOutput(root, expectedJSX) {
assertYieldsWereCleared(root);
const actualJSON = root.toJSON();

Expand Down
8 changes: 4 additions & 4 deletions packages/react-cache/src/ReactCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ if (__DEV__) {
'%s: Invalid key type. Expected a string, number, symbol, or boolean, ' +
'but instead received: %s' +
'\n\nTo use non-primitive values as keys, you must pass a hash ' +
'function as the second argument to createResource().',
'function as the second argument to unstable_createResource().',
methodName,
key,
);
Expand All @@ -341,20 +341,20 @@ type Resource<K, V> = {|
// were a more elegant way to do this in the function definition itself.

// Primitive keys do not request a hash function.
declare function createResource<V, K: primitive, H: primitive>(
declare function unstable_createResource<V, K: primitive, H: primitive>(
loadResource: (K) => Promise<V>,
hash?: (K) => H,
): Resource<K, V>;

// Non-primitive keys *do* require a hash function.
// eslint-disable-next-line no-redeclare
declare function createResource<V, K: mixed, H: primitive>(
declare function unstable_createResource<V, K: mixed, H: primitive>(
loadResource: (K) => Promise<V>,
hash: (K) => H,
): Resource<K, V>;

// eslint-disable-next-line no-redeclare
export function createResource<V, K, H: primitive>(
export function unstable_createResource<V, K, H: primitive>(
loadResource: K => Promise<V>,
hash: K => H,
): Resource<K, V> {
Expand Down
30 changes: 15 additions & 15 deletions packages/react-cache/src/__tests__/ReactCache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ describe('ReactCache', () => {
});

it('throws a promise if the requested value is not in the cache', async () => {
const {createCache, createResource} = ReactCache;
const {createCache, unstable_createResource} = ReactCache;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createCache isn't unstable but createResource is? Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Andrew has concerns that createResource is less stable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok just wanted to make sure it's intentional

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createCache doesn't exist anymore after I merge this: #13865

Let's land this PR as-is then I'll land mine.


function loadUpperCase(text) {
return Promise.resolve(text.toUpperCase());
}
const UpperCase = createResource(loadUpperCase);
const UpperCase = unstable_createResource(loadUpperCase);
const cache = createCache();

let suspender;
Expand All @@ -39,7 +39,7 @@ describe('ReactCache', () => {
});

it('throws an error on the subsequent read if the promise is rejected', async () => {
const {createCache, createResource} = ReactCache;
const {createCache, unstable_createResource} = ReactCache;

let shouldFail = true;
function loadUpperCase(text) {
Expand All @@ -52,7 +52,7 @@ describe('ReactCache', () => {
return Promise.resolve(text.toUpperCase());
}
}
const UpperCase = createResource(loadUpperCase);
const UpperCase = unstable_createResource(loadUpperCase);
const cache = createCache();

let suspender;
Expand Down Expand Up @@ -83,12 +83,12 @@ describe('ReactCache', () => {
});

it('can preload data ahead of time', async () => {
const {createCache, createResource} = ReactCache;
const {createCache, unstable_createResource} = ReactCache;

function loadUpperCase(text) {
return Promise.resolve(text.toUpperCase());
}
const UpperCase = createResource(loadUpperCase);
const UpperCase = unstable_createResource(loadUpperCase);
const cache = createCache();

UpperCase.preload(cache, 'hello');
Expand All @@ -99,12 +99,12 @@ describe('ReactCache', () => {
});

it('does not throw if preloaded promise rejects', async () => {
const {createCache, createResource} = ReactCache;
const {createCache, unstable_createResource} = ReactCache;

function loadUpperCase(text) {
return Promise.reject(new Error('uh oh'));
}
const UpperCase = createResource(loadUpperCase);
const UpperCase = unstable_createResource(loadUpperCase);
const cache = createCache();

UpperCase.preload(cache, 'hello');
Expand All @@ -115,15 +115,15 @@ describe('ReactCache', () => {
});

it('accepts custom hash function', async () => {
const {createCache, createResource} = ReactCache;
const {createCache, unstable_createResource} = ReactCache;

function loadSum([a, b]) {
return Promise.resolve(a + b);
}
function hash([a, b]) {
return `${a + b}`;
}
const Sum = createResource(loadSum, hash);
const Sum = unstable_createResource(loadSum, hash);
const cache = createCache();

Sum.preload(cache, [5, 5]);
Expand Down Expand Up @@ -166,15 +166,15 @@ describe('ReactCache', () => {
});

it('warns if non-primitive key is passed to a resource without a hash function', () => {
const {createCache, createResource} = ReactCache;
const {createCache, unstable_createResource} = ReactCache;

spyOnDev(console, 'error');

function loadSum([a, b]) {
return Promise.resolve(a + b);
}

const Sum = createResource(loadSum);
const Sum = unstable_createResource(loadSum);
const cache = createCache();

function fn() {
Expand All @@ -187,7 +187,7 @@ describe('ReactCache', () => {
'preload: Invalid key type. Expected a string, number, symbol, or ' +
'boolean, but instead received: 5,5\n\n' +
'To use non-primitive values as keys, you must pass a hash ' +
'function as the second argument to createResource().',
'function as the second argument to unstable_createResource().',
],
{withoutStack: true},
);
Expand All @@ -197,12 +197,12 @@ describe('ReactCache', () => {
});

it('stays within maximum capacity by evicting the least recently used record', async () => {
const {createCache, createResource} = ReactCache;
const {createCache, unstable_createResource} = ReactCache;

function loadIntegerString(int) {
return Promise.resolve(int + '');
}
const IntegerStringResource = createResource(loadIntegerString);
const IntegerStringResource = unstable_createResource(loadIntegerString);
const cache = createCache();

// TODO: This is hard-coded to a maximum size of 500. Make this configurable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('ReactDOMSuspensePlaceholder', () => {
cache = ReactCache.createCache(invalidateCache);
}
invalidateCache();
TextResource = ReactCache.createResource(([text, ms = 0]) => {
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
return new Promise((resolve, reject) =>
setTimeout(() => {
resolve(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ describe('ErrorBoundaryReconciliation', () => {
if (isConcurrent) {
renderer.unstable_flushAll();
}
expect(renderer).toMatchRenderedOutput(<span prop="BrokenRender" />);
expect(renderer).unstable_toMatchRenderedOutput(
<span prop="BrokenRender" />,
);

expect(() => {
renderer.update(
Expand All @@ -72,7 +74,9 @@ describe('ErrorBoundaryReconciliation', () => {
}
}).toWarnDev(isConcurrent ? ['invalid', 'invalid'] : ['invalid']);
const Fallback = fallbackTagName;
expect(renderer).toMatchRenderedOutput(<Fallback prop="ErrorBoundary" />);
expect(renderer).unstable_toMatchRenderedOutput(
<Fallback prop="ErrorBoundary" />,
);
}

describe(isConcurrent ? 'concurrent' : 'sync', () => {
Expand Down
Loading