diff --git a/modules/store/spec/fixtures/todos.ts b/modules/store/spec/fixtures/todos.ts index d00a905e1e..de5435ea05 100644 --- a/modules/store/spec/fixtures/todos.ts +++ b/modules/store/spec/fixtures/todos.ts @@ -11,6 +11,10 @@ export const COMPLETE_ALL_TODOS = 'COMPLETE_ALL_TODOS'; let _id = 0; +export function resetId() { + _id = 0; +} + export const VisibilityFilters = { SHOW_ALL: 'SHOW_ALL', SHOW_COMPLETED: 'SHOW_COMPLETED', diff --git a/modules/store/spec/integration.spec.ts b/modules/store/spec/integration.spec.ts index a6fa363d01..9958afd561 100644 --- a/modules/store/spec/integration.spec.ts +++ b/modules/store/spec/integration.spec.ts @@ -9,7 +9,7 @@ import { createSelector, } from '@ngrx/store'; import { combineLatest } from 'rxjs'; -import { first } from 'rxjs/operators'; +import { first, toArray, take } from 'rxjs/operators'; import { INITIAL_STATE, ReducerManager, State } from '../src/private_export'; import { @@ -20,12 +20,13 @@ import { todos, visibilityFilter, VisibilityFilters, + resetId, } from './fixtures/todos'; import { RouterTestingModule, SpyNgModuleFactoryLoader, } from '@angular/router/testing'; -import { Component, NgModuleFactoryLoader, NgModule } from '@angular/core'; +import { NgModuleFactoryLoader, NgModule } from '@angular/core'; import { Router } from '@angular/router'; interface Todo { @@ -54,6 +55,7 @@ describe('ngRx Integration spec', () => { }; beforeEach(() => { + resetId(); spyOn(reducers, 'todos').and.callThrough(); TestBed.configureTestingModule({ @@ -183,7 +185,7 @@ describe('ngRx Integration spec', () => { expect(currentlyVisibleTodos.length).toBe(0); }); - it('should use props to get a todo', () => { + it('should use props to get a todo', (done: DoneFn) => { const getTodosById = createSelector( (state: TodoAppSchema) => state.todos, (todos: Todo[], id: number) => { @@ -191,26 +193,28 @@ describe('ngRx Integration spec', () => { } ); - let testCase = 1; const todo$ = store.select(getTodosById, 2); - todo$.subscribe(todo => { - if (testCase === 1) { - expect(todo).toEqual(undefined); - } else if (testCase === 2) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: false, - }); - } else if (testCase === 3) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: true, - }); - } - testCase++; - }); + todo$ + .pipe( + take(3), + toArray() + ) + .subscribe(res => { + expect(res).toEqual([ + undefined, + { + id: 2, + text: 'second todo', + completed: false, + }, + { + id: 2, + text: 'second todo', + completed: true, + }, + ]); + done(); + }); store.dispatch({ type: ADD_TODO, payload: { text: 'first todo' } }); store.dispatch({ type: ADD_TODO, payload: { text: 'second todo' } }); @@ -220,7 +224,7 @@ describe('ngRx Integration spec', () => { }); }); - it('should use the selector and props to get a todo', () => { + it('should use the selector and props to get a todo', (done: DoneFn) => { const getTodosState = createFeatureSelector( 'todos' ); @@ -231,26 +235,20 @@ describe('ngRx Integration spec', () => { (todos, id) => todos.find(todo => todo.id === id) ); - let testCase = 1; const todo$ = store.select(getTodosById, 2); - todo$.subscribe(todo => { - if (testCase === 1) { - expect(todo).toEqual(undefined); - } else if (testCase === 2) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: false, - }); - } else if (testCase === 3) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: true, - }); - } - testCase++; - }); + todo$ + .pipe( + take(3), + toArray() + ) + .subscribe(res => { + expect(res).toEqual([ + undefined, + { id: 2, text: 'second todo', completed: false }, + { id: 2, text: 'second todo', completed: true }, + ]); + done(); + }); store.dispatch({ type: ADD_TODO, payload: { text: 'first todo' } }); store.dispatch({ type: ADD_TODO, payload: { text: 'second todo' } }); @@ -324,7 +322,7 @@ describe('ngRx Integration spec', () => { expect(currentlyVisibleTodos.length).toBe(0); }); - it('should use the selector and props to get a todo', () => { + it('should use the selector and props to get a todo', (done: DoneFn) => { const getTodosState = createFeatureSelector( 'todos' ); @@ -335,26 +333,20 @@ describe('ngRx Integration spec', () => { (todos, id) => todos.find(todo => todo.id === id) ); - let testCase = 1; const todo$ = store.pipe(select(getTodosById, 2)); - todo$.subscribe(todo => { - if (testCase === 1) { - expect(todo).toEqual(undefined); - } else if (testCase === 2) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: false, - }); - } else if (testCase === 3) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: true, - }); - } - testCase++; - }); + todo$ + .pipe( + take(3), + toArray() + ) + .subscribe(res => { + expect(res).toEqual([ + undefined, + { id: 2, text: 'second todo', completed: false }, + { id: 2, text: 'second todo', completed: true }, + ]); + done(); + }); store.dispatch({ type: ADD_TODO, payload: { text: 'first todo' } }); store.dispatch({ type: ADD_TODO, payload: { text: 'second todo' } }); @@ -364,7 +356,7 @@ describe('ngRx Integration spec', () => { }); }); - it('should use the props in the projector to get a todo', () => { + it('should use the props in the projector to get a todo', (done: DoneFn) => { const getTodosState = createFeatureSelector( 'todos' ); @@ -375,26 +367,28 @@ describe('ngRx Integration spec', () => { todos.find(todo => todo.id === id) ); - let testCase = 1; const todo$ = store.pipe(select(getTodosById, { id: 2 })); - todo$.subscribe(todo => { - if (testCase === 1) { - expect(todo).toEqual(undefined); - } else if (testCase === 2) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: false, - }); - } else if (testCase === 3) { - expect(todo).toEqual({ - id: 2, - text: 'second todo', - completed: true, - }); - } - testCase++; - }); + todo$ + .pipe( + take(3), + toArray() + ) + .subscribe(res => { + expect(res).toEqual([ + undefined, + { + id: 2, + text: 'second todo', + completed: false, + }, + { + id: 2, + text: 'second todo', + completed: true, + }, + ]); + done(); + }); store.dispatch({ type: ADD_TODO, payload: { text: 'first todo' } }); store.dispatch({ type: ADD_TODO, payload: { text: 'second todo' } });