Skip to content

Commit 0640085

Browse files
fix(store): remove afterEach hook in mock store (#3245)
Closes #3243
1 parent d24dde1 commit 0640085

File tree

11 files changed

+58
-126
lines changed

11 files changed

+58
-126
lines changed

modules/store/testing/spec/mock_store.spec.ts

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -516,106 +516,3 @@ describe('Refreshing state', () => {
516516
expect(getTodoItems('li')[0].nativeElement.textContent.trim()).toBe('bbb');
517517
});
518518
});
519-
520-
describe('Cleans up after each test', () => {
521-
const selectData = createSelector(
522-
(state: any) => state,
523-
(state) => state.value
524-
);
525-
526-
it('should return the mocked selectors value', (done: any) => {
527-
TestBed.configureTestingModule({
528-
providers: [
529-
provideMockStore({
530-
initialState: {
531-
value: 100,
532-
},
533-
selectors: [{ selector: selectData, value: 200 }],
534-
}),
535-
],
536-
});
537-
538-
const store = TestBed.inject(Store);
539-
store.pipe(select(selectData)).subscribe((v) => {
540-
expect(v).toBe(200);
541-
done();
542-
});
543-
});
544-
545-
it('should return the real value', (done: any) => {
546-
TestBed.configureTestingModule({
547-
imports: [
548-
StoreModule.forRoot({} as any, {
549-
initialState: {
550-
value: 300,
551-
},
552-
}),
553-
],
554-
});
555-
556-
const store = TestBed.inject(Store);
557-
store.pipe(select(selectData)).subscribe((v) => {
558-
expect(v).toBe(300);
559-
done();
560-
});
561-
});
562-
});
563-
564-
describe('Resets selectors after each test', () => {
565-
const selectorUnderTest = createSelector(
566-
(state: any) => state,
567-
(state) => state.value
568-
);
569-
let shouldSetMockStore = true;
570-
571-
function setupModules(isMock: boolean) {
572-
if (isMock) {
573-
TestBed.configureTestingModule({
574-
providers: [
575-
provideMockStore({
576-
initialState: {
577-
value: 100,
578-
},
579-
selectors: [{ selector: selectorUnderTest, value: 200 }],
580-
}),
581-
],
582-
});
583-
} else {
584-
TestBed.configureTestingModule({
585-
imports: [
586-
StoreModule.forRoot({} as any, {
587-
initialState: {
588-
value: 300,
589-
},
590-
}),
591-
],
592-
});
593-
}
594-
}
595-
596-
/**
597-
* Tests run in random order, so that's why we have two attempts (one runs
598-
* before another - in any order) and whichever runs the test first would
599-
* setup MockStore and override selector. The next one would use the regular
600-
* Store and verifies that the selector is cleared/reset.
601-
*/
602-
it('should reset selector - attempt one', (done: any) => {
603-
setupModules(shouldSetMockStore);
604-
const store: Store<{}> = TestBed.inject(Store);
605-
store.select(selectorUnderTest).subscribe((v) => {
606-
expect(v).toBe(shouldSetMockStore ? 200 : 300);
607-
shouldSetMockStore = false;
608-
done();
609-
});
610-
});
611-
612-
it('should reset selector - attempt two', (done: any) => {
613-
setupModules(shouldSetMockStore);
614-
const store: Store<{}> = TestBed.inject(Store);
615-
store.select(selectorUnderTest).subscribe((v) => {
616-
expect(v).toBe(shouldSetMockStore ? 200 : 300);
617-
shouldSetMockStore = false;
618-
done();
619-
});
620-
});
621-
});

modules/store/testing/src/mock_store.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Inject, Injectable } from '@angular/core';
2-
import { TestBed } from '@angular/core/testing';
32
import { Observable, BehaviorSubject } from 'rxjs';
43
import {
54
Action,
@@ -15,18 +14,6 @@ import { MockState } from './mock_state';
1514
import { MockSelector } from './mock_selector';
1615
import { MOCK_SELECTORS } from './tokens';
1716

18-
if (typeof afterEach === 'function') {
19-
afterEach(() => {
20-
try {
21-
const mockStore: MockStore | undefined = TestBed.inject(MockStore);
22-
if (mockStore) {
23-
mockStore.resetSelectors();
24-
}
25-
// eslint-disable-next-line no-empty
26-
} catch {}
27-
});
28-
}
29-
3017
type OnlyMemoized<T, Result> = T extends string | MemoizedSelector<any, any>
3118
? MemoizedSelector<any, Result>
3219
: T extends MemoizedSelectorWithProps<any, any, any>

projects/example-app/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ module.exports = {
1414
moduleNameMapper: {
1515
tslib: '<rootDir>../../node_modules/tslib/tslib.es6.js',
1616
},
17+
testRunner: 'jest-circus/runner',
1718
};

projects/example-app/src/app/auth/components/login-form.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('Login Page', () => {
5959
};
6060
instance.form.setValue(credentials);
6161

62-
spyOn(instance.submitted, 'emit');
62+
jest.spyOn(instance.submitted, 'emit');
6363
instance.submit();
6464

6565
expect(instance.submitted.emit).toHaveBeenCalledWith(credentials);

projects/example-app/src/app/auth/containers/login-page.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Login Page', () => {
3030
instance = fixture.componentInstance;
3131
store = TestBed.inject(MockStore);
3232

33-
spyOn(store, 'dispatch');
33+
jest.spyOn(store, 'dispatch');
3434
});
3535

3636
/**

projects/example-app/src/app/auth/effects/auth.effects.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('AuthEffects', () => {
5050
routerService = TestBed.inject(Router);
5151
dialog = TestBed.inject(MatDialog);
5252

53-
spyOn(routerService, 'navigate').and.callThrough();
53+
jest.spyOn(routerService, 'navigate');
5454
});
5555

5656
describe('login$', () => {

projects/example-app/src/app/books/containers/collection-page.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('Collection Page', () => {
4343
instance = fixture.componentInstance;
4444
store = TestBed.inject(MockStore);
4545

46-
spyOn(store, 'dispatch');
46+
jest.spyOn(store, 'dispatch');
4747
});
4848

4949
it('should compile', () => {

projects/example-app/src/app/books/containers/find-book-page.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('Find Book Page', () => {
5656
instance = fixture.componentInstance;
5757
store = TestBed.inject(MockStore);
5858

59-
spyOn(store, 'dispatch');
59+
jest.spyOn(store, 'dispatch');
6060
});
6161

6262
it('should compile', () => {

projects/example-app/src/app/books/containers/selected-book-page.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Selected Book Page', () => {
3535
instance = fixture.componentInstance;
3636
store = TestBed.inject(MockStore);
3737

38-
spyOn(store, 'dispatch');
38+
jest.spyOn(store, 'dispatch');
3939
});
4040

4141
it('should compile', () => {

projects/ngrx.io/content/examples/store-walkthrough/src/app/tests/app.component.1.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MemoizedSelector } from '@ngrx/store';
2+
import { Store, StoreModule } from '@ngrx/store';
23
import { provideMockStore, MockStore } from '@ngrx/store/testing';
34
import { TestBed, ComponentFixture } from '@angular/core/testing';
45
import { HttpClientTestingModule } from '@angular/common/http/testing';
@@ -96,3 +97,51 @@ describe('AppComponent', () => {
9697
//#enddocregion mockSelector
9798
});
9899
});
100+
101+
//#docregion resetMockSelector
102+
describe('AppComponent reset selectors', () => {
103+
let store: MockStore;
104+
105+
afterEach(() => {
106+
store?.resetSelectors();
107+
});
108+
109+
it('should return the mocked value', (done: any) => {
110+
TestBed.configureTestingModule({
111+
providers: [
112+
provideMockStore({
113+
selectors: [
114+
{
115+
selector: selectBooks,
116+
value: [
117+
{
118+
id: 'mockedId',
119+
volumeInfo: {
120+
title: 'Mocked Title',
121+
authors: ['Mocked Author'],
122+
},
123+
},
124+
],
125+
},
126+
],
127+
}),
128+
],
129+
});
130+
131+
store = TestBed.inject(MockStore);
132+
133+
store.select(selectBooks).subscribe((mockBooks) => {
134+
expect(mockBooks).toEqual([
135+
{
136+
id: 'mockedId',
137+
volumeInfo: {
138+
title: 'Mocked Title',
139+
authors: ['Mocked Author'],
140+
},
141+
},
142+
]);
143+
done();
144+
});
145+
});
146+
});
147+
//#enddocregion resetMockSelector

0 commit comments

Comments
 (0)