reshow-flux
Version:
The smallest react flux and Fast hook alternative
88 lines (86 loc) • 2.07 kB
JavaScript
//@ts-check
/**
* @typedef {import("mocha")}
*/
import { expect } from "chai";
import ImmutableStore, { Map, mergeMap } from "../ImmutableStore.mjs";
describe("Test ImmutableStore", function () {
var reducer;
beforeEach(function () {
reducer = ImmutableStore(function (state, action) {
return mergeMap(state, action);
});
});
it("test merge map with js array", function () {
var [store, dispatch] = reducer;
var action = {
aaa: {
bbb: "ccc"
}
};
dispatch(action);
var state = store.getState();
var aaa = state.get("aaa");
expect(Map.isMap(aaa)).to.be.false;
expect(aaa).to.deep.equal({
bbb: "ccc"
});
expect(aaa === action.aaa).to.be.true;
});
it("test merge map with map", function () {
var [store, dispatch] = reducer;
var action = Map({
aaa: Map({
bbb: "ccc"
})
});
dispatch(action);
var state = store.getState();
var aaa = state.get("aaa");
expect(Map.isMap(aaa)).to.be.true;
expect(aaa.toJS()).to.deep.equal({
bbb: "ccc"
});
expect(aaa === action.get("aaa")).to.be.true;
});
it("test getIn", function () {
var result = mergeMap(Map(), {
a: {
b: {
c: "d"
}
}
});
var b = result.getIn(["a", "b"]);
expect(Map.isMap(b)).to.be.false;
expect(b).to.deep.equal({
c: "d"
});
});
it("test get map", function () {
var [store, dispatch] = reducer;
var action = {
aaa: {
bbb: "ccc"
}
};
dispatch(action);
expect(store.getMap("aaa")).to.deep.equal({
bbb: "ccc"
});
});
[true, false, 0, null, undefined].forEach(function (v) {
it("test set empty [" + v + "]", function () {
var [store, dispatch] = reducer;
dispatch(v);
expect(store.getState().toJS()).to.deep.equal({});
});
});
it("test with empty string", function () {
var [store, dispatch] = reducer;
dispatch("");
expect(store.getState().toJS()).to.deep.equal({
type: ""
});
});
});