在jasmine beforeEach里修改UI元素的一个side effect

简介: 在jasmine beforeEach里修改UI元素的一个side effect

单元测试源代码:import { Component } from '@angular/core';

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ReactiveFormsModule } from '@angular/forms';

import { ActivatedRoute } from '@angular/router';

import {

 CheckoutDeliveryService,

 DeliveryMode,

 I18nTestingModule,

} from '@spartacus/core';

import { Observable, of } from 'rxjs';

import { CheckoutConfigService } from '../../services/checkout-config.service';

import { CheckoutStepService } from '../../services/checkout-step.service';

import { DeliveryModeComponent } from './delivery-mode.component';

import createSpy = jasmine.createSpy;

import { LoaderState } from '../../../../../../core/src/state/utils/loader';

import { By } from '@angular/platform-browser';

@Component({

 selector: 'cx-spinner',

 template: '',

})

class MockSpinnerComponent {}

class MockCheckoutDeliveryService {

 loadSupportedDeliveryModes = createSpy();

 setDeliveryMode = createSpy();

 getSupportedDeliveryModes(): Observable {

   return of();

 }

 getSelectedDeliveryMode(): Observable {

   return of();

 }

 getLoadSupportedDeliveryModeProcess(): Observable> {

   return of();

 }

}

class MockCheckoutConfigService {

 getPreferredDeliveryMode(): string {

   return '';

 }

}

class MockCheckoutStepService {

 next = createSpy();

 back = createSpy();

 getBackBntText(): string {

   return 'common.back';

 }

}

const mockActivatedRoute = {

 snapshot: {

   url: ['checkout', 'delivery-mode'],

 },

};

describe('DeliveryModeComponent', () => {

 let component: DeliveryModeComponent;

 let fixture: ComponentFixture;

 beforeEach(async(() => {

   TestBed.configureTestingModule({

     imports: [ReactiveFormsModule, I18nTestingModule],

     declarations: [DeliveryModeComponent, MockSpinnerComponent],

     providers: [

       {

         provide: CheckoutDeliveryService,

         useClass: MockCheckoutDeliveryService,

       },

       { provide: CheckoutStepService, useClass: MockCheckoutStepService },

       { provide: CheckoutConfigService, useClass: MockCheckoutConfigService },

       { provide: ActivatedRoute, useValue: mockActivatedRoute },

     ],

   }).compileComponents();

 }));

 beforeEach(() => {

   fixture = TestBed.createComponent(DeliveryModeComponent);

   component = fixture.componentInstance;

   console.log('beforeEach outside continue button describe');

 });

 describe('continue button', () =>{

   const getContinueBtn = () => fixture.debugElement.query(By.css('.cx-checkout-btns .btn-primary'));

   const setDeliveryModeId = (value: string) =>  

     component.mode.controls['deliveryModeId'].setValue(value);

   beforeEach(() => {

     setDeliveryModeId('aa');

     fixture.detectChanges();

     let button = getContinueBtn();

     console.log('beforeEach inside continue button describe, component.deliveryModeInvalid: ' +  

     component.deliveryModeInvalid + ' button.disabled: ' + button.nativeElement.disabled );

     setDeliveryModeId(null);

     fixture.detectChanges();

     console.log('set delivery mode id to null!');

     button = getContinueBtn();

     console.log('after that, component.deliveryModeInvalid: ' +  

     component.deliveryModeInvalid + ' button.disabled: ' + button.nativeElement.disabled );

   });

   

   it('should be available', () => {

     const button = getContinueBtn();

     console.log('fixture.detectChanges is put in beforeEach: ' + button);

     //fixture.detectChanges();

     //button = getContinueBtn();

     //console.log('after call fixture.detectChanges: ' + button);

     expect(button).toBeTruthy();

   });

   /*it('should be disabled when delivery mode is not selected', () => {

     setDeliveryModeId(null);

     fixture.detectChanges();

     expect(getContinueBtn().nativeElement.disabled).toBe(true);

   });*/

 });

});

image.pngimage.pngimage.pngimage.png

目录
相关文章
|
8月前
|
Web App开发 前端开发 JavaScript
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
36 0
|
8月前
|
XML 数据格式
SAP UI5 应用 manifest.json 文件里 Routes 数组元素的相对顺序,不可忽视的试读版
SAP UI5 应用 manifest.json 文件里 Routes 数组元素的相对顺序,不可忽视的试读版
49 0
|
10月前
|
前端开发 JavaScript 中间件
关于浮动元素,你还在自己计算位置吗?来看看 Floating UI 吧!
关于浮动元素,你还在自己计算位置吗?来看看 Floating UI 吧!
138 0
|
Web App开发 开发者 iOS开发
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
SAP UI5 SimpleForm 里在水平方向显示多组 Form 元素的实现方法试读版
|
开发工具 Android开发 iOS开发
如何使用 Draggable 和 DragTarget 在 Flutter 中创建拖放 UI 元素?
如何使用 Draggable 和 DragTarget 在 Flutter 中创建拖放 UI 元素?
352 0
|
JavaScript 索引
面试题分享,修改数据无法更新UI
面试题分享,修改数据无法更新UI
112 0
面试题分享,修改数据无法更新UI
|
API
SAP Spartacus Cart UI 修改 quantity 字段后的 Patch 请求遇到 400 错误 - IllegalArgumentError
SAP Spartacus Cart UI 修改 quantity 字段后的 Patch 请求遇到 400 错误 - IllegalArgumentError
165 0
SAP Spartacus Cart UI 修改 quantity 字段后的 Patch 请求遇到 400 错误 - IllegalArgumentError
|
资源调度
SAP 电商云 Spartacus UI 修改代码后,重新构建基于 SSR 版本的程序报错
SAP 电商云 Spartacus UI 修改代码后,重新构建基于 SSR 版本的程序报错
144 0
SAP 电商云 Spartacus UI 修改代码后,重新构建基于 SSR 版本的程序报错
SAP 电商云 Spartacus UI 修改 Delivery Mode 触发的三个 HTTP 请求(二)
SAP 电商云 Spartacus UI 修改 Delivery Mode 触发的三个 HTTP 请求
70 0
SAP 电商云 Spartacus UI 修改 Delivery Mode 触发的三个 HTTP 请求(二)
SAP 电商云 Spartacus UI 修改 Delivery Mode 触发的三个 HTTP 请求(一)
SAP 电商云 Spartacus UI 修改 Delivery Mode 触发的三个 HTTP 请求
110 0
SAP 电商云 Spartacus UI 修改 Delivery Mode 触发的三个 HTTP 请求(一)

热门文章

最新文章