-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy patheffects_feature_module.spec.ts
More file actions
181 lines (153 loc) · 4.62 KB
/
effects_feature_module.spec.ts
File metadata and controls
181 lines (153 loc) · 4.62 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { vi, type MockInstance } from 'vitest';
import { Injectable, NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import {
Action,
createFeatureSelector,
createSelector,
select,
Store,
StoreModule,
} from '@ngrx/store';
import { firstValueFrom } from 'rxjs';
import { map, take, withLatestFrom } from 'rxjs/operators';
import { Actions, EffectsModule, ofType, createEffect } from '../';
import { FEATURE_EFFECTS_INIT } from '../src/effects_actions';
import { EffectsFeatureModule } from '../src/effects_feature_module';
import { EffectsRootModule } from '../src/effects_root_module';
import { _FEATURE_EFFECTS_INSTANCE_GROUPS } from '../src/tokens';
describe('Effects Feature Module', () => {
describe('when registered', () => {
const sourceA = 'sourceA';
const sourceB = 'sourceB';
const sourceC = 'sourceC';
const effectSourceGroups = [[sourceA], [sourceB], [sourceC]];
let mockEffectSources: { addEffects: MockInstance };
let mockStore: { dispatch: MockInstance };
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{
provide: EffectsRootModule,
useValue: {
addEffects: vi.fn(),
},
},
{
provide: Store,
useValue: {
dispatch: vi.fn(),
},
},
{
provide: _FEATURE_EFFECTS_INSTANCE_GROUPS,
useValue: effectSourceGroups,
},
EffectsFeatureModule,
],
});
mockEffectSources = TestBed.inject<unknown>(EffectsRootModule) as {
addEffects: MockInstance;
};
mockStore = TestBed.inject<unknown>(Store) as {
dispatch: MockInstance;
};
});
it('should add all effects when instantiated', () => {
TestBed.inject(EffectsFeatureModule);
expect(mockEffectSources.addEffects).toHaveBeenCalledWith(sourceA);
expect(mockEffectSources.addEffects).toHaveBeenCalledWith(sourceB);
expect(mockEffectSources.addEffects).toHaveBeenCalledWith(sourceC);
});
it('should dispatch the feature effects init action', () => {
TestBed.inject(EffectsFeatureModule);
expect(mockStore.dispatch).toHaveBeenCalledWith({
type: FEATURE_EFFECTS_INIT,
});
});
});
describe('when registered in a different NgModule from the feature state', () => {
let effects: FeatureEffects;
let store: Store<any>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppModule],
});
effects = TestBed.inject(FeatureEffects);
store = TestBed.inject(Store);
});
it('should have the feature state defined to select from the createEffect', async () => {
const action = { type: 'CREATE_INCREMENT' };
const result = { type: 'CREATE_INCREASE' };
const effectPromise = firstValueFrom(
effects.createEffectWithStore.pipe(take(1))
);
store.dispatch(action);
const res = await effectPromise;
expect(res).toEqual(result);
const data = await firstValueFrom(
store.pipe(select(getCreateDataState), take(1))
);
expect(data).toBe(220);
});
});
});
const FEATURE_KEY = 'feature';
interface State {
FEATURE_KEY: DataState;
}
interface DataState {
data: number;
createData: number;
}
const initialState: DataState = {
data: 100,
createData: 200,
};
function reducer(state: DataState = initialState, action: Action) {
switch (action.type) {
case 'INCREASE':
return {
...state,
data: state.data + 10,
};
case 'CREATE_INCREASE':
return {
...state,
createData: state.createData + 20,
};
}
return state;
}
const getFeatureState = createFeatureSelector<DataState>(FEATURE_KEY);
const getDataState = createSelector(getFeatureState, (state) => state.data);
const getCreateDataState = createSelector(
getFeatureState,
(state) => state.createData
);
@Injectable()
class FeatureEffects {
constructor(
private actions: Actions,
private store: Store<State>
) {}
createEffectWithStore = createEffect(() =>
this.actions.pipe(
ofType('CREATE_INCREMENT'),
withLatestFrom(this.store.select(getDataState)),
map(([action, state]) => ({ type: 'CREATE_INCREASE' }))
)
);
}
@NgModule({
imports: [EffectsModule.forFeature([FeatureEffects])],
})
class FeatureEffectsModule {}
@NgModule({
imports: [FeatureEffectsModule, StoreModule.forFeature(FEATURE_KEY, reducer)],
})
class FeatureModule {}
@NgModule({
imports: [StoreModule.forRoot({}), EffectsModule.forRoot([]), FeatureModule],
})
class AppModule {}