2019-10-22 22:15:08 +00:00
|
|
|
"use strict";
|
2019-10-24 15:52:36 +00:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const _1 = __importDefault(require("."));
|
2019-10-22 22:15:08 +00:00
|
|
|
test('actions from mutations', () => {
|
2019-11-05 01:34:14 +00:00
|
|
|
const { actions: { foo, bar }, } = new _1.default({
|
2019-10-24 15:52:36 +00:00
|
|
|
mutations: {
|
|
|
|
foo: () => (x) => x,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(foo()).toEqual({ type: 'foo' });
|
|
|
|
expect(foo(true)).toEqual({ type: 'foo', payload: true });
|
|
|
|
expect(foo({ bar: 2 }, { timestamp: 613 })).toEqual({
|
|
|
|
type: 'foo',
|
|
|
|
payload: { bar: 2 },
|
|
|
|
meta: { timestamp: 613 },
|
|
|
|
});
|
2019-10-22 22:15:08 +00:00
|
|
|
});
|
|
|
|
test('reducer', () => {
|
2019-11-05 01:34:14 +00:00
|
|
|
const { actions, reducer } = new _1.default({
|
2019-10-24 15:52:36 +00:00
|
|
|
initial: { counter: 1 },
|
|
|
|
mutations: {
|
|
|
|
inc: () => ({ counter }) => ({ counter: counter + 1 }),
|
|
|
|
},
|
|
|
|
});
|
2019-10-29 15:34:53 +00:00
|
|
|
let state = reducer(undefined, { type: 'noop' });
|
2019-10-24 15:52:36 +00:00
|
|
|
expect(state).toEqual({ counter: 1 });
|
|
|
|
state = reducer(state, actions.inc());
|
|
|
|
expect(state).toEqual({ counter: 2 });
|
2019-10-22 22:15:08 +00:00
|
|
|
});
|
|
|
|
test('sub reducers', () => {
|
2019-11-05 01:34:14 +00:00
|
|
|
const foo = new _1.default({
|
2019-10-24 15:52:36 +00:00
|
|
|
initial: 1,
|
|
|
|
mutations: {
|
|
|
|
doFoo: () => (x) => x + 1,
|
|
|
|
doAll: () => (x) => x + 10,
|
|
|
|
},
|
|
|
|
});
|
2019-11-05 01:34:14 +00:00
|
|
|
const bar = new _1.default({
|
2019-10-24 15:52:36 +00:00
|
|
|
initial: 'a',
|
|
|
|
mutations: {
|
|
|
|
doBar: () => (x) => x + 'a',
|
|
|
|
doAll: () => (x) => x + 'b',
|
|
|
|
}
|
|
|
|
});
|
2019-11-05 01:34:14 +00:00
|
|
|
const { initial, actions, reducer } = new _1.default({
|
2019-10-24 15:52:36 +00:00
|
|
|
subduxes: {
|
|
|
|
foo, bar
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(initial).toEqual({ foo: 1, bar: 'a' });
|
|
|
|
expect(Object.keys(actions)).toHaveLength(3);
|
2019-10-29 15:35:13 +00:00
|
|
|
let state = reducer(undefined, { type: 'noop' });
|
2019-10-24 15:52:36 +00:00
|
|
|
expect(state).toEqual({ foo: 1, bar: 'a' });
|
|
|
|
state = reducer(state, actions.doFoo());
|
|
|
|
expect(state).toEqual({ foo: 2, bar: 'a' });
|
|
|
|
state = reducer(state, actions.doBar());
|
|
|
|
expect(state).toEqual({ foo: 2, bar: 'aa' });
|
|
|
|
state = reducer(state, actions.doAll());
|
|
|
|
expect(state).toEqual({ foo: 12, bar: 'aab' });
|
2019-10-22 22:15:08 +00:00
|
|
|
});
|
|
|
|
test('precedence between root and sub-reducers', () => {
|
2019-11-05 01:34:14 +00:00
|
|
|
const { initial, reducer, actions, } = new _1.default({
|
2019-10-22 22:15:08 +00:00
|
|
|
initial: {
|
2019-10-24 15:52:36 +00:00
|
|
|
foo: { bar: 4 },
|
2019-10-22 22:15:08 +00:00
|
|
|
},
|
|
|
|
mutations: {
|
2019-10-24 15:52:36 +00:00
|
|
|
inc: () => (state) => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
surprise: state.foo.bar
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
subduxes: {
|
2019-11-05 01:34:14 +00:00
|
|
|
foo: {
|
2019-10-24 15:52:36 +00:00
|
|
|
initial: {
|
|
|
|
bar: 2,
|
|
|
|
quux: 3,
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
inc: () => (state) => ({ ...state, bar: state.bar + 1 })
|
|
|
|
},
|
2019-11-05 01:34:14 +00:00
|
|
|
},
|
2019-10-22 22:15:08 +00:00
|
|
|
}
|
2019-10-24 15:52:36 +00:00
|
|
|
});
|
|
|
|
expect(initial).toEqual({
|
|
|
|
foo: { bar: 4, quux: 3 }
|
|
|
|
});
|
2019-10-29 15:34:53 +00:00
|
|
|
expect(reducer(undefined, actions.inc())).toEqual({
|
2019-10-24 15:52:36 +00:00
|
|
|
foo: { bar: 5, quux: 3 }, surprise: 5
|
|
|
|
});
|
2019-10-22 22:15:08 +00:00
|
|
|
});
|
|
|
|
function timeout(ms) {
|
2019-10-24 15:52:36 +00:00
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
2019-10-22 22:15:08 +00:00
|
|
|
}
|
|
|
|
test('middleware', async () => {
|
2019-11-05 01:34:14 +00:00
|
|
|
const { middleware, createStore } = new _1.default({
|
2019-10-24 15:52:36 +00:00
|
|
|
initial: "",
|
|
|
|
mutations: {
|
|
|
|
inc: (addition) => (state) => state + addition,
|
|
|
|
doEeet: () => (state) => {
|
|
|
|
return state + 'Z';
|
|
|
|
},
|
|
|
|
},
|
2019-10-22 22:15:08 +00:00
|
|
|
effects: {
|
2019-10-24 15:52:36 +00:00
|
|
|
doEeet: api => next => async (action) => {
|
|
|
|
api.dispatch.inc('a');
|
|
|
|
next(action);
|
|
|
|
await timeout(1000);
|
|
|
|
api.dispatch.inc('c');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
subduxes: {
|
2019-11-05 01:34:14 +00:00
|
|
|
foo: {
|
2019-10-24 15:52:36 +00:00
|
|
|
effects: {
|
2019-11-05 01:34:14 +00:00
|
|
|
doEeet: (api) => (next) => (action) => {
|
2019-10-24 15:52:36 +00:00
|
|
|
api.dispatch({ type: 'inc', payload: 'b' });
|
|
|
|
next(action);
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 01:34:14 +00:00
|
|
|
},
|
2019-10-22 22:15:08 +00:00
|
|
|
}
|
2019-10-24 15:52:36 +00:00
|
|
|
});
|
|
|
|
const store = createStore();
|
|
|
|
store.dispatch.doEeet();
|
|
|
|
expect(store.getState()).toEqual('abZ');
|
|
|
|
await timeout(1000);
|
|
|
|
expect(store.getState()).toEqual('abZc');
|
|
|
|
});
|
2019-11-05 01:34:14 +00:00
|
|
|
test("subduxes and mutations", () => {
|
|
|
|
const foo = new _1.default({ mutations: {
|
|
|
|
quux: () => () => 'x',
|
|
|
|
blart: () => () => 'a',
|
|
|
|
} });
|
|
|
|
const bar = new _1.default({ mutations: {
|
|
|
|
quux: () => () => 'y'
|
|
|
|
} });
|
|
|
|
const baz = new _1.default({
|
|
|
|
mutations: {
|
|
|
|
quux: () => (state) => ({ ...state, "baz": "z" })
|
|
|
|
}, subduxes: { foo, bar }
|
|
|
|
});
|
|
|
|
let state = baz.reducer(undefined, baz.actions.quux());
|
|
|
|
expect(state).toEqual({
|
|
|
|
foo: "x",
|
|
|
|
bar: "y",
|
|
|
|
baz: "z",
|
|
|
|
});
|
|
|
|
});
|
2019-10-24 15:52:36 +00:00
|
|
|
//# sourceMappingURL=test.js.map
|