• [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 > 16) {
          trimmed = trimmed.substr(0, 16);
        }
    
        let numbers = [];
        for (let i = 0; i < trimmed.length; i += 4) {
          numbers.push(trimmed.substr(i, 4));
        }
    
        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 = 123456;
    }
    
    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 = '475123';
        directive.dispatchEvent(new Event('input'));
        expect(directive.value).toBe('4751 23');
        directive.value = '4751239812019201';
        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 = '4751239812019201998394282394823';
        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');
      });
    
    });
  • 相关阅读:
    关于云计算的大局观
    定制WPF中的DataGrid控件支持对不同的实体类实现中文标题显示
    优化网站设计(二):使用CDN
    MVVM实践中的Command与CommandParameter的使用
    设计对浏览器更加友好的网站——整合搜索功能(针对Google Chrome)
    使用javascript脚本增强SharePoint Survey(调查)的功能
    学习和自我学习
    你应该如何更好地利用搜索引擎
    使用Project Linker实现多个项目中代码文件的链接
    SharePoint 2010 大局观(1~3)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6833563.html
Copyright © 2020-2023  润新知