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
Brian Vaughn committed
Oct 23, 2018 at 13:55 UTC
915e4eab53e49660f2dcac7847a3a2e3d97bfc41
13 files changed
+57
-53
fixtures/unstable-async/suspense/src/components/ContributorListPage.js
+4
-2
@@ -1,10 +1,12 @@
1
import React, {Fragment} from 'react';
2
-import {createResource} from 'react-cache';
2
+import {unstable_createResource} from 'react-cache';
3
import {cache} from '../cache';
4
import Spinner from './Spinner';
5
import {fetchCoreContributorListJSON} from '../api';
6
7
-const ContributorListResource = createResource(fetchCoreContributorListJSON);
7
+const ContributorListResource = unstable_createResource(
8
+ fetchCoreContributorListJSON
9
+);
10
11
const ContributorListPage = ({loadingId, onUserClick}) => (
12
<Fragment>
fixtures/unstable-async/suspense/src/components/UserPage.js
+6
-4
@@ -1,5 +1,5 @@
1
import React, {Suspense} from 'react';
2
-import {createResource} from 'react-cache';
2
+import {unstable_createResource} from 'react-cache';
3
import Spinner from './Spinner';
4
import {cache} from '../cache';
5
import {fetchUserProfileJSON, fetchUserRepositoriesListJSON} from '../api';
@@ -21,7 +21,7 @@ export default function UserPage({id}) {
21
);
22
}
23
24
-const UserDetailsResource = createResource(fetchUserProfileJSON);
24
+const UserDetailsResource = unstable_createResource(fetchUserProfileJSON);
25
26
function UserDetails({id}) {
27
const user = UserDetailsResource.read(cache, id);
@@ -103,7 +103,7 @@ const Email = ({email}) => (
103
</div>
104
);
105
106
-const ImageResource = createResource(
106
+const ImageResource = unstable_createResource(
107
src =>
108
new Promise(resolve => {
109
const img = new Image();
@@ -132,7 +132,9 @@ function UserPicture({source}) {
132
);
133
}
134
135
-const UserRepositoriesResource = createResource(fetchUserRepositoriesListJSON);
135
+const UserRepositoriesResource = unstable_createResource(
136
+ fetchUserRepositoriesListJSON
137
+);
138
139
function Repositories({id}) {
140
const repos = UserRepositoriesResource.read(cache, id);
packages/jest-react/src/JestReact.js
+10
-10
@@ -30,11 +30,11 @@ function assertYieldsWereCleared(root) {
30
invariant(
31
actualYields.length === 0,
32
'Log of yielded values is not empty. ' +
33
- 'Call expect(ReactTestRenderer).toHaveYielded(...) first.',
33
+ 'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.',
34
);
35
}
36
37
-export function toFlushAndYield(root, expectedYields) {
37
+export function unstable_toFlushAndYield(root, expectedYields) {
38
assertYieldsWereCleared(root);
39
const actualYields = root.unstable_flushAll();
40
return captureAssertion(() => {
@@ -42,7 +42,7 @@ export function toFlushAndYield(root, expectedYields) {
42
});
43
}
44
45
-export function toFlushAndYieldThrough(root, expectedYields) {
45
+export function unstable_toFlushAndYieldThrough(root, expectedYields) {
46
assertYieldsWereCleared(root);
47
const actualYields = root.unstable_flushNumberOfYields(expectedYields.length);
48
return captureAssertion(() => {
@@ -50,11 +50,11 @@ export function toFlushAndYieldThrough(root, expectedYields) {
50
});
51
}
52
53
-export function toFlushWithoutYielding(root) {
54
- return toFlushAndYield(root, []);
53
+export function unstable_toFlushWithoutYielding(root) {
54
+ return unstable_toFlushAndYield(root, []);
55
}
56
57
-export function toHaveYielded(ReactTestRenderer, expectedYields) {
57
+export function unstable_toHaveYielded(ReactTestRenderer, expectedYields) {
58
return captureAssertion(() => {
59
if (
60
ReactTestRenderer === null ||
@@ -63,9 +63,9 @@ export function toHaveYielded(ReactTestRenderer, expectedYields) {
63
) {
64
invariant(
65
false,
66
- 'The matcher `toHaveYielded` expects an instance of React Test ' +
66
+ 'The matcher `unstable_toHaveYielded` expects an instance of React Test ' +
67
'Renderer.\n\nTry: ' +
68
- 'expect(ReactTestRenderer).toHaveYielded(expectedYields)',
68
+ 'expect(ReactTestRenderer).unstable_toHaveYielded(expectedYields)',
69
);
70
}
71
const actualYields = ReactTestRenderer.unstable_clearYields();
@@ -73,7 +73,7 @@ export function toHaveYielded(ReactTestRenderer, expectedYields) {
73
});
74
}
75
76
-export function toFlushAndThrow(root, ...rest) {
76
+export function unstable_toFlushAndThrow(root, ...rest) {
77
assertYieldsWereCleared(root);
78
return captureAssertion(() => {
79
expect(() => {
@@ -82,7 +82,7 @@ export function toFlushAndThrow(root, ...rest) {
82
});
83
}
84
85
-export function toMatchRenderedOutput(root, expectedJSX) {
85
+export function unstable_toMatchRenderedOutput(root, expectedJSX) {
86
assertYieldsWereCleared(root);
87
const actualJSON = root.toJSON();
88
packages/react-cache/src/ReactCache.js
+4
-4
@@ -323,7 +323,7 @@ if (__DEV__) {
323
'%s: Invalid key type. Expected a string, number, symbol, or boolean, ' +
324
'but instead received: %s' +
325
'\n\nTo use non-primitive values as keys, you must pass a hash ' +
326
- 'function as the second argument to createResource().',
326
+ 'function as the second argument to unstable_createResource().',
327
methodName,
328
key,
329
);
@@ -341,20 +341,20 @@ type Resource<K, V> = {|
341
// were a more elegant way to do this in the function definition itself.
342
343
// Primitive keys do not request a hash function.
344
-declare function createResource<V, K: primitive, H: primitive>(
344
+declare function unstable_createResource<V, K: primitive, H: primitive>(
345
loadResource: (K) => Promise<V>,
346
hash?: (K) => H,
347
): Resource<K, V>;
348
349
// Non-primitive keys *do* require a hash function.
350
// eslint-disable-next-line no-redeclare
351
-declare function createResource<V, K: mixed, H: primitive>(
351
+declare function unstable_createResource<V, K: mixed, H: primitive>(
352
loadResource: (K) => Promise<V>,
353
hash: (K) => H,
354
): Resource<K, V>;
355
356
// eslint-disable-next-line no-redeclare
357
-export function createResource<V, K, H: primitive>(
357
+export function unstable_createResource<V, K, H: primitive>(
358
loadResource: K => Promise<V>,
359
hash: K => H,
360
): Resource<K, V> {
packages/react-cache/src/__tests__/ReactCache-test.js
+15
-15
@@ -18,12 +18,12 @@ describe('ReactCache', () => {
18
});
19
20
it('throws a promise if the requested value is not in the cache', async () => {
21
- const {createCache, createResource} = ReactCache;
21
+ const {createCache, unstable_createResource} = ReactCache;
22
23
function loadUpperCase(text) {
24
return Promise.resolve(text.toUpperCase());
25
}
26
- const UpperCase = createResource(loadUpperCase);
26
+ const UpperCase = unstable_createResource(loadUpperCase);
27
const cache = createCache();
28
29
let suspender;
@@ -39,7 +39,7 @@ describe('ReactCache', () => {
39
});
40
41
it('throws an error on the subsequent read if the promise is rejected', async () => {
42
- const {createCache, createResource} = ReactCache;
42
+ const {createCache, unstable_createResource} = ReactCache;
43
44
let shouldFail = true;
45
function loadUpperCase(text) {
@@ -52,7 +52,7 @@ describe('ReactCache', () => {
52
return Promise.resolve(text.toUpperCase());
53
}
54
}
55
- const UpperCase = createResource(loadUpperCase);
55
+ const UpperCase = unstable_createResource(loadUpperCase);
56
const cache = createCache();
57
58
let suspender;
@@ -83,12 +83,12 @@ describe('ReactCache', () => {
83
});
84
85
it('can preload data ahead of time', async () => {
86
- const {createCache, createResource} = ReactCache;
86
+ const {createCache, unstable_createResource} = ReactCache;
87
88
function loadUpperCase(text) {
89
return Promise.resolve(text.toUpperCase());
90
}
91
- const UpperCase = createResource(loadUpperCase);
91
+ const UpperCase = unstable_createResource(loadUpperCase);
92
const cache = createCache();
93
94
UpperCase.preload(cache, 'hello');
@@ -99,12 +99,12 @@ describe('ReactCache', () => {
99
});
100
101
it('does not throw if preloaded promise rejects', async () => {
102
- const {createCache, createResource} = ReactCache;
102
+ const {createCache, unstable_createResource} = ReactCache;
103
104
function loadUpperCase(text) {
105
return Promise.reject(new Error('uh oh'));
106
}
107
- const UpperCase = createResource(loadUpperCase);
107
+ const UpperCase = unstable_createResource(loadUpperCase);
108
const cache = createCache();
109
110
UpperCase.preload(cache, 'hello');
@@ -115,7 +115,7 @@ describe('ReactCache', () => {
115
});
116
117
it('accepts custom hash function', async () => {
118
- const {createCache, createResource} = ReactCache;
118
+ const {createCache, unstable_createResource} = ReactCache;
119
120
function loadSum([a, b]) {
121
return Promise.resolve(a + b);
@@ -123,7 +123,7 @@ describe('ReactCache', () => {
123
function hash([a, b]) {
124
return `${a + b}`;
125
}
126
- const Sum = createResource(loadSum, hash);
126
+ const Sum = unstable_createResource(loadSum, hash);
127
const cache = createCache();
128
129
Sum.preload(cache, [5, 5]);
@@ -166,7 +166,7 @@ describe('ReactCache', () => {
166
});
167
168
it('warns if non-primitive key is passed to a resource without a hash function', () => {
169
- const {createCache, createResource} = ReactCache;
169
+ const {createCache, unstable_createResource} = ReactCache;
170
171
spyOnDev(console, 'error');
172
@@ -174,7 +174,7 @@ describe('ReactCache', () => {
174
return Promise.resolve(a + b);
175
}
176
177
- const Sum = createResource(loadSum);
177
+ const Sum = unstable_createResource(loadSum);
178
const cache = createCache();
179
180
function fn() {
@@ -187,7 +187,7 @@ describe('ReactCache', () => {
187
'preload: Invalid key type. Expected a string, number, symbol, or ' +
188
'boolean, but instead received: 5,5\n\n' +
189
'To use non-primitive values as keys, you must pass a hash ' +
190
- 'function as the second argument to createResource().',
190
+ 'function as the second argument to unstable_createResource().',
191
],
192
{withoutStack: true},
193
);
@@ -197,12 +197,12 @@ describe('ReactCache', () => {
197
});
198
199
it('stays within maximum capacity by evicting the least recently used record', async () => {
200
- const {createCache, createResource} = ReactCache;
200
+ const {createCache, unstable_createResource} = ReactCache;
201
202
function loadIntegerString(int) {
203
return Promise.resolve(int + '');
204
}
205
- const IntegerStringResource = createResource(loadIntegerString);
205
+ const IntegerStringResource = unstable_createResource(loadIntegerString);
206
const cache = createCache();
207
208
// TODO: This is hard-coded to a maximum size of 500. Make this configurable
packages/react-dom/src/__tests__/ReactDOMSuspensePlaceholder-test.internal.js
+1
-1
@@ -31,7 +31,7 @@ describe('ReactDOMSuspensePlaceholder', () => {
31
cache = ReactCache.createCache(invalidateCache);
32
}
33
invalidateCache();
34
- TextResource = ReactCache.createResource(([text, ms = 0]) => {
34
+ TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
35
return new Promise((resolve, reject) =>
36
setTimeout(() => {
37
resolve(text);
packages/react-reconciler/src/__tests__/ReactSuspense-test.internal.js
+1
-1
@@ -29,7 +29,7 @@ describe('ReactSuspense', () => {
29
cache = ReactCache.createCache(invalidateCache);
30
}
31
invalidateCache();
32
- TextResource = ReactCache.createResource(([text, ms = 0]) => {
32
+ TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
33
let listeners = null;
34
let status = 'pending';
35
let value = null;
packages/react-reconciler/src/__tests__/ReactSuspensePlaceholder-test.internal.js
+1
-1
@@ -43,7 +43,7 @@ function runPlaceholderTests(suiteLabel, loadReactNoop) {
43
cache = ReactCache.createCache(invalidateCache);
44
}
45
invalidateCache();
46
- TextResource = ReactCache.createResource(([text, ms = 0]) => {
46
+ TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
47
let listeners = null;
48
let status = 'pending';
49
let value = null;
packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.internal.js
+1
-1
@@ -32,7 +32,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
32
cache = ReactCache.createCache(invalidateCache);
33
}
34
invalidateCache();
35
- TextResource = ReactCache.createResource(([text, ms = 0]) => {
35
+ TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
36
return new Promise((resolve, reject) =>
37
setTimeout(() => {
38
if (textResourceShouldFail) {
packages/react/src/__tests__/ReactProfiler-test.internal.js
+1
-1
@@ -2176,7 +2176,7 @@ describe('Profiler', () => {
2176
2177
resourcePromise = null;
2178
2179
- TextResource = ReactCache.createResource(([text, ms = 0]) => {
2179
+ TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
2180
resourcePromise = new Promise((resolve, reject) =>
2181
setTimeout(() => {
2182
yieldForRenderer(`Promise resolved [${text}]`);
packages/react/src/__tests__/ReactProfilerDOM-test.internal.js
+1
-1
@@ -85,7 +85,7 @@ describe('ProfilerDOM', () => {
85
86
resourcePromise = null;
87
88
- TextResource = ReactCache.createResource(([text, ms = 0]) => {
88
+ TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
89
resourcePromise = new Promise(
90
SchedulerTracing.unstable_wrap((resolve, reject) => {
91
setTimeout(
scripts/jest/setupTests.js
+6
-6
@@ -48,12 +48,12 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
48
...require('./matchers/interactionTracing'),
49
...require('./matchers/toWarnDev'),
50
51
- toFlushWithoutYielding: JestReact.toFlushWithoutYielding,
52
- toFlushAndYield: JestReact.toFlushAndYield,
53
- toFlushAndYieldThrough: JestReact.toFlushAndYieldThrough,
54
- toHaveYielded: JestReact.toHaveYielded,
55
- toFlushAndThrow: JestReact.toFlushAndThrow,
56
- toMatchRenderedOutput: JestReact.toMatchRenderedOutput,
51
+ toFlushWithoutYielding: JestReact.unstable_toFlushWithoutYielding,
52
+ toFlushAndYield: JestReact.unstable_toFlushAndYield,
53
+ toFlushAndYieldThrough: JestReact.unstable_toFlushAndYieldThrough,
54
+ toHaveYielded: JestReact.unstable_toHaveYielded,
55
+ toFlushAndThrow: JestReact.unstable_toFlushAndThrow,
56
+ toMatchRenderedOutput: JestReact.unstable_toMatchRenderedOutput,
57
});
58
59
// We have a Babel transform that inserts guards against infinite loops.
scripts/jest/spec-equivalence-reporter/setupTests.js
+6
-6
@@ -50,12 +50,12 @@ expect.extend({
50
...require('../matchers/interactionTracing'),
51
...require('../matchers/toWarnDev'),
52
53
- toFlushWithoutYielding: JestReact.toFlushWithoutYielding,
54
- toFlushAndYield: JestReact.toFlushAndYield,
55
- toFlushAndYieldThrough: JestReact.toFlushAndYieldThrough,
56
- toHaveYielded: JestReact.toHaveYielded,
57
- toFlushAndThrow: JestReact.toFlushAndThrow,
58
- toMatchRenderedOutput: JestReact.toMatchRenderedOutput,
53
+ toFlushWithoutYielding: JestReact.unstable_toFlushWithoutYielding,
54
+ toFlushAndYield: JestReact.unstable_toFlushAndYield,
55
+ toFlushAndYieldThrough: JestReact.unstable_toFlushAndYieldThrough,
56
+ toHaveYielded: JestReact.unstable_toHaveYielded,
57
+ toFlushAndThrow: JestReact.unstable_toFlushAndThrow,
58
+ toMatchRenderedOutput: JestReact.unstable_toMatchRenderedOutput,
59
});
60
61
beforeEach(() => (numExpectations = 0));