When using Ngrx, we need to know how to test the component which has Router injected.

Component:

import {Component} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {Store} from '@ngrx/store'; import * as fromAuth from '../../reducers/auth';
import * as actions from '../../actions/auth'; @Component({
selector: 'register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent { error: string; constructor(private store: Store<fromAuth.State>) { } registerUser(event: FormGroup) {
const {email, password} = event.value;
this.store.dispatch(new actions.Register({
email,
password
}));
} }

One thing we can test just for component wihtout template is to test whether 'dispatch' function was called on Store.

import {async, ComponentFixture, TestBed} from '@angular/core/testing';

import {RegisterComponent} from './register.component';
import {RouterTestingModule} from '@angular/router/testing';
import {combineReducers, Store, StoreModule} from '@ngrx/store';
import {SharedModule} from '../../shared/SharedModule'; import {State as AuthState, reducers as AuthReducers} from '../../reducers';
import {Register, REGISTER} from '../../actions/auth';
import * as fromRoot from '../../../reducers';
import {FormGroup} from '@angular/forms'; describe('RegisterComponent', () => {
let component: RegisterComponent;
let fixture: ComponentFixture<RegisterComponent>;
let store: Store<fromRoot.State | AuthState>; beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [RegisterComponent],
imports: [
StoreModule.forRoot({
...fromRoot.reducers,
'auth': combineReducers(AuthReducers)
}),
SharedModule
]
})
.compileComponents();
})); beforeEach(() => { store = TestBed.get(Store);
spyOn(store, 'dispatch').and.callThrough(); fixture = TestBed.createComponent(RegisterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}); it('should create', () => {
expect(component).toBeTruthy();
}); it('should call distach when rigster a new user', () => {
const payload = {
email: 'test@test.com',
password: 'test123'
};
const event = {
value: payload
};
const action = new Register(payload); // init the action creatir
component.registerUser(event as FormGroup); // call the function or trigger from DOM
expect(store.dispatch).toHaveBeenCalledWith(action); // expect the dispatch have been call
expect(action.payload).toBe(payload); // check whether payload is correct
expect(action.type).toBe(REGISTER); // check the action type is correct
});
});

最新文章

  1. 初识的Spring Mvc-----原理
  2. hibernate 一对一关联关系 及其懒加载,总结
  3. rapidminer 数据导入及几个算子简单应用
  4. SharedPreferences实现记住密码功能
  5. JavaScript- The Good Parts Chapter 4
  6. idea集成svn插件
  7. [Rails Level 2] Ground up
  8. android http协议post请求方式
  9. MongoDB Sharding
  10. maps.reg
  11. 【网络流24题22】最长k可重线段集问题
  12. Android开发——设置界面的创建
  13. range()函数
  14. CorelCAD for Mac(绘图设计软件)破解版安装
  15. mysqld服务启动失败, Failed to restart mysqld.service: Unit not found.
  16. 20165318 2017-2018-2 《Java程序设计》第八周学习总结
  17. JSP,servlet和数据库之间传值出现乱码的问题
  18. 执行一条sql语句update多条不同值的记录实现思路
  19. Ulipad和有道词典冲突解决方法
  20. python3----练习题(图片转字符画)

热门文章

  1. POJ-1456 Supermarket 贪心问题 有时间限制的最小化惩罚问题
  2. 一:1.1 python程序与数据储存【进制转换】
  3. 【SRM 717 div2 B】LexmaxReplace
  4. Qt 在圆形中贴图片
  5. POJ——T 3728 The merchant
  6. docker 笔记1
  7. 27.C语言宽字符操作
  8. Linux 终端仿真程序Putty
  9. 解决MyEclipse中安装或升级ADT之后SDK Target无法显示的问题
  10. 【Codeforces Round #459 (Div. 2) D】MADMAX