-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathsub-store.spec.ts
113 lines (98 loc) · 3.37 KB
/
sub-store.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { NgZone } from '@angular/core';
import { Action, AnyAction } from 'redux';
import { RootStore } from './root-store';
import { NgRedux } from './ng-redux';
import { ObservableStore } from './observable-store';
import { take, toArray } from 'rxjs/operators';
class MockNgZone extends NgZone {
run<T>(fn: (...args: any[]) => T): T {
return fn() as T;
}
}
interface ISubState {
wat: {
quux: number;
};
}
interface IAppState {
foo: {
bar: ISubState;
};
}
describe('Substore', () => {
const defaultReducer = (state: any, _: Action) => state;
const basePath = ['foo', 'bar'];
let ngRedux: NgRedux<IAppState>;
let subStore: ObservableStore<ISubState>;
beforeEach(() => {
ngRedux = new RootStore<IAppState>(new MockNgZone({
enableLongStackTrace: false,
}) as NgZone);
ngRedux.configureStore(defaultReducer, {
foo: {
bar: { wat: { quux: 3 } },
},
});
subStore = ngRedux.configureSubStore<ISubState>(basePath, defaultReducer);
});
it('adds a key to actions it dispatches', () =>
expect(subStore.dispatch<AnyAction>({ type: 'MY_ACTION' })).toEqual({
type: 'MY_ACTION',
'@angular-redux::fractalkey': '["foo","bar"]',
}));
it('gets state rooted at the base path', () =>
expect(subStore.getState()).toEqual({ wat: { quux: 3 } }));
it('selects based on base path', () =>
subStore.select('wat').subscribe(wat => expect(wat).toEqual({ quux: 3 })));
it(`handles property selection on a base path that doesn't exist yet`, () => {
const nonExistentSubStore = ngRedux.configureSubStore(
['sure', 'whatever'],
(state: any, action: any) => ({ ...state, value: action.newValue })
);
nonExistentSubStore
.select<any>('value')
.pipe(take(2), toArray())
.subscribe(v => expect(v).toEqual([undefined, 'now I exist']));
nonExistentSubStore.dispatch<AnyAction>({
type: 'nvm',
newValue: 'now I exist',
});
});
it(`handles path selection on a base path that doesn't exist yet`, () => {
const nonExistentSubStore = ngRedux.configureSubStore(
['sure', 'whatever'],
(state: any, action: any) => ({ ...state, value: action.newValue })
);
nonExistentSubStore
.select<any>(['value'])
.pipe(take(2), toArray())
.subscribe(v => expect(v).toEqual([undefined, 'now I exist']));
nonExistentSubStore.dispatch<AnyAction>({
type: 'nvm',
newValue: 'now I exist',
});
});
it(`handles function selection on a base path that doesn't exist yet`, () => {
const nonExistentSubStore = ngRedux.configureSubStore(
['sure', 'whatever'],
(state: any, action: any) => ({ ...state, value: action.newValue })
);
nonExistentSubStore
.select(s => (s ? s.value : s))
.pipe(take(2), toArray())
.subscribe(v => expect(v).toEqual([undefined, 'now I exist']));
nonExistentSubStore.dispatch<AnyAction>({
type: 'nvm',
newValue: 'now I exist',
});
});
it('can create its own sub-store', () => {
const subSubStore = subStore.configureSubStore(['wat'], defaultReducer);
expect(subSubStore.getState()).toEqual({ quux: 3 });
subSubStore.select('quux').subscribe(quux => expect(quux).toEqual(3));
expect(subSubStore.dispatch<AnyAction>({ type: 'MY_ACTION' })).toEqual({
type: 'MY_ACTION',
'@angular-redux::fractalkey': '["foo","bar","wat"]',
});
});
});