[Angular Unit Testing] Testing Component methods
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
import {StockCounterComponent} from './stock-counter.component';
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
describe('StockCounterComponent', () => {
let component: StockCounterComponent;
let fixture: ComponentFixture<StockCounterComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
StockCounterComponent
]
});
fixture = TestBed.createComponent(StockCounterComponent);
component = fixture.componentInstance;
component.value = 0;
});
it('should increment correctly', () => {
component.increment()
expect(component.value).toBe(1);
});
it('should decrement correctly', () => {
component.increment()
expect(component.value).toBe(1);
component.decrement()
expect(component.value).toBe(0);
});
it('should not decrement below the minimum value', () => {
component.increment()
expect(component.value).toBe(1);
component.decrement()
expect(component.value).toBe(0);
component.decrement()
expect(component.value).toBe(0);
});
it('should not increment below the maximum value', () => {
for (let i = 0; i < 200; i++) {
component.increment();
}
expect(component.value).toBe(100);
});
});
component:
import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'stock-counter',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="stock-counter">
<div>
<div
(keydown)="onKeyUp($event)"
(blur)="onBlur($event)"
(focus)="onFocus($event)"
tabindex="0">
<p>{{ value }}</p>
<div tabindex="-1">
<button type="button" tabindex="-1" (click)="increment()" [disabled]="value === max">
+
</button>
<button type="button" tabindex="-1" (click)="decrement()" [disabled]="value === min">
-
</button>
</div>
</div>
</div>
</div>
`
})
export class StockCounterComponent {
@Input() step: number = 1;
@Input() min: number = 0;
@Input() max: number = 100;
@Output() changed = new EventEmitter<number>();
value: number = 0;
focused: boolean;
increment() {
if (this.value < this.max) {
this.value = this.value + this.step;
this.changed.emit(this.value);
}
}
decrement() {
if (this.value > this.min) {
this.value = this.value - this.step;
this.changed.emit(this.value);
}
}
private onBlur(event: FocusEvent) {
this.focused = false;
event.preventDefault();
event.stopPropagation();
}
private onKeyUp(event: KeyboardEvent) {
let handlers = {
ArrowDown: () => this.decrement(),
ArrowUp: () => this.increment()
};
if (handlers[event.code]) {
handlers[event.code]();
event.preventDefault();
event.stopPropagation();
}
}
private onFocus(event: FocusEvent) {
this.focused = true;
event.preventDefault();
event.stopPropagation();
}
}
[Angular Unit Testing] Testing Component methods的更多相关文章
- [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] Testing Component with Store
When using Ngrx, we need to know how to test the component which has Router injected. Component: imp ...
- [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 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- ...
随机推荐
- stm32与arm7比较(经典)
http://wenku.baidu.com/link?url=LIVcT1AQL0IgVF1xan5Zy9rXarCBo66hj7OXSxM1ap7FpssO4c3sd1pZd8azfBPr3PBy ...
- HDU 2281 Square Number Pell方程
http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...
- BZOJ3282: Tree (LCT模板)
Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...
- ODBC总结
引用头文件:sql.h.sqlext.h.sqltypes.h 添加库文件:odbc32.lib odbccp32.lib 1.定义环境变量: SQLHENV henv =nullptr://环境 ...
- 15.lambda表达式
#include <iostream> #include <array> using namespace std; //解决函数怀孕现象 //[](){} //[] =引用,只 ...
- winform程序,备份数据库+并压缩+并删除以前的备份
说明:为了定时备份服务器上的数据库并压缩到指定目录,方便下载到本地而写本程序.配合windows的任务计划,可以达到定时备份数据库的目的. 程序需引用SQLDMO.DLL,如电脑上已安装sqlserv ...
- import 与export详解
ES6 1.export default 其他模块加载该模块时,import命令可以为该匿名函数指定任意名字. 如: import Vue from 'vue' vue里面的第三方模块都是用了这个 使 ...
- 开发过程使用Tomcat Maven插件持续快捷部署Web项目
我在平时工作中部署Web项目到测试服务器上的Tomcat时用的是Hudson.Hudson本身已经跟SVN.Git.Maven集成并且支持添加各种插件.但如果使用Hudson,我需要配置两个任务:一个 ...
- 图灵机器人API,适用于微信、微博、QQ群、智能硬件等
该API有智能聊天.查天气.查快递.查菜谱.查车票.查航班.查出行.查周边等近500个功能,能够用在微信公众平台.QQ群.手机语音助手.智能硬件等领域\ [1].[代码] [PHP]代码 跳至 [1] ...
- 一个IP建多个Web站点
TCP端口法 由于各种原因,我们有时候需要在一个IP地址上建立多个web站点,在IIS5中,我们可能通过简单的设 置达到这个目标. 在IIS中,每个 Web 站点都具有唯一的.由三个部分组成的标识 ...