zoukankan      html  css  js  c++  java
  • [Angular Unit Testing] Testing Component methods

    import {ComponentFixture, TestBed} from '@angular/core/testing';
    import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
    
    import {StockCounterComponent} from './stock-counter.component';
    
    TestBed.initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
    );
    
    describe('StockCounterComponent', () => {
    
      let component: StockCounterComponent;
      let fixture: ComponentFixture<StockCounterComponent>;
    
      beforeEach(() => {
        TestBed.configureTestingModule({
          declarations: [
            StockCounterComponent
          ]
        });
    
        fixture = TestBed.createComponent(StockCounterComponent);
        component = fixture.componentInstance;
    
        component.value = 0;
      });
    
      it('should increment correctly', () => {
        component.increment()
        expect(component.value).toBe(1);
      });
    
      it('should decrement correctly', () => {
        component.increment()
        expect(component.value).toBe(1);
        component.decrement()
        expect(component.value).toBe(0);
      });
    
      it('should not decrement below the minimum value', () => {
        component.increment()
        expect(component.value).toBe(1);
        component.decrement()
        expect(component.value).toBe(0);
        component.decrement()
        expect(component.value).toBe(0);
      });
    
      it('should not increment below the maximum value', () => {
        for (let i = 0; i < 200; i++) {
          component.increment();
        }
        expect(component.value).toBe(100);
      });
    });

    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();
      }
    
    }
  • 相关阅读:
    【转】浏览器兼容性问题汇总
    【转】sql server数据库操作大全——常用语句/技巧集锦/经典语句
    如何在数据库中导入excel文件内的数据
    【总算解决了】A network-related or instance-specific error occurred while establishing a connection to SQL Server
    【转】JS容器拖拽效果,并通过cookie保存拖拽各容器的所在位置
    【转】SQL多条件模糊查询解决方案-存储过程
    ASP搜索查询
    解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问
    简单鼠标跟随代码
    【JS】jquery通知插件toastr
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6801976.html
Copyright © 2011-2022 走看看