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:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { DebugElement } from '@angular/core';
import { StockCounterComponent } from './stock-counter.component';
import { By } from '@angular/platform-browser'; TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
); describe('StockCounterComponent', () => { let component: StockCounterComponent;
let fixture: ComponentFixture<StockCounterComponent>;
let el: DebugElement; beforeEach(() => {
TestBed.configureTestingModule({
declarations: [StockCounterComponent]
}); fixture = TestBed.createComponent(StockCounterComponent);
component = fixture.componentInstance;
el = fixture.debugElement; component.value = ;
}); it('should increase the value when + button is clicked', () => {
el.query(By.css('button:first-child')).triggerEventHandler('click', null);
expect(component.value).toBe();
el.query(By.css('button:first-child')).triggerEventHandler('click', null);
fixture.detectChanges();
expect(component.value).toBe();
expect(el.query(By.css('p')).nativeElement.textContent).toBe('');
}); it('should decrease the value when - button is clicked', () => {
component.value = ;
fixture.detectChanges();
el.query(By.css('button:last-child')).triggerEventHandler('click', null);
fixture.detectChanges();
expect(component.value).toBe();
expect(el.query(By.css('p')).nativeElement.textContent).toBe('');
}); it('should show the right value in p tag when the arrow up key is pressed', () => {
const event = new Event('KeyboardEvent') as any;
event.code = "ArrowUp";
el.query(By.css('.stock-counter > div > div')).triggerEventHandler('keydown', event);
fixture.detectChanges();
expect(component.value).toBe();
expect(el.query(By.css('p')).nativeElement.textContent).toBe('');
});
});

[Angular] Test component template的更多相关文章

  1. [Angular 2] Component relative paths

    Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...

  2. angular 引入 component 报错

    每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...

  3. Failed to mount component: template or render function not defined.

    Failed to mount component: template or render function not defined. vue-loader13.0有一个变更就是默认启用了esModu ...

  4. "[Vue warn]: Failed to mount component: template or render function not defined"错误的解决

    VUE中动态路由出错: vue.esm.js?a026: [Vue warn]: Failed to mount component: template or render function not ...

  5. VUE之命令行报错:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead 解决办法

    Failed to compile. ./node_modules/vue-loader/lib/template-compiler?{"id":"data-v-5992 ...

  6. VUE编译报错 Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead

    背景: 在使用VUE添加标签的时候编译报错,报错如下: Component template should contain exactly one root element. If you are u ...

  7. vue报错:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.

    在.vue文件中引入了 element-ui 的 table 和 pagination 组件后,报错:Component template should contain exactly one roo ...

  8. Vue出现Component template should ...

    当运行vue出现错误Component template should contain exactly one root element. If you ...的时候,我们只需要将<templa ...

  9. [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)

    Here is the defulat tab header template: <ng-template #defaultTabHeader let-tabs="tabsX" ...

随机推荐

  1. [NOIP2013]车站分级 解题报告

    妈蛋这道普及组水(神)题搞了我非常久. 一. 首先一个非常显然的事情就是每一个火车告诉了站与站之间的等级关系,所以拓扑求最长路. 可是发现暴力建边的话最坏能够达到500*500,所以时间复杂度有O(M ...

  2. actionBar-进入界面闪烁问题解决

    问题分析: 主要是因为在开启一个应用的时候,当前界面并不是第一界面,在它之前,还有一个界面启动了,这个界面的唯一目的就是启动主界面,它目的不是显示.虽然如此,但是呢,这个界面的theme因为没有做统一 ...

  3. Day6下午题解1

    预计分数:100+?+30=130+? 实际分数:100+25+30=155 T1 https://www.luogu.org/problem/show?pid=T15920 DP裸题,用dp[i][ ...

  4. dataguard主备延迟多长时间的查询方法

    select value from v$dataguard_stats where name='apply lag';    

  5. JavaScript 进度条重复加载

    <!DOCTYPE HTML> <html> <head> <meta charset ="utf-8"> <title> ...

  6. Vue Invalid handler for event "": got undefined

    原因:绑定的方法不是放在methods:{}里.比如我把绑定的函数写在了computed:{}里就会报这个错.

  7. ORA-01078错误举例:SID的大写和小写错误

    案例重演: dbca建库.SID:metro    --手工建库时实例名小写的metro ...... [oracle@org54 ~]$ export ORACLE_SID=METRO        ...

  8. C/C++函数指针声明

    前天看APUE,看到signal的声明竟然是 void (*signal(int,void(*)(int)))(int); 初看下面,还真是看不出这是啥意思.道行太浅,仅仅能看到这样的函数指针 voi ...

  9. 97.TCP通信

    运行截图: 客户端 创建通信套接字 //通信套接字,用于创建TCP连接 SOCKET socket_send; 创建tcp通信 //创建tcp通信 socket_send = socket(AF_IN ...

  10. 软件——protel 的pcb电路图制作

    近期一直在学习PCB板的绘制.