ducks
Version:
🦆🦆🦆 Ducks is a Reducer Bundles Manager that Implementing the Redux Ducks Modular Proposal with Great Convenience.
98 lines • 4.82 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Ducks - https://github.com/huan/ducks
*
* @copyright 2020 Huan LI (李卓桓) <https://github.com/huan>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const tstest_1 = require("tstest");
const counterDuck = __importStar(require("../../examples/counter/mod.js"));
const switcherDuck = __importStar(require("../../examples/switcher/mod.js"));
const combine_duckery_js_1 = require("./combine-duckery.js");
const insert_reducers_js_1 = require("./insert-reducers.js");
/**
* Insert switcher to counter
*/
(0, tstest_1.test)('insertReducers(counter, switcher)', async (t) => {
const originalReducer = (0, combine_duckery_js_1.combineDuckery)({
counter: counterDuck,
});
const insertReducer = {
switcher: switcherDuck.default,
};
const newReducer = (0, insert_reducers_js_1.insertReducers)(originalReducer, insertReducer);
// reduce with a unknown type action should get the default state
const state0 = newReducer(undefined, { type: 'NOOP' });
t.equal(state0.counter.total, 0, 'should get state.count.total = 0 on initialization');
t.equal(state0.switcher.status, false, 'should get state.switcher.status = false on initialization');
const state1 = newReducer(state0, counterDuck.actions.tap());
t.not(state1, state0, 'should not mutate the state 0');
/**
* We have lost the typing information from the switcher Duck
* because the Reducer is using the AnyAction
*/
const state2 = newReducer(state1, switcherDuck.actions.toggle());
t.not(state2, state1, 'should not mutate the state 1');
t.equal(state2.counter.total, 1, 'should get state.count.total = 1 after tap()');
t.equal(state2.switcher.status, true, 'should get state.switcher.status = true after toggle()');
const state3 = newReducer(state2, { type: 'NOOP' });
t.equal(state3, state2, 'state should not change if reducer got unknown action type');
});
/**
* Insert counter to switcher
*/
(0, tstest_1.test)('insertReducers(switcher, counter)', async (t) => {
const originalReducer = (0, combine_duckery_js_1.combineDuckery)({
switcher: switcherDuck,
});
const insertReducer = {
counter: counterDuck.default,
};
const newReducer = (0, insert_reducers_js_1.insertReducers)(originalReducer, insertReducer);
// reduce with a unknown type action should get the default state
const state0 = newReducer(undefined, { type: 'NOOP' });
t.equal(state0.counter.total, 0, 'should get state.count.total = 0 on initialization');
t.equal(state0.switcher.status, false, 'should get state.switcher.status = false on initialization');
const state1 = newReducer(state0, counterDuck.actions.tap());
t.not(state1, state0, 'should not mutate the state 0');
const state2 = newReducer(state1, switcherDuck.actions.toggle());
t.not(state2, state1, 'should not mutate the state 1');
t.equal(state2.counter.total, 1, 'should get state.count.total = 1 after tap()');
t.equal(state2.switcher.status, true, 'should get state.switcher.status = true after toggle()');
const state3 = newReducer(state2, { type: 'NOOP' });
t.equal(state3, state2, 'state should not change if reducer got unknown action type');
});
//# sourceMappingURL=insert-reducers.spec.js.map