Skip to content

Commit

Permalink
Add "unstable_" prefix to react-cache and jest-react (facebook#13929)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bvaughn authored and jetoneza committed Jan 23, 2019
1 parent bc12034 commit b8ebcd2
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 53 deletions.
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;

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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions scripts/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions scripts/jest/spec-equivalence-reporter/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit b8ebcd2

Please sign in to comment.