[Angular] Test Directive
2024-08-31 21:41:53
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');
}); });
最新文章
- 学习ES6--data2
- centos6.6 安装 LXC
- win10上安装Docker
- js基础知识总结(全)
- (七)Angularjs - 控制器
- 【自学php】第三天 - 读写文件
- 小强的HTML5移动开发之路(50)——jquerymobile页面初始化过程
- centos7使用cobbler(2.8)批量部署操作系统之一
- iOS - XMPP Openfire 服务器的搭建
- python nonloacal
- ubuntu16.04下g++安装及使用
- Centos 7 Ntop 流量分析 安装
- WeUI移动页面实现时间选择器(年-月-日-时-分)
- Go语言实现数据结构(一)单链表
- myeclipse 代码提示
- Tensorflow從源碼編譯
- centos7 安装java和tomcat9
- linux 相关命令记录
- AJAX传递数组
- linux 虚拟机设置好固定ip 修改了 dns 网络需要设置成桥接模式
热门文章
- Visual Studio Code配置GoLang开发环境
- Http协议的断点续传下载器,使用观察者模式监视下载进度,使用xml保存下载进度。
- 洛谷 P2908 [USACO08OPEN]文字的力量Word Power
- HDU 1848(sg博弈) Fibonacci again and again
- Android: 分页浏览的利器 android View Pager
- 关于C++中的内存泄露
- python序列中是否包含某个元素
- vue 自定义分页组件
- BZOJ——T2190: [SDOI2008]仪仗队
- c++ builder firemonkey 实现填充椭圆