[Angular & Unit Testing] Testing Component with Store
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
});
});
[Angular & Unit Testing] Testing Component with Store的更多相关文章
- [Angular & Unit Testing] Testing a RouterOutlet component
The way to test router componet needs a little bit setup, first we need to create a "router-stu ...
- [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...
- [Angular Unit Testing] Debug unit testing -- component rendering
If sometime you want to log out the comonent html to see whether the html render correctly, you can ...
- [Angular Unit Testing] Testing Component methods
import {ComponentFixture, TestBed} from '@angular/core/testing'; import {BrowserDynamicTestingModule ...
- [Angular Testing] Unit Testing -- Test component and service.
Recommend to use angular-cli to generate component and service, so we can get testing templates. ng ...
- [Angular & Unit Testing] Automatic change detection
When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...
- [Angular Unit Testing] Shallow Pipe Testing
import { TestBed, ComponentFixture } from '@angular/core/testing'; import { BrowserDynamicTestingMod ...
- [Angular Unit Testing] Testing Services with dependencies
import { Http, Response, ResponseOptions } from '@angular/http'; import { TestBed } from '@angular/c ...
- [Angular + Unit] AngularJS Unit testing using Karma
http://social.technet.microsoft.com/wiki/contents/articles/32300.angularjs-unit-testing-using-karma- ...
随机推荐
- Android 连接网络数据库的方式
以连接MS SQL(sqlserver数据库)的网络数据库为例,从当前搜集的资料来看,一共有两种方式:在Android工程中引入JDBC驱动,直接连接:通过WebService等方法的间接连接. 采用 ...
- 怎样在HP-UX中安装bash?
怎样在HP-UX中安装bash? 注:HPUX全称是Hewlett Packard UniX是惠普9000系列server的Unix操作系统,能够在HP的PA-RISC处理器.Intel的Ita ...
- Linux下EPoll通信模型简析
EPoll基于I/O的事件通知机制,由系统通知用户那些SOCKET触发了那些相关I/O事件.事件中包括相应的文件描写叙述符以及事件类型.这样应用程序能够针对事件以及事件的source做相应的处理(Ac ...
- VS 2015支持C语言和C++程序
先要安装C++的相关支持控件! 然后就可以使用VS编写C++或者C程序了. 默认支持的是C++,将后缀名改为C就是支持C了. 学习数据结构算法之类的,就可以通过VS来学习了. 安装 新建C++项目 C ...
- Aizu - 2555 Everlasting Zero 模拟
Aizu - 2555 Everlasting Zero 题意:学习技能,每个技能有不同的要求,问能否学习全部特殊技能 思路:枚举每两个技能,得到他们的先后学习关系,如果两个都不能先学的话就是No了, ...
- SQL server无法启动服务,提示“错误1069: 由于登录失败而无法启动服务”
原因:大部分情况是你修改了服务器系统的登录密码,而导致SQL服务无法启动. 解决方法:将sql server(mssql server)服务的登录密码改为系统登录密码或本地登录,如下操作步骤: 在wi ...
- 硬件时间,操作系统时间,Windows 和linux 双系统时间差8小时问题说明
1.硬件时间:硬件时钟是存储在主板上CMOS里的时间即BIOS时间,关机后该时钟依然运行,主板的电池为它供电.对应于嵌入式设备有一个RTC模块.硬件时钟即RTC时钟.信息比较少没时区.夏令时的概念. ...
- openSUSE Leap与 SELS的区别
openSUSE Leap 是 openSUSE 常规发行版本的新名称,在 13.2 之前它仅仅被称为“openSUSE”. 一.openSUSE 发行周期:(15年以前仅有一个openSUSE发行版 ...
- yum配置中driver-class-name: com.mysql.jdbc.Driver报错
错误: 原因: 解决方法:把方框中的<scope>runtime</scope>删掉
- 玩转 Jupyter Notebook (CentOS)
Jupyter Notebook 简介 Jupyter Notebook 是一个开源的 Web 应用程序,可以用来创建和共享包含动态代码.方程式.可视化及解释性文本的文档.其应用于包括:数据整理与转换 ...