|
1 |
| -import { TestBed, async } from '@angular/core/testing'; |
| 1 | +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { Component } from '@angular/core'; |
| 3 | +import { RouterModule, Routes, ActivatedRoute } from '@angular/router'; |
| 4 | +import { ReactiveFormsModule, FormsModule } from '@angular/forms'; |
| 5 | +import { StoreModule, Store } from '@ngrx/store'; |
| 6 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 7 | +// import { Observable } from 'rxjs'; |
| 8 | +// import 'rxjs/add/observable/of'; |
2 | 9 |
|
| 10 | +import { rootReducer, AppState } from './../redux/app.reducer'; |
| 11 | +import * as TodoActions from './../redux/todo/todo.actions'; |
| 12 | + |
| 13 | +import { TodoComponent } from './todo/todo.component'; |
| 14 | +import { FooterComponent } from './footer/footer.component'; |
| 15 | +import { NewTodoComponent } from './new-todo/new-todo.component'; |
| 16 | +import { TodoListComponent } from './todo-list/todo-list.component'; |
3 | 17 | import { AppComponent } from './app.component';
|
4 | 18 |
|
5 |
| -describe('AppComponent', () => { |
| 19 | +fdescribe('AppComponent', () => { |
| 20 | + |
6 | 21 | beforeEach(async(() => {
|
7 | 22 | TestBed.configureTestingModule({
|
8 | 23 | declarations: [
|
9 |
| - AppComponent |
| 24 | + AppComponent, |
| 25 | + TodoComponent, |
| 26 | + FooterComponent, |
| 27 | + TodoListComponent, |
| 28 | + NewTodoComponent |
10 | 29 | ],
|
| 30 | + imports: [ |
| 31 | + ReactiveFormsModule, |
| 32 | + FormsModule, |
| 33 | + RouterTestingModule.withRoutes([ |
| 34 | + {path: '', component: TodoListComponent} |
| 35 | + ]), |
| 36 | + StoreModule.forRoot(rootReducer), |
| 37 | + ] |
11 | 38 | }).compileComponents();
|
12 | 39 | }));
|
13 | 40 |
|
14 |
| - it('should create the app', async(() => { |
15 |
| - const fixture = TestBed.createComponent(AppComponent); |
16 |
| - const app = fixture.debugElement.componentInstance; |
17 |
| - expect(app).toBeTruthy(); |
18 |
| - })); |
19 | 41 |
|
20 |
| - it(`should have as title 'app'`, async(() => { |
21 |
| - const fixture = TestBed.createComponent(AppComponent); |
22 |
| - const app = fixture.debugElement.componentInstance; |
23 |
| - expect(app.title).toEqual('app'); |
24 |
| - })); |
| 42 | + it('should be created', () => { |
| 43 | + let store = TestBed.get(Store); |
| 44 | + spyOn(store, 'dispatch').and.callThrough(); |
25 | 45 |
|
26 |
| - it('should render title in a h1 tag', async(() => { |
27 |
| - const fixture = TestBed.createComponent(AppComponent); |
| 46 | + spyOn(localStorage, 'getItem').and.returnValue('[]'); |
| 47 | + |
| 48 | + let fixture = TestBed.createComponent(AppComponent); |
| 49 | + let component = fixture.componentInstance; |
28 | 50 | fixture.detectChanges();
|
29 |
| - const compiled = fixture.debugElement.nativeElement; |
30 |
| - expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!'); |
31 |
| - })); |
| 51 | + expect(component).toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('Test for populateTodos', () => { |
| 55 | + |
| 56 | + it('should dispatch an action with item in localStorage', () => { |
| 57 | + |
| 58 | + let store = TestBed.get(Store); |
| 59 | + spyOn(store, 'dispatch').and.callThrough(); |
| 60 | + |
| 61 | + spyOn(localStorage, 'getItem').and.returnValue('[]'); |
| 62 | + |
| 63 | + let fixture = TestBed.createComponent(AppComponent); |
| 64 | + let component = fixture.componentInstance; |
| 65 | + fixture.detectChanges(); |
| 66 | + |
| 67 | + const action = new TodoActions.PopulateTodosAction([]); |
| 68 | + expect(store.dispatch).toHaveBeenCalledWith(action); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should dispatch an action with null in localStorage', () => { |
| 72 | + |
| 73 | + let store = TestBed.get(Store); |
| 74 | + spyOn(store, 'dispatch').and.callThrough(); |
| 75 | + |
| 76 | + spyOn(localStorage, 'getItem').and.returnValue(null); |
| 77 | + |
| 78 | + let fixture = TestBed.createComponent(AppComponent); |
| 79 | + let component = fixture.componentInstance; |
| 80 | + fixture.detectChanges(); |
| 81 | + |
| 82 | + const action = new TodoActions.PopulateTodosAction([]); |
| 83 | + expect(store.dispatch).toHaveBeenCalledWith(action); |
| 84 | + }); |
| 85 | + |
| 86 | + }); |
| 87 | + |
| 88 | + describe('Test for updateTodos', () => { |
| 89 | + |
| 90 | + it('should called localStorage.setItem', () => { |
| 91 | + |
| 92 | + let store = TestBed.get(Store); |
| 93 | + spyOn(store, 'dispatch').and.callThrough(); |
| 94 | + |
| 95 | + spyOn(localStorage, 'getItem').and.returnValue('[]'); |
| 96 | + spyOn(localStorage, 'setItem').and.callThrough(); |
| 97 | + |
| 98 | + let fixture = TestBed.createComponent(AppComponent); |
| 99 | + let component = fixture.componentInstance; |
| 100 | + fixture.detectChanges(); |
| 101 | + |
| 102 | + expect(localStorage.setItem).toHaveBeenCalledWith('angular-ngrx-todos', '[]'); |
| 103 | + }); |
| 104 | + |
| 105 | + }); |
32 | 106 | });
|
0 commit comments