[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- ...
随机推荐
- PHP DES-ECB加密对接Java解密
最近公司有个业务,需要对接第三方接口,但是参数是需要加密的,对方也只提供了一个java的demo,在网上到处搜索,没有找到直接就能用的方法,后来还是跟公司的Android工程师对接出来的,在这里记录一 ...
- 题解 CF1037D 【Valid BFS?】
不管怎么说,这都不是道紫题吧... 这里采用的思想有点类似轻重链剖分. 我们按照每个节点在序列里面出现的顺序,把每一个节点连出去的边都排一个序. 这样(如果序列没错)肯定会按照序列的方式遍历完全图. ...
- traits的介绍
traits基本利用了上一篇文章的思想,通过局部特化,来返回针对特定类型的信息. 可以自己实现traits,约定俗成了,返回特性信息. 也可以利用iterator的traits信息. iterator ...
- 在Unix上用 BIND建立名称服务器(naem server)
在Unix上用 BIND建立名称服务器(naem server) 安装 apt install -y bind9 yum install -y bind bind-utils 下载源码并解压缩,htt ...
- ISheet ICell
/// <summary> /// Gets the first row on the sheet /// </summary> /// <value>the nu ...
- style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)"
style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)" 一.问题 backgroun ...
- poj--2391--Ombrophobic Bovines(floyd+二分+最大流拆点)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u ...
- 手动新建hive编程环境(以hive-1.0.0和hive-1.2.1为例)
如下,是用maven构建项目,本篇博文重点不是这个.初学者(小白)变成小鸟后,建议开始用maven啦! Eclipse下Maven新建项目.自动打依赖jar包(包含普通项目和Web项目) HBase ...
- 基于Linux下Iptables限制BT下载的研究
基于Linux下Iptables限制BT下载的研究 摘要: 当前BT下载技术和软件飞速发展,给人们网上冲浪获取资源带来了极大的便利, 但同时BT占用大量的网络带宽等资源也给网络和网络管理员 ...
- VBA 字符串操作(基础篇)
转自:http://blog.csdn.net/jyh_jack/article/details/2315345 mid(字符串,从第几个开始,长度) 在[字符串]中[从第几个开始]取出[长度个字符串 ...