[Angular] Test component template
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的更多相关文章
- [Angular 2] Component relative paths
Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...
- angular 引入 component 报错
每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...
- Failed to mount component: template or render function not defined.
Failed to mount component: template or render function not defined. vue-loader13.0有一个变更就是默认启用了esModu ...
- "[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 ...
- 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 ...
- 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 ...
- 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 ...
- Vue出现Component template should ...
当运行vue出现错误Component template should contain exactly one root element. If you ...的时候,我们只需要将<templa ...
- [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)
Here is the defulat tab header template: <ng-template #defaultTabHeader let-tabs="tabsX" ...
随机推荐
- 数据存储值归档Archive
先比較一下各个数据存储之间的关系: 关于归档.是ios中的shu'j数据存储中的一种数据存储方式.以下了解一下归档中的一个实例: 以下的是父类person #import <Foundation ...
- VUE错误记录 - 小球模拟购物车
<body> <div id="app"> <input type="button" value="Add to Car ...
- Python操作MySQL数据库完成简易的增删改查功能
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...
- 微信小程序弹框提示绑定手环实例
今天想聊一聊小程序里面存在的一些逻辑问题,拿手上的这个小程序来说,(这个小程序是开发出来玩的,每个人手上有一个手环,带着手环时候的心率,运动步数,血压数据都会展现在这个小程序里面,一目了然)用户第一次 ...
- 【例题 8-3 UVA - 1152】4 Values whose Sum is 0
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然中间相遇. 自己写了个hash处理一下冲突就可以了. [代码] /* 1.Shoud it use long long ? 2. ...
- 10.static_extern
另一个文件声明 #include <iostream> using namespace std; ; void show() { cout << " << ...
- Android Intent的setClass和setClassName的区别
setClass:跳转到与该工程下的(同一个Application中的)activity或者service setClassName:跳转到不同Applicaiton的activity或者servic ...
- 关于python的序列和矩阵运算的写法
#其实下面是这样一个函数,传入的是obj_value,传出的是newobj_value.,, #这里的obj_value实际上是一个序列... for z in obj_value: ...
- ThinkPHP5.0的安装
ThinkPHP5.0的安装很简单: 1.下载“phpstudy”安装 2.下载thinkphp源文件 3.把thinkphp源文件解压并放到phpstudy目录下的“WWW”目录 4.然后开启服务并 ...
- Java Web学习总结(6)——通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 package gacl.res ...