59 lines
2.3 KiB
JavaScript
59 lines
2.3 KiB
JavaScript
import * as React from 'react';
|
|
import { asAsync } from './asAsync';
|
|
import { mount } from 'enzyme';
|
|
describe('asAsync', function () {
|
|
it('can async load exports', function (done) {
|
|
var _resolve = function () { return undefined; };
|
|
var _loadCalled = false;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
var loadThingPromise = new Promise(function (resolve) {
|
|
_resolve = resolve;
|
|
});
|
|
var AsyncThing = asAsync({
|
|
load: function () {
|
|
_loadCalled = true;
|
|
return loadThingPromise;
|
|
},
|
|
});
|
|
var wrapper = mount(React.createElement(AsyncThing, null));
|
|
expect(_loadCalled).toBe(true);
|
|
expect(wrapper.text()).toBeFalsy();
|
|
expect(_resolve).toBeTruthy();
|
|
_resolve(function () { return React.createElement("div", null, "thing"); });
|
|
process.nextTick(function () {
|
|
wrapper.update();
|
|
expect(wrapper.text()).toEqual('thing');
|
|
_loadCalled = false;
|
|
// Test cached case.
|
|
mount(React.createElement(AsyncThing, null));
|
|
expect(_loadCalled).toBe(false);
|
|
expect(wrapper.text()).toEqual('thing');
|
|
done();
|
|
});
|
|
});
|
|
it('can async load with placeholder', function (done) {
|
|
var _resolve = function () { return undefined; };
|
|
var _loadCalled = false;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
var loadThingPromise = new Promise(function (resolve) {
|
|
_resolve = resolve;
|
|
});
|
|
var AsyncThing = asAsync({
|
|
load: function () {
|
|
_loadCalled = true;
|
|
return loadThingPromise;
|
|
},
|
|
});
|
|
var wrapper = mount(React.createElement(AsyncThing, { asyncPlaceholder: function () { return React.createElement("div", null, "placeholder"); } }));
|
|
expect(_loadCalled).toBe(true);
|
|
expect(wrapper.text()).toEqual('placeholder');
|
|
expect(_resolve).toBeTruthy();
|
|
_resolve(function () { return React.createElement("div", null, "thing"); });
|
|
process.nextTick(function () {
|
|
wrapper.update();
|
|
expect(wrapper.text()).toEqual('thing');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
//# sourceMappingURL=asAsync.test.js.map
|