Skip to content

Commit 9c417d5

Browse files
committed
add test for AppComponent
1 parent d57180c commit 9c417d5

File tree

3 files changed

+95
-22
lines changed

3 files changed

+95
-22
lines changed

src/app/app.component.spec.ts

Lines changed: 92 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,106 @@
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';
29

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';
317
import { AppComponent } from './app.component';
418

5-
describe('AppComponent', () => {
19+
fdescribe('AppComponent', () => {
20+
621
beforeEach(async(() => {
722
TestBed.configureTestingModule({
823
declarations: [
9-
AppComponent
24+
AppComponent,
25+
TodoComponent,
26+
FooterComponent,
27+
TodoListComponent,
28+
NewTodoComponent
1029
],
30+
imports: [
31+
ReactiveFormsModule,
32+
FormsModule,
33+
RouterTestingModule.withRoutes([
34+
{path: '', component: TodoListComponent}
35+
]),
36+
StoreModule.forRoot(rootReducer),
37+
]
1138
}).compileComponents();
1239
}));
1340

14-
it('should create the app', async(() => {
15-
const fixture = TestBed.createComponent(AppComponent);
16-
const app = fixture.debugElement.componentInstance;
17-
expect(app).toBeTruthy();
18-
}));
1941

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();
2545

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;
2850
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+
});
32106
});

src/app/app.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export class AppComponent {
1919
}
2020

2121
private populateTodos() {
22-
const todos: Todo[] = JSON.parse(localStorage.getItem('angular-todos') || '[]');
22+
const todos: Todo[] = JSON.parse(localStorage.getItem('angular-ngrx-todos') || '[]');
2323
this.store.dispatch(new TodoActions.PopulateTodosAction(todos));
2424
}
2525

2626
private updateTodos() {
2727
this.store.select('todos')
2828
.subscribe(todos => {
29-
localStorage.setItem('angular-todos', JSON.stringify(todos));
29+
localStorage.setItem('angular-ngrx-todos', JSON.stringify(todos));
3030
});
3131
}
3232

src/app/todo-list/todo-list.component.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import { TodoComponent } from './../todo/todo.component';
2121
export class BlankCmp {
2222
}
2323

24-
25-
fdescribe('TodoListComponent', () => {
24+
describe('TodoListComponent', () => {
2625
let component: TodoListComponent;
2726
let fixture: ComponentFixture<TodoListComponent>;
2827
let store: Store<AppState>;

0 commit comments

Comments
 (0)