87 lines
2.4 KiB
JavaScript
87 lines
2.4 KiB
JavaScript
|
import { buildInitialState } from './initialState.js';
|
||
|
import Updux from './Updux.js';
|
||
|
test('default', () => {
|
||
|
const dux = new Updux({});
|
||
|
expect(dux.initialState).toBeTypeOf('object');
|
||
|
expect(dux.initialState).toEqual({});
|
||
|
expectTypeOf(dux.initialState).toEqualTypeOf();
|
||
|
});
|
||
|
test('number', () => {
|
||
|
const dux = new Updux({ initialState: 3 });
|
||
|
expect(dux.initialState).toEqual(3);
|
||
|
expectTypeOf(dux.initialState).toEqualTypeOf();
|
||
|
const f = { initialState: dux.initialState };
|
||
|
});
|
||
|
test('single dux', () => {
|
||
|
const foo = new Updux({
|
||
|
initialState: { a: 1 },
|
||
|
});
|
||
|
expect(foo.initialState).toEqual({ a: 1 });
|
||
|
expectTypeOf(foo.initialState).toEqualTypeOf();
|
||
|
});
|
||
|
test('no initialState for subdux', () => {
|
||
|
const subduxes = {
|
||
|
bar: new Updux({}),
|
||
|
baz: new Updux({ initialState: 'potato' }),
|
||
|
};
|
||
|
const dux = new Updux({
|
||
|
subduxes,
|
||
|
});
|
||
|
expectTypeOf(dux.initialState).toEqualTypeOf();
|
||
|
expect(dux.initialState).toEqual({ bar: {}, baz: 'potato' });
|
||
|
});
|
||
|
test('basic', () => {
|
||
|
expect(buildInitialState({ a: 1 }, { b: { initialState: { c: 2 } }, d: { initialState: 'e' } })).toEqual({
|
||
|
a: 1,
|
||
|
b: { c: 2 },
|
||
|
d: 'e',
|
||
|
});
|
||
|
});
|
||
|
test('throw if subduxes and initialState is not an object', () => {
|
||
|
expect(() => {
|
||
|
buildInitialState(3, { bar: 'foo' });
|
||
|
}).toThrow();
|
||
|
});
|
||
|
const bar = new Updux({ initialState: 123 });
|
||
|
const foo = new Updux({
|
||
|
initialState: { root: 'abc' },
|
||
|
subduxes: {
|
||
|
bar
|
||
|
},
|
||
|
});
|
||
|
test('initialState to createStore', () => {
|
||
|
const initialState = {
|
||
|
a: 1,
|
||
|
b: 2,
|
||
|
};
|
||
|
const dux = new Updux({
|
||
|
initialState,
|
||
|
});
|
||
|
expect(dux.createStore({ preloadedState: { a: 3, b: 4 } }).getState()).toEqual({
|
||
|
a: 3,
|
||
|
b: 4,
|
||
|
});
|
||
|
});
|
||
|
// TODO add 'check for no todo eslint rule'
|
||
|
test('initialState value', () => {
|
||
|
expect(foo.initialState).toEqual({
|
||
|
root: 'abc',
|
||
|
bar: 123,
|
||
|
});
|
||
|
expectTypeOf(foo.initialState.bar).toMatchTypeOf();
|
||
|
expectTypeOf(foo.initialState).toMatchTypeOf();
|
||
|
});
|
||
|
test.todo('splat initialState', async () => {
|
||
|
const bar = new Updux({
|
||
|
initialState: { id: 0 },
|
||
|
});
|
||
|
const foo = new Updux({
|
||
|
subduxes: { '*': bar },
|
||
|
});
|
||
|
expect(foo.initialState).toEqual([]);
|
||
|
expect(new Updux({
|
||
|
initialState: 'overriden',
|
||
|
subduxes: { '*': bar },
|
||
|
}).initialState).toEqual('overriden');
|
||
|
});
|