reshow-return
Version:
reshow-return (simple connect component with reshow-flux)
127 lines (126 loc) • 4.19 kB
JavaScript
import _asyncToGenerator from "reshow-runtime/es/helpers/asyncToGenerator";
var _span, _span2, _span3, _i, _i2, _span4, _i3;
// @ts-check
import { expect } from "chai";
import { render, waitFor, act, getSinon } from "reshow-unit";
import * as React from "react";
var {
useEffect,
useState
} = React;
import { Map } from "reshow-flux";
import usePartialRender from "../usePartialRender.mjs";
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
describe("Test usePartialRender", function () {
it("smoke test", /*#__PURE__*/_asyncToGenerator(function* () {
var spy = getSinon().spy();
var Comp = function Comp() {
var [renderItems, partialRender, _setRenderKeys] = usePartialRender(["foo", "bar"]);
useEffect(function () {
partialRender({
foo: _span || (_span = /*#__PURE__*/_jsx("span", {
children: "aaa"
})),
bar: _span2 || (_span2 = /*#__PURE__*/_jsx("span", {
children: "bbb"
}))
});
}, []);
spy();
return /*#__PURE__*/_jsx("div", {
children: renderItems
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
yield act(function () {});
yield waitFor(function () {
expect(wrap.html()).to.equal("<div><span>aaa</span><span>bbb</span></div>");
// because STRICT_MODE will call two times, else actually 1.
expect(spy.callCount).to.equal(2);
});
}));
it("test init children", /*#__PURE__*/_asyncToGenerator(function* () {
var Comp = function Comp() {
var list = {
foo: _span3 || (_span3 = /*#__PURE__*/_jsx("span", {})),
bar: _i || (_i = /*#__PURE__*/_jsx("i", {}))
};
var [renderItems] = usePartialRender(Object.keys(list), list);
return /*#__PURE__*/_jsx("div", {
children: renderItems
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
yield act(function () {});
yield waitFor(function () {
expect(wrap.html()).to.equal("<div><span></span><i></i></div>");
});
}));
it("test empty children", /*#__PURE__*/_asyncToGenerator(function* () {
var Comp = function Comp() {
var [renderItems] = usePartialRender();
return /*#__PURE__*/_jsx("div", {
children: renderItems
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
yield act(function () {});
yield waitFor(function () {
expect(wrap.html()).to.equal("<div></div>");
});
}));
it("test reset", /*#__PURE__*/_asyncToGenerator(function* () {
var gSetCall;
var gCount = 0;
var Comp = function Comp() {
var list = {
foo: /*#__PURE__*/_jsx("span", {
children: gCount
}),
bar: _i2 || (_i2 = /*#__PURE__*/_jsx("i", {}))
};
var [call, setCall] = useState(0);
var [renderItems, partialRender] = usePartialRender(Object.keys(list), list);
gSetCall = setCall;
useEffect(function () {
partialRender(list);
}, [call]);
return /*#__PURE__*/_jsx("div", {
"data-call": call,
children: renderItems
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
yield act(function () {});
yield waitFor(function () {
expect(wrap.html()).to.equal("<div data-call=\"0\"><span>0</span><i></i></div>");
});
yield act(function () {
gCount++;
gSetCall(function (/** @type number*/c) {
return ++c;
});
});
yield act();
yield waitFor(function () {
expect(wrap.html()).to.equal("<div data-call=\"1\"><span>1</span><i></i></div>");
});
}));
it("test use keySeq", /*#__PURE__*/_asyncToGenerator(function* () {
var list = Map({
foo: _span4 || (_span4 = /*#__PURE__*/_jsx("span", {})),
bar: _i3 || (_i3 = /*#__PURE__*/_jsx("i", {}))
});
var Comp = function Comp() {
var [renderItems] = usePartialRender(list.keySeq(), list);
return /*#__PURE__*/_jsx(_Fragment, {
children: renderItems
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
yield act();
yield waitFor(function () {
expect(wrap.html()).to.equal("<span></span><i></i>");
});
}));
});