[Angular] Test Directive
directive:
import { Directive, HostListener, HostBinding, ElementRef } from '@angular/core'; @Directive({
selector: '[credit-card]'
})
export class CreditCardDirective { @HostBinding('style.border')
border: string; @HostListener('input', ['$event'])
onKeyDown(event: KeyboardEvent) { const input = event.target as HTMLInputElement; let trimmed = input.value.replace(/\s+/g, '');
if (trimmed.length > ) {
trimmed = trimmed.substr(, );
} let numbers = [];
for (let i = ; i < trimmed.length; i += ) {
numbers.push(trimmed.substr(i, ));
} input.value = numbers.join(' '); this.border = '';
if (/[^\d]+/.test(trimmed)) {
this.border = '1px solid red';
} }
}
test:
import { DebugElement, Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { By } from '@angular/platform-browser';
import { CreditCardDirective } from './credit-card.directive'; TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
); @Component({
template: `
<input type="text" [value]="value" credit-card>
`
})
class TestComponent {
value = ;
} describe('CreditCardDirective', () => { let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let el: DebugElement; beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
CreditCardDirective,
TestComponent
]
});
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
el = fixture.debugElement;
}); it('should format the string with spaces', () => {
const directive = el.query(By.directive(CreditCardDirective)).nativeElement;
directive.value = '';
directive.dispatchEvent(new Event('input'));
expect(directive.value).toBe('4751 23');
directive.value = '';
directive.dispatchEvent(new Event('input'));
expect(directive.value).toBe('4751 2398 1201 9201');
}); it('should have a max-length of 16 characters', () => {
const directive = el.query(By.directive(CreditCardDirective));
const directiveEl = directive.nativeElement;
directiveEl.value = '';
directiveEl.dispatchEvent(new Event('input'));
expect(directiveEl.value).toBe('4751 2398 1201 9201');
directiveEl.value = 'abscdef';
directiveEl.dispatchEvent(new Event('input'));
const directiveInstance = directive.injector.get(CreditCardDirective);
expect(directiveInstance.border).toContain('1px solid red');
}); });
[Angular] Test Directive的更多相关文章
- [Angular 2] Directive intro and exportAs
First, What is directive, what is the difference between component and directive. For my understandi ...
- 关于angular 自定义directive
关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...
- [Angular] Custom directive Form validator
Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...
- [Angular] Export directive functionalities by using 'exportAs'
Directive ables to change component behaives and lookings. Directive can also export some APIs which ...
- [Angular] Using directive to create a simple Credit card validator
We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...
- angular service/directive
<html class=" js cssanimations csstransitions" ng-app="phonecatApp" > < ...
- 一个Demo就懂的Angular之directive
<body> <div ng-controller="myCtrl"> <hello-word></hello-word> < ...
- angular 中 directive中的多个指令
<div ng-controller="ctrl1"> <superman weight length speed>superman</superma ...
- Angular中directive——scope选项与绑定策略,这个也经常迷惑的。
开门见山地说,scope:{}使指令与外界隔离开来,使其模板(template)处于non-inheriting(无继承)的状态,当然除非你在其中使用了transclude嵌入,这点之后的笔记会再详细 ...
随机推荐
- 高速入手ITOO导入-改进导入.xlsx格式
这两天一直在研究师哥的导入,在他的基础上进行了一些改进.这次的改进就是能够导入还有一种格式.xlsx格式的.经过几番调试和看师哥写的底层代码最终知道哪里的问题了. workbook = new HSS ...
- OpenCASCADE Job - 上海地目
- SDUT--Pots(二维BFS)
Pots Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 You are given two pots, having the ...
- mysql 压力测试之批量插入自增字段不连续问题
Gaps in auto-increment values for “bulk inserts” With innodb_autoinc_lock_mode set to 0 (“traditiona ...
- ftp实现图片上传,文件也类似
本来用得是easyui得控件 点击按钮实现选择图片得 ,但是老板非得要双击图片框实现上传代码....做个简单得记录 前端代码: 首先,<form>表单要加上 enctype="m ...
- JS学习笔记 - Try / Catch / Finally
<body> <p>请输入 5 和 10 之间的一个数:</p> <input id="demo" type="text&quo ...
- BZOJ4182: Shopping(点分治,树上背包)
Description 马上就是小苗的生日了,为了给小苗准备礼物,小葱兴冲冲地来到了商店街.商店街有n个商店,并且它们之间的道路构成了一颗树的形状. 第i个商店只卖第i种物品,小苗对于这种物品的喜爱度 ...
- CISP/CISA 每日一题 七
CISA 每日一题(答) 确保只有恰当授权的出站交易才能被处理,控制目的: 1.出站交易是基于授权而被启动: 2.出站交易包含了唯一的预先授权的交易类型: 3.出站交易只能被发送到合法的商业伙伴那里. ...
- 【习题 7-6 UVA - 12113】Overlapping Squares
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出来一个正方形. 然后每次枚举新加的正方形左上角的坐标就可以. 注意覆盖的规则,控制一下就可以. 然后暴力判断是否相同. 暴 ...
- 【Codeforces Round #451 (Div. 2) C】Phone Numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map<string,vector > dic;模拟就好. 后缀.翻转一下就变成前缀了. 两重循环剔除这种情况不输出就 ...