Skip to content

Commit

Permalink
test(store-testing): nextMock and spyOnDispatcher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrstaniow committed Aug 29, 2018
1 parent a05a577 commit 6eb8102
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
44 changes: 44 additions & 0 deletions modules/store/spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import Spy = jasmine.Spy;
import any = jasmine.any;
import { take } from 'rxjs/operators';
import { MockStore, provideMockStore } from '../testing';

interface TestAppSchema {
counter1: number;
Expand Down Expand Up @@ -432,4 +433,47 @@ describe('ngRx Store', () => {
};
}
});

describe('State mocking', () => {
let mockStore: MockStore<TestAppSchema>;
beforeEach(() => {
const initialState = { counter1: 0, counter2: 1 };
const reducers = {
counter1: counterReducer,
counter2: counterReducer,
counter3: counterReducer,
};

TestBed.configureTestingModule({
imports: [StoreModule.forRoot(reducers, { initialState })],
providers: [provideMockStore()],
});

mockStore = TestBed.get(Store);
});

it('should set the initial state to a mocked one', (done: DoneFn) => {
const fixedState = {
counter1: 17,
counter2: 11,
counter3: 25,
};
mockStore.nextMock(fixedState);
mockStore.pipe(take(1)).subscribe({
next(val) {
expect(val).toEqual(fixedState);
},
error: done.fail,
complete: done,
});
});

it('should set a spy on dispatcher', () => {
const spyFactory = () => jasmine.createSpy('dispatcher');
const action = { type: INCREMENT };
mockStore.spyOnDispatch(spyFactory);
mockStore.dispatch(action);
expect(mockStore.dispatch).toHaveBeenCalledWith(action);
});
});
});
6 changes: 5 additions & 1 deletion modules/store/testing/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export class MockStore<T> extends Store<T> {
this.source = this.stateSubject.asObservable();
}

nextMock(nextState: T) {
nextMock(nextState: T): void {
this.stateSubject.next(nextState);
}

spyOnDispatch(spyFactory: Function): void {
this.dispatch = spyFactory(this.dispatch);
}
}

export function provideMockStore() {
Expand Down

0 comments on commit 6eb8102

Please sign in to comment.