mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Add "unstable_" prefix to react-cache and jest-react (#13929)
* Add "unstable_" prefix to react-cache createResource and jest-react matchers * Reverted accidental change to error-codes JSON * Remove unstable_ prefix from internal React tests for jest-test
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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';
|
||||
@@ -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);
|
||||
@@ -103,7 +103,7 @@ const Email = ({email}) => (
|
||||
</div>
|
||||
);
|
||||
|
||||
const ImageResource = createResource(
|
||||
const ImageResource = unstable_createResource(
|
||||
src =>
|
||||
new Promise(resolve => {
|
||||
const img = new Image();
|
||||
@@ -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);
|
||||
|
||||
@@ -30,11 +30,11 @@ 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(() => {
|
||||
@@ -42,7 +42,7 @@ export function toFlushAndYield(root, expectedYields) {
|
||||
});
|
||||
}
|
||||
|
||||
export function toFlushAndYieldThrough(root, expectedYields) {
|
||||
export function unstable_toFlushAndYieldThrough(root, expectedYields) {
|
||||
assertYieldsWereCleared(root);
|
||||
const actualYields = root.unstable_flushNumberOfYields(expectedYields.length);
|
||||
return captureAssertion(() => {
|
||||
@@ -50,11 +50,11 @@ export function toFlushAndYieldThrough(root, 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 ||
|
||||
@@ -63,9 +63,9 @@ 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();
|
||||
@@ -73,7 +73,7 @@ export function toHaveYielded(ReactTestRenderer, expectedYields) {
|
||||
});
|
||||
}
|
||||
|
||||
export function toFlushAndThrow(root, ...rest) {
|
||||
export function unstable_toFlushAndThrow(root, ...rest) {
|
||||
assertYieldsWereCleared(root);
|
||||
return captureAssertion(() => {
|
||||
expect(() => {
|
||||
@@ -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();
|
||||
|
||||
|
||||
+4
-4
@@ -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,
|
||||
);
|
||||
@@ -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> {
|
||||
|
||||
+15
-15
@@ -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;
|
||||
|
||||
function loadUpperCase(text) {
|
||||
return Promise.resolve(text.toUpperCase());
|
||||
}
|
||||
const UpperCase = createResource(loadUpperCase);
|
||||
const UpperCase = unstable_createResource(loadUpperCase);
|
||||
const cache = createCache();
|
||||
|
||||
let suspender;
|
||||
@@ -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) {
|
||||
@@ -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;
|
||||
@@ -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');
|
||||
@@ -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');
|
||||
@@ -115,7 +115,7 @@ 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);
|
||||
@@ -123,7 +123,7 @@ describe('ReactCache', () => {
|
||||
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]);
|
||||
@@ -166,7 +166,7 @@ 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');
|
||||
|
||||
@@ -174,7 +174,7 @@ describe('ReactCache', () => {
|
||||
return Promise.resolve(a + b);
|
||||
}
|
||||
|
||||
const Sum = createResource(loadSum);
|
||||
const Sum = unstable_createResource(loadSum);
|
||||
const cache = createCache();
|
||||
|
||||
function fn() {
|
||||
@@ -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},
|
||||
);
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -29,7 +29,7 @@ describe('ReactSuspense', () => {
|
||||
cache = ReactCache.createCache(invalidateCache);
|
||||
}
|
||||
invalidateCache();
|
||||
TextResource = ReactCache.createResource(([text, ms = 0]) => {
|
||||
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
|
||||
let listeners = null;
|
||||
let status = 'pending';
|
||||
let value = null;
|
||||
|
||||
@@ -43,7 +43,7 @@ function runPlaceholderTests(suiteLabel, loadReactNoop) {
|
||||
cache = ReactCache.createCache(invalidateCache);
|
||||
}
|
||||
invalidateCache();
|
||||
TextResource = ReactCache.createResource(([text, ms = 0]) => {
|
||||
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
|
||||
let listeners = null;
|
||||
let status = 'pending';
|
||||
let value = null;
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
|
||||
cache = ReactCache.createCache(invalidateCache);
|
||||
}
|
||||
invalidateCache();
|
||||
TextResource = ReactCache.createResource(([text, ms = 0]) => {
|
||||
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
|
||||
return new Promise((resolve, reject) =>
|
||||
setTimeout(() => {
|
||||
if (textResourceShouldFail) {
|
||||
|
||||
@@ -2176,7 +2176,7 @@ describe('Profiler', () => {
|
||||
|
||||
resourcePromise = null;
|
||||
|
||||
TextResource = ReactCache.createResource(([text, ms = 0]) => {
|
||||
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
|
||||
resourcePromise = new Promise((resolve, reject) =>
|
||||
setTimeout(() => {
|
||||
yieldForRenderer(`Promise resolved [${text}]`);
|
||||
|
||||
@@ -85,7 +85,7 @@ describe('ProfilerDOM', () => {
|
||||
|
||||
resourcePromise = null;
|
||||
|
||||
TextResource = ReactCache.createResource(([text, ms = 0]) => {
|
||||
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
|
||||
resourcePromise = new Promise(
|
||||
SchedulerTracing.unstable_wrap((resolve, reject) => {
|
||||
setTimeout(
|
||||
|
||||
@@ -48,12 +48,12 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
|
||||
...require('./matchers/interactionTracing'),
|
||||
...require('./matchers/toWarnDev'),
|
||||
|
||||
toFlushWithoutYielding: JestReact.toFlushWithoutYielding,
|
||||
toFlushAndYield: JestReact.toFlushAndYield,
|
||||
toFlushAndYieldThrough: JestReact.toFlushAndYieldThrough,
|
||||
toHaveYielded: JestReact.toHaveYielded,
|
||||
toFlushAndThrow: JestReact.toFlushAndThrow,
|
||||
toMatchRenderedOutput: JestReact.toMatchRenderedOutput,
|
||||
toFlushWithoutYielding: JestReact.unstable_toFlushWithoutYielding,
|
||||
toFlushAndYield: JestReact.unstable_toFlushAndYield,
|
||||
toFlushAndYieldThrough: JestReact.unstable_toFlushAndYieldThrough,
|
||||
toHaveYielded: JestReact.unstable_toHaveYielded,
|
||||
toFlushAndThrow: JestReact.unstable_toFlushAndThrow,
|
||||
toMatchRenderedOutput: JestReact.unstable_toMatchRenderedOutput,
|
||||
});
|
||||
|
||||
// We have a Babel transform that inserts guards against infinite loops.
|
||||
|
||||
@@ -50,12 +50,12 @@ expect.extend({
|
||||
...require('../matchers/interactionTracing'),
|
||||
...require('../matchers/toWarnDev'),
|
||||
|
||||
toFlushWithoutYielding: JestReact.toFlushWithoutYielding,
|
||||
toFlushAndYield: JestReact.toFlushAndYield,
|
||||
toFlushAndYieldThrough: JestReact.toFlushAndYieldThrough,
|
||||
toHaveYielded: JestReact.toHaveYielded,
|
||||
toFlushAndThrow: JestReact.toFlushAndThrow,
|
||||
toMatchRenderedOutput: JestReact.toMatchRenderedOutput,
|
||||
toFlushWithoutYielding: JestReact.unstable_toFlushWithoutYielding,
|
||||
toFlushAndYield: JestReact.unstable_toFlushAndYield,
|
||||
toFlushAndYieldThrough: JestReact.unstable_toFlushAndYieldThrough,
|
||||
toHaveYielded: JestReact.unstable_toHaveYielded,
|
||||
toFlushAndThrow: JestReact.unstable_toFlushAndThrow,
|
||||
toMatchRenderedOutput: JestReact.unstable_toMatchRenderedOutput,
|
||||
});
|
||||
|
||||
beforeEach(() => (numExpectations = 0));
|
||||
|
||||
Reference in New Issue
Block a user