单元测试源代码:

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<DeliveryMode[]> {return of();}getSelectedDeliveryMode(): Observable<DeliveryMode> {return of();}getLoadSupportedDeliveryModeProcess(): Observable<LoaderState<void>> {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<DeliveryModeComponent>;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);});*/});
});

先enable,再disable:

Actual test result:


disable不了。

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

在jasmine beforeEach里修改UI元素的一个side effect相关推荐

  1. Swift之深入解析如何使用Xcode和LLDB v2修改UI元素

    一.前言 在上一篇博客中,已经详细地介绍如何使用 LLDB 表达式修改 UI 元素,具体请参考:Swift之深入解析如何将代码添加为自定义LLDB命令. 在这篇博客中,将继续讨论相同的问题需求,并将重 ...

  2. BSP和JSP里的UI元素ID生成逻辑

    CRM WebClient UI WebClient UI渲染出来的DOM元素的这些C#_W#格式的id是在哪行ABAP代码被生成出来的? 参考我的博客WebClient UI element ID ...

  3. 使用XML声明自定义Android UI元素

    如何使用XML声明Android UI元素? #1楼 Google似乎更新了其开发者页面,并在那里添加了各种培训. 其中一个处理自定义视图的创建,可以在这里找到 #2楼 除了大多数投票的答案. obt ...

  4. WPF 中使用附加属性,将任意 UI 元素或控件裁剪成圆形(椭圆)

    原文:WPF 中使用附加属性,将任意 UI 元素或控件裁剪成圆形(椭圆) 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.欢迎转载.使用.重新发布,但务必保 ...

  5. Silverlight 2 DispatcherTimer和通过XAML创建UI元素

    XAML标签元素在silverlight运行时被转换成相应的对象,通过XamlReader类的Load方法,动态创建UI元素: 指定一条XAML内容字符串,为按照XML规则运行,XamlReader. ...

  6. WPF实例秀——如何获取UI元素的图像

    这个标题还真难说明白,我还是再解释一下吧. 比如我想在UI上拖拽某个元素,拖拽过程中,我需要让这个UI元素的影相跟着鼠标移动(但UI还停留在原位),当放开鼠标的时候,UI元素移动到新的位置. 这是个很 ...

  7. 动态控制SAP C4C UI元素的显示和隐藏

    C4C UI上UI元素的显示和隐藏可以通过Key User在Adaptation模式里通过编辑一些简单的rule去控制,诸如这种格式: if ( logic expression = true )th ...

  8. wpf 使子ui元素可视区域不超过父元素_对游戏UI设计的一点思考

    UI决定了一个游戏的初体验,甚至决定了玩家的初始留存,甚至可以说决定了一个游戏的品质,虽然看起来是表象的,却是直指游戏核心的.简单讲,玩家认可一款游戏永远都是造型场景好,剧情好,画质棒:但是玩家骂一款 ...

  9. php修改js内容,js怎样修改html元素的内容?HTML DOM实现修改内容

    js怎样修改html元素的内容?本章就给大家介绍在js中利用HTML DOM是怎样修改html元素内容的.有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助. 首先我们要了解HTML DO ...

最新文章

  1. centos6.5命令安装redis并设置redis自启动,可远程连接
  2. vue-cli eslint 规则
  3. 【Git】git add -A = git add . + git add -u
  4. Qt:Qt实现Winsock网络编程—TCP服务端和客户端通信(多线程)
  5. # 20165208 2017-2018-2 《Java程序设计》第三周错题总结
  6. 《防患未然:实施情报先导的信息安全方法与实践》——2.3 情报循环
  7. IAR软件移植FreeRTOS操作系统软件
  8. docker安装elasticsearch教程
  9. Microsoft Edge浏览器兼容性问题解决方法
  10. Spring Cloud学习笔记(二)Restful理解及微服务的Restful调用
  11. 压力测试-LR工具安装
  12. 远程协助控制电脑的三种方式
  13. 阿里云服务器安装宝塔流程
  14. 微信小程序 canIUse
  15. 时间序列之MATLAB程序
  16. 英国电信日本东芝公开测试英国首个商用量子安全城域网
  17. 关于WLAN被禁用,且启动后还是被禁用的问题
  18. 《区块链技术与应用》北大肖臻老师——课程笔记【1-3】
  19. C# word 转换成pdf 并且修改其中控件内容
  20. 个人计算机特长描述,个人简历特长如何描述

热门文章

  1. 【HDOJ】3948 The Number of Palindromes
  2. [javascript]图解+注释版 Ext.extend()
  3. Vue.js—60分钟快速入门
  4. OSI七层模型每层的功能及包含的协议
  5. 英语口语练习系列-C24-杂技-问候语-乡愁
  6. JAVA Spring 简单的配置和操作 ( 创建实体类, 配置XML文件, 调试 )
  7. KnockoutJS 3.X API 第七章 其他技术(2) 使用扩展器来增加可观察量(监控属性)
  8. 06_排序_希尔排序
  9. 对POST提交数据限制的解决方案
  10. APR 以及linux安装apr 库目的