[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- ...
随机推荐
- C# 中编写函数式代码
http://www.dotnetcurry.com/csharp/1384/functional-programming-fsharp-for-csharp-developers 写给 C# 开发人 ...
- js ---- 实现千位分隔符
第一种方法: var num = 1234567; var string = num.toString(); var arr = string.split('').reverse(); console ...
- javafx image button
public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...
- STL中erase()的陷阱
最近在刷stl源码剖析这本书时,对于vector的erase()函数引起了我的注意 在删除单个元素时是这样定义的: iterator erase(iterator position){ !=end() ...
- HDU 3232 && UVA 12230 (简单期望)
Crossing Rivers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- MyBatis学习总结(15)——定制Mybatis自动代码生成的maven插件
==================================================================================================== ...
- 关于Clipboard和GlobalAlloc函数的关系
一句话:为了满足进程间通信,使用了clipboard的方法,clipboard是系统提供的一段任何进程都可以访问的公共内存块,malloc 和new分配的动态内存块是在进程的私有地址空间分配的,所以必 ...
- (转)xshell无法在vim中复制黏贴
ssh xshell 连接在vim中无法用 ctrl+insert 复制黏贴 修改.vimrc set mouse=c vi的三种模式:命令模式,插入模式,可视模式.鼠标可以启动于各种模式中: The ...
- ZJOI2002昂贵的聘礼题解
http://codevs.cn/problem/1324/ 题目大意 一个部落,你能够和社会地位等级的极差不大于M的全部人交易.你能够拿金币直接从一个人手里买东西,也能够从别人那里买到那个人想要的东 ...
- php课程 8-30 实现验证码验证的难点是什么
php课程 8-30 实现验证码验证的难点是什么 一.总结 一句话总结:session技术实现验证码传递. 1.生成验证码的那个网页(php文件)中的验证码怎么搁到别的网页中去? 直接在img的src ...