|
1 |
| -import { TestBed } from '@angular/core/testing'; |
2 |
| -import { skip, take } from 'rxjs/operators'; |
| 1 | +import { TestBed, ComponentFixture } from '@angular/core/testing'; |
| 2 | +import { skip, take, tap } from 'rxjs/operators'; |
3 | 3 | import { MockStore, provideMockStore } from '@ngrx/store/testing';
|
4 |
| -import { Store, createSelector, select, StoreModule } from '@ngrx/store'; |
| 4 | +import { |
| 5 | + Store, |
| 6 | + createSelector, |
| 7 | + select, |
| 8 | + StoreModule, |
| 9 | + MemoizedSelector, |
| 10 | + createFeatureSelector, |
| 11 | +} from '@ngrx/store'; |
5 | 12 | import { INCREMENT } from '../../spec/fixtures/counter';
|
| 13 | +import { Component, OnInit } from '@angular/core'; |
| 14 | +import { Observable } from 'rxjs'; |
| 15 | +import { By } from '@angular/platform-browser'; |
6 | 16 |
|
7 | 17 | interface TestAppSchema {
|
8 | 18 | counter1: number;
|
@@ -260,6 +270,81 @@ describe('Mock Store', () => {
|
260 | 270 | });
|
261 | 271 | });
|
262 | 272 |
|
| 273 | +describe('Refreshing state', () => { |
| 274 | + type TodoState = { |
| 275 | + items: { name: string; done: boolean }[]; |
| 276 | + }; |
| 277 | + const selectTodosState = createFeatureSelector<TodoState>('todos'); |
| 278 | + const todos = createSelector(selectTodosState, todos => todos.items); |
| 279 | + const getTodoItems = (elSelector: string) => |
| 280 | + fixture.debugElement.queryAll(By.css(elSelector)); |
| 281 | + let mockStore: MockStore<TodoState>; |
| 282 | + let mockSelector: MemoizedSelector<TodoState, any[]>; |
| 283 | + const initialTodos = [{ name: 'aaa', done: false }]; |
| 284 | + let fixture: ComponentFixture<TodosComponent>; |
| 285 | + |
| 286 | + @Component({ |
| 287 | + selector: 'app-todos', |
| 288 | + template: ` |
| 289 | + <ul> |
| 290 | + <li *ngFor="let todo of todos | async"> |
| 291 | + {{ todo.name }} <input type="checkbox" [checked]="todo.done" /> |
| 292 | + </li> |
| 293 | +
|
| 294 | + <p *ngFor="let todo of todosSelect | async"> |
| 295 | + {{ todo.name }} <input type="checkbox" [checked]="todo.done" /> |
| 296 | + </p> |
| 297 | + </ul> |
| 298 | + `, |
| 299 | + }) |
| 300 | + class TodosComponent implements OnInit { |
| 301 | + todos: Observable<any[]>; |
| 302 | + todosSelect: Observable<any[]>; |
| 303 | + |
| 304 | + constructor(private store: Store<{}>) {} |
| 305 | + |
| 306 | + ngOnInit() { |
| 307 | + this.todos = this.store.pipe(select(todos)); |
| 308 | + this.todosSelect = this.store.select(todos); |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + beforeEach(() => { |
| 313 | + TestBed.configureTestingModule({ |
| 314 | + declarations: [TodosComponent], |
| 315 | + providers: [provideMockStore()], |
| 316 | + }).compileComponents(); |
| 317 | + |
| 318 | + mockStore = TestBed.get(Store); |
| 319 | + mockSelector = mockStore.overrideSelector(todos, initialTodos); |
| 320 | + |
| 321 | + fixture = TestBed.createComponent(TodosComponent); |
| 322 | + fixture.detectChanges(); |
| 323 | + }); |
| 324 | + |
| 325 | + it('should work with store and select operator', () => { |
| 326 | + const newTodos = [{ name: 'bbb', done: true }]; |
| 327 | + mockSelector.setResult(newTodos); |
| 328 | + mockStore.refreshState(); |
| 329 | + |
| 330 | + fixture.detectChanges(); |
| 331 | + |
| 332 | + expect(getTodoItems('li').length).toBe(1); |
| 333 | + expect(getTodoItems('li')[0].nativeElement.textContent.trim()).toBe('bbb'); |
| 334 | + }); |
| 335 | + |
| 336 | + it('should work with store.select method', () => { |
| 337 | + const newTodos = [{ name: 'bbb', done: true }]; |
| 338 | + mockSelector.setResult(newTodos); |
| 339 | + mockStore.refreshState(); |
| 340 | + |
| 341 | + fixture.detectChanges(); |
| 342 | + |
| 343 | + expect(getTodoItems('p').length).toBe(1); |
| 344 | + expect(getTodoItems('p')[0].nativeElement.textContent.trim()).toBe('bbb'); |
| 345 | + }); |
| 346 | +}); |
| 347 | + |
263 | 348 | describe('Cleans up after each test', () => {
|
264 | 349 | const selectData = createSelector(
|
265 | 350 | (state: any) => state,
|
|
0 commit comments