98 lines
5.0 KiB
JavaScript
98 lines
5.0 KiB
JavaScript
import { __awaiter, __generator } from "tslib";
|
|
import * as React from 'react';
|
|
import { asAsync } from './asAsync';
|
|
import { act, render, waitFor } from '@testing-library/react';
|
|
describe('asAsync', function () {
|
|
it('can async load exports', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
var _resolve, _loadCalled, loadThingPromise, AsyncThing, _a, container, unmount;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
_resolve = function () { return undefined; };
|
|
_loadCalled = false;
|
|
loadThingPromise = new Promise(function (resolve) {
|
|
_resolve = resolve;
|
|
});
|
|
AsyncThing = asAsync({
|
|
load: function () {
|
|
_loadCalled = true;
|
|
return loadThingPromise;
|
|
},
|
|
});
|
|
_a = render(React.createElement(AsyncThing, null)), container = _a.container, unmount = _a.unmount;
|
|
expect(_loadCalled).toBe(true);
|
|
expect(container).toBeEmptyDOMElement();
|
|
expect(_resolve).toBeTruthy();
|
|
return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
_resolve(function () { return React.createElement("div", null, "thing"); });
|
|
// allow microtasks to flush
|
|
return [4 /*yield*/, Promise.resolve()];
|
|
case 1:
|
|
// allow microtasks to flush
|
|
_a.sent();
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
}); })];
|
|
case 1:
|
|
_b.sent();
|
|
return [4 /*yield*/, waitFor(function () { return expect(container.firstChild).toHaveTextContent('thing'); })];
|
|
case 2:
|
|
_b.sent();
|
|
_loadCalled = false;
|
|
// Test cached case.
|
|
render(React.createElement(AsyncThing, null));
|
|
expect(_loadCalled).toBe(false);
|
|
expect(container.firstChild).toHaveTextContent('thing');
|
|
unmount();
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
}); });
|
|
it('can async load with placeholder', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
var _resolve, _loadCalled, loadThingPromise, AsyncThing, _a, container, unmount;
|
|
return __generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
_resolve = function () { return undefined; };
|
|
_loadCalled = false;
|
|
loadThingPromise = new Promise(function (resolve) {
|
|
_resolve = resolve;
|
|
});
|
|
AsyncThing = asAsync({
|
|
load: function () {
|
|
_loadCalled = true;
|
|
return loadThingPromise;
|
|
},
|
|
});
|
|
_a = render(React.createElement(AsyncThing, { asyncPlaceholder: function () { return React.createElement("div", null, "placeholder"); } })), container = _a.container, unmount = _a.unmount;
|
|
expect(_loadCalled).toBe(true);
|
|
expect(container).toHaveTextContent('placeholder');
|
|
expect(_resolve).toBeTruthy();
|
|
return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
_resolve(function () { return React.createElement("div", null, "thing"); });
|
|
return [4 /*yield*/, Promise.resolve()];
|
|
case 1:
|
|
_a.sent();
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
}); })];
|
|
case 1:
|
|
_b.sent();
|
|
return [4 /*yield*/, waitFor(function () { return expect(container.firstChild).toHaveTextContent('thing'); })];
|
|
case 2:
|
|
_b.sent();
|
|
unmount();
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
}); });
|
|
});
|
|
//# sourceMappingURL=asAsync.test.js.map
|