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的更多相关文章

  1. angular 的 @Input、@Output 的一个用法

    angular 使用 @input.@Output 来进行父子组件之间数据的传递. 如下: 父元素: <child-root parent_value="this is parent ...

  2. Angular中input和output使用

    // 写法一: 1 @Components({ 2 ...., 3 inputs:['init'], 4 outputs:['finish'] 5 }) 6 export class xxx(){ 7 ...

  3. Angular4学习笔记(六)- Input和Output

    概述 Angular中的输入输出是通过注解@Input和@Output来标识,它位于组件控制器的属性上方. 输入输出针对的对象是父子组件. 演示 Input 新建项目connInComponents: ...

  4. [20160704]Addition program that use JOptionPane for input and output

    //Addition program that use JOptionPane for input and output. import javax.swing.JOptionPane; public ...

  5. Python Tutorial 学习(七)--Input and Output

    7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...

  6. [Python] Print input and output in table

    Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...

  7. Input and Output File

    Notes from C++ Primer File State Condition state is used to manage stream state, which indicates if ...

  8. [20171128]rman Input or output Memory Buffers.txt

    [20171128]rman Input or output Memory Buffers.txt --//做一个简单测试rman 的Input or output Memory Buffers. 1 ...

  9. Python - 3. Input and Output

    from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html Input a ...

随机推荐

  1. ORA-01031: 权限不足

    1.错误描写叙述 ORA-01031: 权限不足 2.错误原因 SQL> create user yhd identified by scott account unlock; create u ...

  2. 深入具体解释SQL中的Null

    NULL 在计算机和编程世界中表示的是未知,不确定.尽管中文翻译为 "空", 但此空(null)非彼空(empty). Null表示的是一种未知状态.未来状态,比方小明兜里有多少钱 ...

  3. 4个开源的Gmail替代品

    资料来源 https://opensource.com/alternatives/gmail 本文是对原文翻译,在调试 privmx 时么有成功,这些代码大多用于 php5 ,对PHP7 不兼容. 相 ...

  4. GestureDetector- 滑屏手势方式实现

    今天做的项目中,需要使用滑屏来调出一个界面,经过自己的尝试,结合网上的方法,成功实现了. 代码如下 package com.example.text; import android.app.Activ ...

  5. Android 基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器aaa

    MDPlayer万能播放器 MDPlayer,基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器,可以播 ...

  6. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  7. HTML5梦幻星空,可用作网页背景

    <html> <head> <title>星空</title> <META http-equiv="X-UA-Compatible&qu ...

  8. 机器学习算法中怎样选取超參数:学习速率、正则项系数、minibatch size

    本文是<Neural networks and deep learning>概览 中第三章的一部分,讲机器学习算法中,怎样选取初始的超參数的值.(本文会不断补充) 学习速率(learnin ...

  9. Excel Add-in

    Excel Add-in 前言 这个系列文章应该有一阵子没有更新了,原因是一如既往的多,但是根本所在是我对于某些章节其实还没有完全想好怎么写,尤其是对于Office Add-in这块 —— 到底是要每 ...

  10. Codeforces #28 C.Bath Queue (概率dp)

    Codeforces Beta Round #28 (Codeforces format) 题目链接: http://codeforces.com/contest/28/problem/C 题意: 有 ...