Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(store): fix flaky integration test #2209

Merged
merged 1 commit into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/store/spec/fixtures/todos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
158 changes: 76 additions & 82 deletions modules/store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -54,6 +55,7 @@ describe('ngRx Integration spec', () => {
};

beforeEach(() => {
resetId();
spyOn(reducers, 'todos').and.callThrough();

TestBed.configureTestingModule({
Expand Down Expand Up @@ -183,34 +185,36 @@ 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) => {
return todos.find(p => p.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' } });
Expand All @@ -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<TodoAppSchema, Todo[]>(
'todos'
);
Expand All @@ -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' } });
Expand Down Expand Up @@ -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<TodoAppSchema, Todo[]>(
'todos'
);
Expand All @@ -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' } });
Expand All @@ -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<TodoAppSchema, Todo[]>(
'todos'
);
Expand All @@ -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' } });
Expand Down