Skip to content

Commit 59a9e6c

Browse files
timdeschryverbrandonroberts
authored andcommitted
revert(store): store should fail synchronously (#1871)
Closes #1865 * revert(store): should fail synchronously This commit partially reverts commit 60633b7 * test(store): should fail synchronously
1 parent 4a5153c commit 59a9e6c

File tree

3 files changed

+124
-115
lines changed

3 files changed

+124
-115
lines changed

modules/store/spec/runtime_checks.spec.ts

Lines changed: 93 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ngCore from '@angular/core';
2-
import { TestBed } from '@angular/core/testing';
2+
import { TestBed, fakeAsync, flush } from '@angular/core/testing';
33
import { Store, StoreModule, META_REDUCERS } from '..';
44
import { createActiveRuntimeChecks } from '../src/runtime_checks';
55
import { RuntimeChecks } from '../src/models';
@@ -113,32 +113,29 @@ describe('Runtime checks:', () => {
113113
describe('State Serialization:', () => {
114114
const invalidAction = () => ({ type: ErrorTypes.UnserializableState });
115115

116-
it('should throw when enabled', (done: DoneFn) => {
117-
const store = setupStore({ strictStateSerializability: true });
118-
119-
store.subscribe({
120-
error: err => {
121-
expect(err).toMatch(/Detected unserializable state/);
122-
done();
123-
},
124-
});
125-
126-
store.dispatch(invalidAction());
127-
});
128-
129-
it('should not throw when disabled', (done: DoneFn) => {
130-
const store = setupStore({ strictStateSerializability: false });
131-
132-
store.subscribe({
133-
next: ({ state }) => {
134-
if (state.invalidSerializationState) {
135-
done();
136-
}
137-
},
138-
});
139-
140-
store.dispatch(invalidAction());
141-
});
116+
it(
117+
'should throw when enabled',
118+
fakeAsync(() => {
119+
const store = setupStore({ strictStateSerializability: true });
120+
121+
expect(() => {
122+
store.dispatch(invalidAction());
123+
flush();
124+
}).toThrowError(/Detected unserializable state/);
125+
})
126+
);
127+
128+
it(
129+
'should not throw when disabled',
130+
fakeAsync(() => {
131+
const store = setupStore({ strictStateSerializability: false });
132+
133+
expect(() => {
134+
store.dispatch(invalidAction());
135+
flush();
136+
}).not.toThrow();
137+
})
138+
);
142139
});
143140

144141
describe('Action Serialization:', () => {
@@ -147,63 +144,59 @@ describe('Runtime checks:', () => {
147144
invalid: new Date(),
148145
});
149146

150-
it('should throw when enabled', (done: DoneFn) => {
151-
const store = setupStore({ strictActionSerializability: true });
152-
153-
store.subscribe({
154-
error: err => {
155-
expect(err).toMatch(/Detected unserializable action/);
156-
done();
157-
},
158-
});
159-
store.dispatch(invalidAction());
160-
});
161-
162-
it('should not throw when disabled', (done: DoneFn) => {
163-
const store = setupStore({ strictActionSerializability: false });
164-
165-
store.subscribe({
166-
next: ({ state }) => {
167-
if (state.invalidSerializationAction) {
168-
done();
169-
}
170-
},
171-
});
172-
173-
store.dispatch(invalidAction());
174-
});
147+
it(
148+
'should throw when enabled',
149+
fakeAsync(() => {
150+
const store = setupStore({ strictActionSerializability: true });
151+
152+
expect(() => {
153+
store.dispatch(invalidAction());
154+
flush();
155+
}).toThrowError(/Detected unserializable action/);
156+
})
157+
);
158+
159+
it(
160+
'should not throw when disabled',
161+
fakeAsync(() => {
162+
const store = setupStore({ strictActionSerializability: false });
163+
164+
expect(() => {
165+
store.dispatch(invalidAction());
166+
flush();
167+
}).not.toThrow();
168+
})
169+
);
175170
});
176171

177172
describe('State Mutations', () => {
178173
const invalidAction = () => ({
179174
type: ErrorTypes.MutateState,
180175
});
181176

182-
it('should throw when enabled', (done: DoneFn) => {
183-
const store = setupStore({ strictImmutability: true });
184-
185-
store.subscribe({
186-
error: _ => {
187-
done();
188-
},
189-
});
190-
191-
store.dispatch(invalidAction());
192-
});
193-
194-
it('should not throw when disabled', (done: DoneFn) => {
195-
const store = setupStore({ strictImmutability: false });
196-
197-
store.subscribe({
198-
next: ({ state }) => {
199-
if (state.invalidMutationState) {
200-
done();
201-
}
202-
},
203-
});
204-
205-
store.dispatch(invalidAction());
206-
});
177+
it(
178+
'should throw when enabled',
179+
fakeAsync(() => {
180+
const store = setupStore({ strictImmutability: true });
181+
182+
expect(() => {
183+
store.dispatch(invalidAction());
184+
flush();
185+
}).toThrowError(/Cannot add property/);
186+
})
187+
);
188+
189+
it(
190+
'should not throw when disabled',
191+
fakeAsync(() => {
192+
const store = setupStore({ strictImmutability: false });
193+
194+
expect(() => {
195+
store.dispatch(invalidAction());
196+
flush();
197+
}).not.toThrow();
198+
})
199+
);
207200
});
208201

209202
describe('Action Mutations', () => {
@@ -212,31 +205,29 @@ describe('Runtime checks:', () => {
212205
foo: 'foo',
213206
});
214207

215-
it('should throw when enabled', (done: DoneFn) => {
216-
const store = setupStore({ strictImmutability: true });
217-
218-
store.subscribe({
219-
error: _ => {
220-
done();
221-
},
222-
});
223-
224-
store.dispatch(invalidAction());
225-
});
226-
227-
it('should not throw when disabled', (done: DoneFn) => {
228-
const store = setupStore({ strictImmutability: false });
229-
230-
store.subscribe({
231-
next: ({ state }) => {
232-
if (state.invalidMutationAction) {
233-
done();
234-
}
235-
},
236-
});
237-
238-
store.dispatch(invalidAction());
239-
});
208+
it(
209+
'should throw when enabled',
210+
fakeAsync(() => {
211+
const store = setupStore({ strictImmutability: true });
212+
213+
expect(() => {
214+
store.dispatch(invalidAction());
215+
flush();
216+
}).toThrowError(/Cannot assign to read only property/);
217+
})
218+
);
219+
220+
it(
221+
'should not throw when disabled',
222+
fakeAsync(() => {
223+
const store = setupStore({ strictImmutability: false });
224+
225+
expect(() => {
226+
store.dispatch(invalidAction());
227+
flush();
228+
}).not.toThrow();
229+
})
230+
);
240231
});
241232
});
242233

modules/store/spec/state.spec.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { TestBed } from '@angular/core/testing';
2-
import { INIT, Store, StoreModule } from '@ngrx/store';
1+
import { TestBed, fakeAsync, flush } from '@angular/core/testing';
2+
import { INIT, Store, StoreModule, Action } from '@ngrx/store';
33

44
describe('ngRx State', () => {
5-
const initialState = 123;
6-
const reducer = jasmine.createSpy('reducer').and.returnValue(initialState);
5+
it('should call the reducer to scan over the dispatcher', () => {
6+
const initialState = 123;
7+
const reducer = jasmine.createSpy('reducer').and.returnValue(initialState);
78

8-
beforeEach(() => {
99
TestBed.configureTestingModule({
1010
imports: [
1111
StoreModule.forRoot(
@@ -14,13 +14,34 @@ describe('ngRx State', () => {
1414
),
1515
],
1616
});
17-
});
1817

19-
it('should call the reducer to scan over the dispatcher', function() {
2018
TestBed.get(Store);
2119

2220
expect(reducer).toHaveBeenCalledWith(initialState, {
2321
type: INIT,
2422
});
2523
});
24+
25+
it(
26+
'should fail synchronously',
27+
fakeAsync(() => {
28+
function reducer(state: any, action: Action) {
29+
if (action.type === 'THROW_ERROR') {
30+
throw new Error('(╯°□°)╯︵ ┻━┻');
31+
}
32+
33+
return state;
34+
}
35+
36+
TestBed.configureTestingModule({
37+
imports: [StoreModule.forRoot({ reducer })],
38+
});
39+
40+
const store = TestBed.get(Store) as Store<any>;
41+
expect(() => {
42+
store.dispatch({ type: 'THROW_ERROR' });
43+
flush();
44+
}).toThrow();
45+
})
46+
);
2647
});

modules/store/src/state.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ export class State<T> extends BehaviorSubject<any> implements OnDestroy {
4747
)
4848
);
4949

50-
this.stateSubscription = stateAndAction$.subscribe({
51-
next: ({ state, action }) => {
52-
this.next(state);
53-
scannedActions.next(action);
54-
},
55-
error: err => this.error(err),
50+
this.stateSubscription = stateAndAction$.subscribe(({ state, action }) => {
51+
this.next(state);
52+
scannedActions.next(action);
5653
});
5754
}
5855

0 commit comments

Comments
 (0)