reshow-flux
Version:
The smallest react flux and Fast hook alternative
152 lines (151 loc) • 4.57 kB
JavaScript
import _asyncToGenerator from "reshow-runtime/es/helpers/asyncToGenerator";
// @ts-check
import * as React from "react";
import * as sinon from "sinon";
import { expect } from "chai";
import { act, render, waitFor } from "reshow-unit";
import { createReducer } from "reshow-flux-base";
import useStore from "../useStore.mjs";
import ImmutableStore from "../ImmutableStore.mjs";
import { jsx as _jsx } from "react/jsx-runtime";
describe("useStore Test", function () {
var reducer;
beforeEach(function () {
reducer = createReducer(function (_state, action) {
return action;
}, {});
});
it("test default", function () {
var [store] = reducer;
var Comp = function Comp() {
var state = useStore(store, function () {
return "foo";
});
return /*#__PURE__*/_jsx("div", {
children: state
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
expect(wrap.html()).to.equal("<div>foo</div>");
});
it("test apply dispatch", /*#__PURE__*/_asyncToGenerator(function* () {
var [store, dispatch] = reducer;
var Comp = function Comp() {
var state = useStore(store, function (emit) {
if (emit.current) {
emit.current.state = "bar";
emit.current.notify();
return emit.current.state;
}
});
return /*#__PURE__*/_jsx("div", {
children: state
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
expect(wrap.html()).to.equal("<div></div>");
yield act(function () {
return dispatch();
}, 5);
yield waitFor(function () {
expect(wrap.html()).to.equal("<div>bar</div>");
});
}));
it("test not apply dispatch", /*#__PURE__*/_asyncToGenerator(function* () {
var heeding = sinon.spy(function (emit) {
var {
action,
notify
} = emit.current || {};
if (action && action.type === "on") {
emit.current.state = "bar";
notify();
}
return "foo";
});
var [store, dispatch] = reducer;
var Comp = function Comp() {
var state = useStore(store, heeding);
return /*#__PURE__*/_jsx("div", {
children: state
});
};
expect(heeding.callCount).to.equal(0);
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
expect(heeding.callCount >= 1).to.be.true;
expect(wrap.html()).to.equal("<div>foo</div>");
yield act(function () {
return dispatch("on");
}, 5);
yield waitFor(/*#__PURE__*/_asyncToGenerator(function* () {
yield act(function () {
expect(heeding.callCount >= 2).to.be.true;
expect(wrap.html()).to.equal("<div>bar</div>");
});
}));
yield act(function () {
return dispatch("off");
}, 5);
yield waitFor(/*#__PURE__*/_asyncToGenerator(function* () {
yield act(function () {
expect(heeding.callCount >= 3).to.be.true;
expect(wrap.html()).to.equal("<div>bar</div>");
});
}));
}));
});
describe("useStore Test without heeding", function () {
var reducer;
beforeEach(function () {
reducer = createReducer(function (_state, action) {
return action;
}, {
foo: "bar"
});
});
it("test default", function () {
var [store] = reducer;
var Comp = function Comp() {
var state = useStore(store);
return /*#__PURE__*/_jsx("div", {
children: state.foo
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
expect(wrap.html()).to.equal("<div>bar</div>");
});
it("test apply dispatch", /*#__PURE__*/_asyncToGenerator(function* () {
var [store, dispatch] = reducer;
var Comp = function Comp() {
var state = useStore(store);
return /*#__PURE__*/_jsx("div", {
children: state.bar
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
expect(wrap.html()).to.equal("<div></div>");
yield act(function () {
return dispatch({
bar: "aaa"
});
}, 5);
expect(wrap.html()).to.equal("<div>aaa</div>");
}));
it("test ImmutableStore", /*#__PURE__*/_asyncToGenerator(function* () {
var [store, dispatch] = ImmutableStore();
var Comp = function Comp() {
var state = useStore(store);
return /*#__PURE__*/_jsx("div", {
children: state.get("foo")
});
};
var wrap = render(/*#__PURE__*/_jsx(Comp, {}));
expect(wrap.html()).to.equal("<div></div>");
yield act(function () {
return dispatch({
foo: "bar"
});
}, 5);
expect(wrap.html()).to.equal("<div>bar</div>");
}));
});