typedux
Version:
Slightly adjusted Redux (awesome by default) for TS
111 lines • 4.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
require("source-map-support/register");
require("jest");
const logger_proxy_1 = require("@3fv/logger-proxy");
const PropertyChain_1 = require("../util/PropertyChain");
const SelectorChain_1 = require("../selectors/SelectorChain");
const ObservableStore_1 = require("../store/ObservableStore");
const MockConstants_1 = require("./mocks/MockConstants");
const MockLeafState_1 = require("./mocks/MockLeafState");
const Actions_1 = require("../actions/Actions");
const MockActionFactory_1 = require("./mocks/MockActionFactory");
const dev_1 = require("../dev");
require("source-map-support").install();
//installMockStoreProvider()
const log = logger_proxy_1.getLogger(__filename);
describe('#selectors', function () {
//jest.setTimeout(10000)
jest.setTimeout(10000);
let reducer, leafReducers, leafReducer, store = null, actions;
beforeEach(() => {
// leafReducers = ObservableStore.makeSimpleReducers({type: MockKey, str1: MockStateStr1})//new MockLeafReducer()
// ROOT REDUCER
//reducer = createMockRootReducer(ObservableStore.makeInternalReducer(),leafReducer)
// STORE
store = ObservableStore_1.ObservableStore.createObservableStore(ObservableStore_1.ObservableStore.makeSimpleReducers(new MockLeafState_1.MockLeafState()), undefined, undefined,
//getDefaultMockState(reducer),
{
mock: new MockLeafState_1.MockLeafState()
});
Actions_1.setGlobalStore(store);
// INIT
//store.dispatch({type:'@INIT'})
// ACTIONS
actions = new MockActionFactory_1.MockActionFactory().setStore(store);
});
it('Selector subscribe', () => {
let count = 0;
SelectorChain_1.selectorChain(store, null)
.mock
.str1()
.subscribe((newValue, oldValue) => {
//console.log("new", newValue, "old", oldValue)
if (!count) {
expect(newValue).toBe(MockConstants_1.MockStateStr1);
}
else {
expect(newValue).toBe("cool");
expect(oldValue).toBe(MockConstants_1.MockStateStr1);
}
count++;
});
actions.mockUpdate(MockConstants_1.MockStateStr1);
actions.mockUpdate("cool");
});
it('Selector results in simple selector', () => {
const o = {
type: "dummyState",
a: {
b: {
c: "hello",
d: [1, 2, 3]
}
}
},
//store = configureMockStore()(o),
selectorChainTests = [
[
SelectorChain_1.selectorChain(store, o).a.b.c(),
o.a.b.c,
["a", "b", "c"]
]
], propChainTests = [
[
PropertyChain_1.propertyChain(o).a.b.c((getter, keyPath) => [getter(o), keyPath]),
o.a.b.c,
["a", "b", "c"]
],
[
PropertyChain_1.propertyChain(o).a.b.d((getter, keyPath) => [getter(o), keyPath]),
o.a.b.d,
["a", "b", "d"]
],
[
PropertyChain_1.propertyChain(o).a.b.d[0]((getter, keyPath) => [getter(o), keyPath]),
o.a.b.d[0],
["a", "b", "d", 0]
]
];
//const selector = selectorChain(o).a.b.c()
for (const [[value, keyPath], testValue, testKeyPath] of propChainTests) {
if (log.isDebugEnabled() && dev_1.isDev) {
log.debug("Key path", keyPath, "value", value);
}
expect(value).toBe(testValue);
expect(keyPath).toMatchObject(testKeyPath);
}
for (const [selector, testValue] of selectorChainTests) {
//const [value, keyPath] = chain()
const value = selector(o);
//log.info("Key path", keyPath, "value", value)
expect(value).toBe(testValue);
//expect(keyPath).to.deep.equal(testKeyPath)
}
// Make sure the children changed too
// let mockStateAfter = actions.state
// expect(mockState).not.to.equal(mockStateAfter)
// expect(mockStr1Update).to.equal(mockStateAfter.str1)
});
});
//# sourceMappingURL=PropertyChain.spec.js.map
;