[Angular] Testing @Input and @Output bindings
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();
}
}
Test @Input & @Output:
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 = ;
});
it('should not increase over the max value', () => {
component.step = ;
component.max = ;
component.increment();
component.increment();
expect(component.value).toEqual();
});
it('should not decrease below the min value', () => {
component.step = ;
component.min = ;
component.value = ;
component.decrement();
expect(component.value).toEqual();
component.decrement();
expect(component.value).toEqual();
});
it('should call the output on a value change', () => {
spyOn(component.changed, 'emit').and.callThrough();
component.step = ;
component.increment();
expect(component.changed.emit).toHaveBeenCalledWith()
});
});
[Angular] Testing @Input and @Output bindings的更多相关文章
- angular 的 @Input、@Output 的一个用法
angular 使用 @input.@Output 来进行父子组件之间数据的传递. 如下: 父元素: <child-root parent_value="this is parent ...
- Angular中input和output使用
// 写法一: 1 @Components({ 2 ...., 3 inputs:['init'], 4 outputs:['finish'] 5 }) 6 export class xxx(){ 7 ...
- Angular4学习笔记(六)- Input和Output
概述 Angular中的输入输出是通过注解@Input和@Output来标识,它位于组件控制器的属性上方. 输入输出针对的对象是父子组件. 演示 Input 新建项目connInComponents: ...
- [20160704]Addition program that use JOptionPane for input and output
//Addition program that use JOptionPane for input and output. import javax.swing.JOptionPane; public ...
- Python Tutorial 学习(七)--Input and Output
7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...
- [Python] Print input and output in table
Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...
- Input and Output File
Notes from C++ Primer File State Condition state is used to manage stream state, which indicates if ...
- [20171128]rman Input or output Memory Buffers.txt
[20171128]rman Input or output Memory Buffers.txt --//做一个简单测试rman 的Input or output Memory Buffers. 1 ...
- Python - 3. Input and Output
from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html Input a ...
随机推荐
- windows删除多余启动引导项
方法1: 按快捷键win+r,打开运行界面,输入msconfig. 点击确定,进入系统配置,选择引导选项卡,如图: 选中你不需要的启动项,点击下面的删除按钮即可.删除完成之后点击确定,重启计算机就可以 ...
- View_01_LayoutInflater的原理、使用方法
View_01_LayoutInflater的原理.使用方法 本篇博客是郭神博客Android视图状态及重绘流程分析,带你一步步深入了解View(一)的读书笔记的笔记. LayoutInflater简 ...
- open阶段的一致性检验(二)
SQL> select * from v$version where rownum=1; BANNER --------------------------------------------- ...
- javafx image button
public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...
- js中event事件处理
1. HTML事件 直接添加到HTML结构中 function show() { alert('hello'); } <body> <button id="btn&quo ...
- Python 之Numpy应用
NumPy 数据类型 numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型.下表列举了常用 NumPy ...
- 【Codeforces Round #452 (Div. 2) D】Shovel Sale
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让N乘2->a 然后看一下位数是多少. 假设有x位(x>=2) 则(0..(a%10-1) ) + (99..9)[x- ...
- java8新增特性(二)----函数式接口(Functional)
上一篇博客介绍了java8新增的Lambda表达式,这一节介绍一下java8的函数式编程,两者之间有什么联系呢?请往下看~~~ Lambda表达式怎样在java类型中表示的呢? 语言设计者投入了大量的 ...
- 使用差分VHD启动Win7
平台:windows 8.1 目的:在VHD里装个Win7 介绍:建立VHD装系统姑且不表,比较有意思的是用差分VHD备份系统这点. 差分硬盘这个概念在虚拟机里常用,类似VM中的快照,不过差分硬盘更灵 ...
- 关于Newtonsoft.json JsonConvert.DeserializeObject反序列化的使用
object obj = JsonConvert.DeserializeObject("{\"Sta\":3}", paramClass); //paramCl ...