Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e54ace6

Browse files
Merge pull request #9 from aduth/sync-react-hooks-3-4-1
Sync to @testing-library/react-hooks@3.4.1
2 parents 9badbd2 + 35ec34c commit e54ace6

18 files changed

+453
-358
lines changed

‎lib/asyncUtils.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { ResolverType } from "./_types";
22
export interface TimeoutOptions {
33
timeout?: number;
4+
interval?: number;
45
suppressErrors?: boolean;
56
}
67
declare function asyncUtils(addResolver: (resolver: ResolverType) => void): {
7-
waitForValueToChange: (selector: () => any, options?: TimeoutOptions) => Promise<void>;
8-
waitForNextUpdate: (options?: TimeoutOptions) => Promise<void>;
98
wait: (callback: () => any, options?: TimeoutOptions) => Promise<void>;
9+
waitFor: (callback: () => any, { interval, timeout, suppressErrors }?: TimeoutOptions) => Promise<void>;
10+
waitForNextUpdate: (options?: TimeoutOptions) => Promise<void>;
11+
waitForValueToChange: (selector: () => any, options?: TimeoutOptions) => Promise<void>;
1012
};
1113
export default asyncUtils;

‎lib/asyncUtils.js

Lines changed: 57 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ var TimeoutError = /** @class */ (function (_super) {
7070
}
7171
return TimeoutError;
7272
}(Error));
73+
function resolveAfter(ms) {
74+
return new Promise(function (resolve) {
75+
setTimeout(resolve, ms);
76+
});
77+
}
78+
var hasWarnedDeprecatedWait = false;
7379
function asyncUtils(addResolver) {
7480
var nextUpdatePromise;
7581
function waitForNextUpdate(options) {
@@ -102,39 +108,45 @@ function asyncUtils(addResolver) {
102108
});
103109
});
104110
}
105-
function wait(callback, options) {
106-
if(options=== void 0){options={ timeout: 0,suppressErrors: true };}
111+
function waitFor(callback, _a) {
112+
var_b=_a=== void 0 ? {} : _a,interval=_b.interval, timeout=_b.timeout,_c=_b.suppressErrors,suppressErrors=_c===void0 ? true : _c;
107113
return __awaiter(this, void 0, void 0, function () {
108114
var checkResult, waitForResult;
109115
var _this = this;
110-
return __generator(this, function (_a) {
111-
switch (_a.label) {
116+
return __generator(this, function (_d) {
117+
switch (_d.label) {
112118
case 0:
113119
checkResult = function () {
114120
try {
115121
var callbackResult = callback();
116122
return callbackResult || callbackResult === undefined;
117123
}
118124
catch (err) {
119-
if (!options.suppressErrors) {
125+
if (!suppressErrors) {
120126
throw err;
121127
}
122128
}
123129
};
124130
waitForResult = function () { return __awaiter(_this, void 0, void 0, function () {
125-
var initialTimeout, startTime, err_1;
131+
var initialTimeout, startTime, nextCheck,err_1;
126132
return __generator(this, function (_a) {
127133
switch (_a.label) {
128134
case 0:
129-
initialTimeout = options.timeout;
135+
initialTimeout = timeout;
130136
_a.label = 1;
131137
case 1:
132138
if (!true) return [3 /*break*/, 6];
133139
startTime = Date.now();
134140
_a.label = 2;
135141
case 2:
136142
_a.trys.push([2, 4, , 5]);
137-
return [4 /*yield*/, waitForNextUpdate({ timeout: options.timeout })];
143+
nextCheck = interval
144+
? Promise.race([
145+
waitForNextUpdate({ timeout: timeout }),
146+
resolveAfter(interval),
147+
])
148+
: waitForNextUpdate({ timeout: timeout });
149+
return [4 /*yield*/, nextCheck];
138150
case 3:
139151
_a.sent();
140152
if (checkResult()) {
@@ -144,11 +156,11 @@ function asyncUtils(addResolver) {
144156
case 4:
145157
err_1 = _a.sent();
146158
if (err_1.timeout) {
147-
throw new TimeoutError("wait", { timeout: initialTimeout });
159+
throw new TimeoutError("waitFor", { timeout: initialTimeout });
148160
}
149161
throw err_1;
150162
case 5:
151-
options.timeout -= Date.now() - startTime;
163+
timeout -= Date.now() - startTime;
152164
return [3 /*break*/, 1];
153165
case 6: return [2 /*return*/];
154166
}
@@ -157,8 +169,8 @@ function asyncUtils(addResolver) {
157169
if (!!checkResult()) return [3 /*break*/, 2];
158170
return [4 /*yield*/, waitForResult()];
159171
case 1:
160-
_a.sent();
161-
_a.label = 2;
172+
_d.sent();
173+
_d.label = 2;
162174
case 2: return [2 /*return*/];
163175
}
164176
});
@@ -175,7 +187,7 @@ function asyncUtils(addResolver) {
175187
_a.label = 1;
176188
case 1:
177189
_a.trys.push([1, 3, , 4]);
178-
return [4 /*yield*/, wait(function () { return selector() !== initialValue; }, __assign({ suppressErrors: false }, options))];
190+
return [4 /*yield*/, waitFor(function () { return selector() !== initialValue; }, __assign({ suppressErrors: false }, options))];
179191
case 2:
180192
_a.sent();
181193
return [3 /*break*/, 4];
@@ -190,104 +202,40 @@ function asyncUtils(addResolver) {
190202
});
191203
});
192204
}
205+
function wait(callback, options) {
206+
if (options === void 0) { options = { timeout: 0, suppressErrors: true }; }
207+
return __awaiter(this, void 0, void 0, function () {
208+
var err_3;
209+
return __generator(this, function (_a) {
210+
switch (_a.label) {
211+
case 0:
212+
if (!hasWarnedDeprecatedWait) {
213+
hasWarnedDeprecatedWait = true;
214+
console.warn("`wait` has been deprecated. Use `waitFor` instead: https://react-hooks-testing-library.com/reference/api#waitfor.");
215+
}
216+
_a.label = 1;
217+
case 1:
218+
_a.trys.push([1, 3, , 4]);
219+
return [4 /*yield*/, waitFor(callback, options)];
220+
case 2:
221+
_a.sent();
222+
return [3 /*break*/, 4];
223+
case 3:
224+
err_3 = _a.sent();
225+
if (err_3.timeout) {
226+
throw new TimeoutError("wait", { timeout: options.timeout });
227+
}
228+
throw err_3;
229+
case 4: return [2 /*return*/];
230+
}
231+
});
232+
});
233+
}
193234
return {
194-
waitForValueToChange: waitForValueToChange,
195-
waitForNextUpdate: waitForNextUpdate,
196235
wait: wait,
236+
waitFor: waitFor,
237+
waitForNextUpdate: waitForNextUpdate,
238+
waitForValueToChange: waitForValueToChange,
197239
};
198240
}
199241
exports.default = asyncUtils;
200-
// function asyncUtils(addResolver: (r: ResolverType) => void) {
201-
// let nextUpdatePromise: Promise<any> | null = null;
202-
// const waitForNextUpdate = async (
203-
// options: TimeoutOptions = { timeout: 0 }
204-
// ) => {
205-
// if (!nextUpdatePromise) {
206-
// nextUpdatePromise = new Promise((resolve, reject) => {
207-
// let timeoutId: NodeJS.Timeout;
208-
// if (options.timeout && options.timeout > 0) {
209-
// timeoutId = setTimeout(
210-
// () => reject(new TimeoutError("waitForNextUpdate", options)),
211-
// options.timeout
212-
// );
213-
// }
214-
// addResolver(() => {
215-
// clearTimeout(timeoutId);
216-
// nextUpdatePromise = null;
217-
// resolve();
218-
// });
219-
// });
220-
// await act(() => {
221-
// if (nextUpdatePromise) {
222-
// return nextUpdatePromise;
223-
// }
224-
// return;
225-
// });
226-
// }
227-
// await nextUpdatePromise;
228-
// };
229-
// const wait = async (
230-
// callback: () => any,
231-
// { timeout, suppressErrors }: WaitOptions = {
232-
// timeout: 0,
233-
// suppressErrors: true,
234-
// }
235-
// ) => {
236-
// const checkResult = () => {
237-
// try {
238-
// const callbackResult = callback();
239-
// return callbackResult || callbackResult === undefined;
240-
// } catch (e) {
241-
// if (!suppressErrors) {
242-
// throw e;
243-
// }
244-
// }
245-
// };
246-
// const waitForResult = async () => {
247-
// const initialTimeout = timeout;
248-
// while (true) {
249-
// const startTime = Date.now();
250-
// try {
251-
// await waitForNextUpdate({ timeout });
252-
// if (checkResult()) {
253-
// return;
254-
// }
255-
// } catch (e) {
256-
// if (e.timeout) {
257-
// throw new TimeoutError("wait", { timeout: initialTimeout });
258-
// }
259-
// throw e;
260-
// }
261-
// timeout -= Date.now() - startTime;
262-
// }
263-
// };
264-
// if (!checkResult()) {
265-
// await waitForResult();
266-
// }
267-
// };
268-
// const waitForValueToChange = async (
269-
// selector: () => any,
270-
// options: TimeoutOptions = {
271-
// timeout: 0,
272-
// }
273-
// ) => {
274-
// const initialValue = selector();
275-
// try {
276-
// await wait(() => selector() !== initialValue, {
277-
// suppressErrors: false,
278-
// ...options,
279-
// });
280-
// } catch (e) {
281-
// if (e.timeout) {
282-
// throw new TimeoutError("waitForValueToChange", options);
283-
// }
284-
// throw e;
285-
// }
286-
// };
287-
// return {
288-
// wait,
289-
// waitForNextUpdate,
290-
// waitForValueToChange,
291-
// };
292-
// }
293-
// export default asyncUtils;

‎lib/index.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
import { renderHook } from "./renderHook";
2-
import { act } from "@testing-library/preact";
3-
import { cleanup } from "./cleanup";
4-
export { renderHook, act, cleanup };
1+
export * from "./pure";

‎lib/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5+
}) : (function(o, m, k, k2) {
6+
if (k2 === undefined) k2 = k;
7+
o[k2] = m[k];
8+
}));
9+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
10+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
11+
}
212
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
313
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
414
return new (P || (P = Promise))(function (resolve, reject) {
@@ -36,25 +46,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
3646
}
3747
};
3848
Object.defineProperty(exports, "__esModule", { value: true });
39-
exports.cleanup = exports.act = exports.renderHook = void 0;
4049
/* globals afterEach */
41-
var renderHook_1 = require("./renderHook");
42-
Object.defineProperty(exports, "renderHook", { enumerable: true, get: function () { return renderHook_1.renderHook; } });
43-
var preact_1 = require("@testing-library/preact");
44-
Object.defineProperty(exports, "act", { enumerable: true, get: function () { return preact_1.act; } });
45-
var cleanup_1 = require("./cleanup");
46-
Object.defineProperty(exports, "cleanup", { enumerable: true, get: function () { return cleanup_1.cleanup; } });
50+
var pure_1 = require("./pure");
4751
// @ts-ignore
4852
if (typeof afterEach === "function" && !process.env.PHTL_SKIP_AUTO_CLEANUP) {
4953
// @ts-ignore
5054
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
5155
return __generator(this, function (_a) {
5256
switch (_a.label) {
53-
case 0: return [4 /*yield*/, cleanup_1.cleanup()];
57+
case 0: return [4 /*yield*/, pure_1.cleanup()];
5458
case 1:
5559
_a.sent();
5660
return [2 /*return*/];
5761
}
5862
});
5963
}); });
6064
}
65+
__exportStar(require("./pure"), exports);

‎lib/pure.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { renderHook } from "./renderHook";
2+
import { act } from "@testing-library/preact";
3+
import { cleanup } from "./cleanup";
4+
export { renderHook, act, cleanup };

‎lib/pure.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.cleanup = exports.act = exports.renderHook = void 0;
4+
var renderHook_1 = require("./renderHook");
5+
Object.defineProperty(exports, "renderHook", { enumerable: true, get: function () { return renderHook_1.renderHook; } });
6+
var preact_1 = require("@testing-library/preact");
7+
Object.defineProperty(exports, "act", { enumerable: true, get: function () { return preact_1.act; } });
8+
var cleanup_1 = require("./cleanup");
9+
Object.defineProperty(exports, "cleanup", { enumerable: true, get: function () { return cleanup_1.cleanup; } });

‎lib/renderHook.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ export interface RenderHookOptions<P> {
55
wrapper?: ComponentType;
66
}
77
export declare function renderHook<P, R>(callback: Callback<P, R>, { initialProps, wrapper }?: RenderHookOptions<P>): {
8-
waitForValueToChange: (selector: () => any, options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
9-
waitForNextUpdate: (options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
108
wait: (callback: () => any, options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
9+
waitFor: (callback: () => any, { interval, timeout, suppressErrors }?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
10+
waitForNextUpdate: (options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
11+
waitForValueToChange: (selector: () => any, options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
1112
result: {
1213
readonly current: R;
1314
readonly error: Error;

‎lib/renderHook.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ var resultContainer_1 = __importDefault(require("./resultContainer"));
4141
var TestComponent_1 = __importStar(require("./TestComponent"));
4242
var cleanup_1 = require("./cleanup");
4343
var asyncUtils_1 = __importDefault(require("./asyncUtils"));
44-
var defaultWrapper = function (Component) { return preact_1.h(Component, null); };
4544
function renderHook(callback, _a) {
4645
var _b = _a === void 0 ? {} : _a, initialProps = _b.initialProps, wrapper = _b.wrapper;
4746
var _c = resultContainer_1.default(), result = _c.result, setValue = _c.setValue, setError = _c.setError, addResolver = _c.addResolver;
4847
var hookProps = {
4948
current: initialProps,
5049
};
5150
var wrapUiIfNeeded = function (innerElement) {
52-
return wrapper ? preact_1.h(wrapper, null, innerElement) : innerElement;
51+
return wrapper ? preact_1.h(wrapper, hookProps.current, innerElement) : innerElement;
5352
};
5453
var TestHook = function () {
5554
return wrapUiIfNeeded(preact_1.h(compat_1.Suspense, { fallback: preact_1.h(TestComponent_1.Fallback, null) },

‎package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@
2525
"test": "jest"
2626
},
2727
"devDependencies": {
28+
"@testing-library/preact": "^2.0.0",
2829
"@types/jest": "^25.2.2",
2930
"jest": "^25",
31+
"preact": "^10.4.8",
3032
"rimraf": "^3.0.2",
3133
"ts-jest": "^25.5.1",
3234
"typescript": "^3.9.2"
3335
},
3436
"peerDependencies": {
35-
"@testing-library/preact": "^1.0.2",
36-
"preact": "^10.4.1"
37+
"@testing-library/preact": "^2.0.0",
38+
"preact": "^10.4.8"
3739
},
3840
"dependencies": {}
3941
}

‎pure.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// makes it so people can import from '@testing-library/react-hooks/pure'
2+
module.exports = require("./lib/pure");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /