Services

Let's add the todos service.

todos.service.ts
export class TodosService {
constructor(private todosStore: TodosStore) {}
get() {
this.fakeHttp().subscribe(todos => this.todosStore.set(todos));
}
fakeHttp() {
const fake = [createTodo(), createTodo()];
return timer(500).pipe(mapTo(fake));
}
add() {
this.todosStore.add(createTodo());
}
}

And the test.

todos.service.spec.ts
import { of } from 'rxjs';
describe('TodosService', () => {
let todosService: TodosService;
let todosStore: TodosStore;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [TodosService, TodosStore],
imports: [HttpClientTestingModule]
});
todosService = TestBed.get(TodosService);
todosStore = TestBed.get(TodosStore);
});
it('should be created', () => {
expect(todosService).toBeDefined();
});
it('should set todos', () => {
const fakeTodos = [createTodo(), createTodo()];
spyOn(todosService, 'fakeHttp').and.returnValue(of(fakeTodos));
spyOn(todosStore, 'set').and.callThrough();
todosService.get();
expect(todosStore.set).toHaveBeenCalledWith(fakeTodos);
});
it('should add todo', () => {
spyOn(todosStore, 'add').and.callThrough();
todosService.add();
expect(todosStore.add).toHaveBeenCalledTimes(1);
});
});

There is nothing special here. It's just a regular class that performs assertions.