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

Sync to @testing-library/react-hooks@3.4.1 #9

Merged
merged 4 commits into from Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/asyncUtils.d.ts
@@ -1,11 +1,13 @@
import { ResolverType } from "./_types";
export interface TimeoutOptions {
timeout?: number;
interval?: number;
suppressErrors?: boolean;
}
declare function asyncUtils(addResolver: (resolver: ResolverType) => void): {
waitForValueToChange: (selector: () => any, options?: TimeoutOptions) => Promise<void>;
waitForNextUpdate: (options?: TimeoutOptions) => Promise<void>;
wait: (callback: () => any, options?: TimeoutOptions) => Promise<void>;
waitFor: (callback: () => any, { interval, timeout, suppressErrors }?: TimeoutOptions) => Promise<void>;
waitForNextUpdate: (options?: TimeoutOptions) => Promise<void>;
waitForValueToChange: (selector: () => any, options?: TimeoutOptions) => Promise<void>;
};
export default asyncUtils;
166 changes: 57 additions & 109 deletions lib/asyncUtils.js
Expand Up @@ -70,6 +70,12 @@ var TimeoutError = /** @class */ (function (_super) {
}
return TimeoutError;
}(Error));
function resolveAfter(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}
var hasWarnedDeprecatedWait = false;
function asyncUtils(addResolver) {
var nextUpdatePromise;
function waitForNextUpdate(options) {
Expand Down Expand Up @@ -102,39 +108,45 @@ function asyncUtils(addResolver) {
});
});
}
function wait(callback, options) {
if (options === void 0) { options = { timeout: 0, suppressErrors: true }; }
function waitFor(callback, _a) {
var _b = _a === void 0 ? {} : _a, interval = _b.interval, timeout = _b.timeout, _c = _b.suppressErrors, suppressErrors = _c === void 0 ? true : _c;
return __awaiter(this, void 0, void 0, function () {
var checkResult, waitForResult;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
checkResult = function () {
try {
var callbackResult = callback();
return callbackResult || callbackResult === undefined;
}
catch (err) {
if (!options.suppressErrors) {
if (!suppressErrors) {
throw err;
}
}
};
waitForResult = function () { return __awaiter(_this, void 0, void 0, function () {
var initialTimeout, startTime, err_1;
var initialTimeout, startTime, nextCheck, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
initialTimeout = options.timeout;
initialTimeout = timeout;
_a.label = 1;
case 1:
if (!true) return [3 /*break*/, 6];
startTime = Date.now();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, waitForNextUpdate({ timeout: options.timeout })];
nextCheck = interval
? Promise.race([
waitForNextUpdate({ timeout: timeout }),
resolveAfter(interval),
])
: waitForNextUpdate({ timeout: timeout });
return [4 /*yield*/, nextCheck];
case 3:
_a.sent();
if (checkResult()) {
Expand All @@ -144,11 +156,11 @@ function asyncUtils(addResolver) {
case 4:
err_1 = _a.sent();
if (err_1.timeout) {
throw new TimeoutError("wait", { timeout: initialTimeout });
throw new TimeoutError("waitFor", { timeout: initialTimeout });
}
throw err_1;
case 5:
options.timeout -= Date.now() - startTime;
timeout -= Date.now() - startTime;
return [3 /*break*/, 1];
case 6: return [2 /*return*/];
}
Expand All @@ -157,8 +169,8 @@ function asyncUtils(addResolver) {
if (!!checkResult()) return [3 /*break*/, 2];
return [4 /*yield*/, waitForResult()];
case 1:
_a.sent();
_a.label = 2;
_d.sent();
_d.label = 2;
case 2: return [2 /*return*/];
}
});
Expand All @@ -175,7 +187,7 @@ function asyncUtils(addResolver) {
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, wait(function () { return selector() !== initialValue; }, __assign({ suppressErrors: false }, options))];
return [4 /*yield*/, waitFor(function () { return selector() !== initialValue; }, __assign({ suppressErrors: false }, options))];
case 2:
_a.sent();
return [3 /*break*/, 4];
Expand All @@ -190,104 +202,40 @@ function asyncUtils(addResolver) {
});
});
}
function wait(callback, options) {
if (options === void 0) { options = { timeout: 0, suppressErrors: true }; }
return __awaiter(this, void 0, void 0, function () {
var err_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!hasWarnedDeprecatedWait) {
hasWarnedDeprecatedWait = true;
console.warn("`wait` has been deprecated. Use `waitFor` instead: https://react-hooks-testing-library.com/reference/api#waitfor.");
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, waitFor(callback, options)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
err_3 = _a.sent();
if (err_3.timeout) {
throw new TimeoutError("wait", { timeout: options.timeout });
}
throw err_3;
case 4: return [2 /*return*/];
}
});
});
}
return {
waitForValueToChange: waitForValueToChange,
waitForNextUpdate: waitForNextUpdate,
wait: wait,
waitFor: waitFor,
waitForNextUpdate: waitForNextUpdate,
waitForValueToChange: waitForValueToChange,
};
}
exports.default = asyncUtils;
// function asyncUtils(addResolver: (r: ResolverType) => void) {
// let nextUpdatePromise: Promise<any> | null = null;
// const waitForNextUpdate = async (
// options: TimeoutOptions = { timeout: 0 }
// ) => {
// if (!nextUpdatePromise) {
// nextUpdatePromise = new Promise((resolve, reject) => {
// let timeoutId: NodeJS.Timeout;
// if (options.timeout && options.timeout > 0) {
// timeoutId = setTimeout(
// () => reject(new TimeoutError("waitForNextUpdate", options)),
// options.timeout
// );
// }
// addResolver(() => {
// clearTimeout(timeoutId);
// nextUpdatePromise = null;
// resolve();
// });
// });
// await act(() => {
// if (nextUpdatePromise) {
// return nextUpdatePromise;
// }
// return;
// });
// }
// await nextUpdatePromise;
// };
// const wait = async (
// callback: () => any,
// { timeout, suppressErrors }: WaitOptions = {
// timeout: 0,
// suppressErrors: true,
// }
// ) => {
// const checkResult = () => {
// try {
// const callbackResult = callback();
// return callbackResult || callbackResult === undefined;
// } catch (e) {
// if (!suppressErrors) {
// throw e;
// }
// }
// };
// const waitForResult = async () => {
// const initialTimeout = timeout;
// while (true) {
// const startTime = Date.now();
// try {
// await waitForNextUpdate({ timeout });
// if (checkResult()) {
// return;
// }
// } catch (e) {
// if (e.timeout) {
// throw new TimeoutError("wait", { timeout: initialTimeout });
// }
// throw e;
// }
// timeout -= Date.now() - startTime;
// }
// };
// if (!checkResult()) {
// await waitForResult();
// }
// };
// const waitForValueToChange = async (
// selector: () => any,
// options: TimeoutOptions = {
// timeout: 0,
// }
// ) => {
// const initialValue = selector();
// try {
// await wait(() => selector() !== initialValue, {
// suppressErrors: false,
// ...options,
// });
// } catch (e) {
// if (e.timeout) {
// throw new TimeoutError("waitForValueToChange", options);
// }
// throw e;
// }
// };
// return {
// wait,
// waitForNextUpdate,
// waitForValueToChange,
// };
// }
// export default asyncUtils;
5 changes: 1 addition & 4 deletions lib/index.d.ts
@@ -1,4 +1 @@
import { renderHook } from "./renderHook";
import { act } from "@testing-library/preact";
import { cleanup } from "./cleanup";
export { renderHook, act, cleanup };
export * from "./pure";
21 changes: 13 additions & 8 deletions lib/index.js
@@ -1,4 +1,14 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
}
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand Down Expand Up @@ -36,25 +46,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanup = exports.act = exports.renderHook = void 0;
/* globals afterEach */
var renderHook_1 = require("./renderHook");
Object.defineProperty(exports, "renderHook", { enumerable: true, get: function () { return renderHook_1.renderHook; } });
var preact_1 = require("@testing-library/preact");
Object.defineProperty(exports, "act", { enumerable: true, get: function () { return preact_1.act; } });
var cleanup_1 = require("./cleanup");
Object.defineProperty(exports, "cleanup", { enumerable: true, get: function () { return cleanup_1.cleanup; } });
var pure_1 = require("./pure");
// @ts-ignore
if (typeof afterEach === "function" && !process.env.PHTL_SKIP_AUTO_CLEANUP) {
// @ts-ignore
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, cleanup_1.cleanup()];
case 0: return [4 /*yield*/, pure_1.cleanup()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
}
__exportStar(require("./pure"), exports);
4 changes: 4 additions & 0 deletions lib/pure.d.ts
@@ -0,0 +1,4 @@
import { renderHook } from "./renderHook";
import { act } from "@testing-library/preact";
import { cleanup } from "./cleanup";
export { renderHook, act, cleanup };
9 changes: 9 additions & 0 deletions lib/pure.js
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanup = exports.act = exports.renderHook = void 0;
var renderHook_1 = require("./renderHook");
Object.defineProperty(exports, "renderHook", { enumerable: true, get: function () { return renderHook_1.renderHook; } });
var preact_1 = require("@testing-library/preact");
Object.defineProperty(exports, "act", { enumerable: true, get: function () { return preact_1.act; } });
var cleanup_1 = require("./cleanup");
Object.defineProperty(exports, "cleanup", { enumerable: true, get: function () { return cleanup_1.cleanup; } });
5 changes: 3 additions & 2 deletions lib/renderHook.d.ts
Expand Up @@ -5,9 +5,10 @@ export interface RenderHookOptions<P> {
wrapper?: ComponentType;
}
export declare function renderHook<P, R>(callback: Callback<P, R>, { initialProps, wrapper }?: RenderHookOptions<P>): {
waitForValueToChange: (selector: () => any, options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
waitForNextUpdate: (options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
wait: (callback: () => any, options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
waitFor: (callback: () => any, { interval, timeout, suppressErrors }?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
waitForNextUpdate: (options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
waitForValueToChange: (selector: () => any, options?: import("./asyncUtils").TimeoutOptions) => Promise<void>;
result: {
readonly current: R;
readonly error: Error;
Expand Down
3 changes: 1 addition & 2 deletions lib/renderHook.js
Expand Up @@ -41,15 +41,14 @@ var resultContainer_1 = __importDefault(require("./resultContainer"));
var TestComponent_1 = __importStar(require("./TestComponent"));
var cleanup_1 = require("./cleanup");
var asyncUtils_1 = __importDefault(require("./asyncUtils"));
var defaultWrapper = function (Component) { return preact_1.h(Component, null); };
function renderHook(callback, _a) {
var _b = _a === void 0 ? {} : _a, initialProps = _b.initialProps, wrapper = _b.wrapper;
var _c = resultContainer_1.default(), result = _c.result, setValue = _c.setValue, setError = _c.setError, addResolver = _c.addResolver;
var hookProps = {
current: initialProps,
};
var wrapUiIfNeeded = function (innerElement) {
return wrapper ? preact_1.h(wrapper, null, innerElement) : innerElement;
return wrapper ? preact_1.h(wrapper, hookProps.current, innerElement) : innerElement;
};
var TestHook = function () {
return wrapUiIfNeeded(preact_1.h(compat_1.Suspense, { fallback: preact_1.h(TestComponent_1.Fallback, null) },
Expand Down
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -25,15 +25,17 @@
"test": "jest"
},
"devDependencies": {
"@testing-library/preact": "^2.0.0",
"@types/jest": "^25.2.2",
"jest": "^25",
"preact": "^10.4.8",
"rimraf": "^3.0.2",
"ts-jest": "^25.5.1",
"typescript": "^3.9.2"
},
"peerDependencies": {
"@testing-library/preact": "^1.0.2",
"preact": "^10.4.1"
"@testing-library/preact": "^2.0.0",
"preact": "^10.4.8"
},
"dependencies": {}
}
2 changes: 2 additions & 0 deletions pure.js
@@ -0,0 +1,2 @@
// makes it so people can import from '@testing-library/react-hooks/pure'
module.exports = require("./lib/pure");