Angular jasmine fixture.detectChanges如何触发directive的set方法

简介: Angular jasmine fixture.detectChanges如何触发directive的set方法

import { Component } from '@angular/core';

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

import { FocusDirective } from './focus.directive';

import { KeyboardFocusService } from './services';

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

@Component({

 selector: 'cx-host',

 template: `

`,

})

class MockComponent {

 jerry = 1;

}

class MockKeyboardFocusService {

 get() {}

 set() {}

 shouldFocus() {}

 getPersistenceGroup() {}

 findFirstFocusable(){}

 hasPersistedFocus(){}

}

describe('FocusDirective', () => {

 let fixture: ComponentFixture;

 let component: MockComponent;

 beforeEach(

   waitForAsync(() => {

     TestBed.configureTestingModule({

       declarations: [FocusDirective, MockComponent],

       providers: [

         {

           provide: KeyboardFocusService,

           useClass: MockKeyboardFocusService,

         },

       ],

     }).compileComponents();

     fixture = TestBed.createComponent(MockComponent);

     component = fixture.componentInstance;

     fixture.detectChanges();

   })

 );

 it('should focus itself', () => {

   let service: KeyboardFocusService;

   service = TestBed.inject(KeyboardFocusService);

   const host = fixture.debugElement.query(By.css('#b'));

   const el = host.nativeElement;

   spyOn(service, 'findFirstFocusable').and.returnValue(el);

   spyOn(el, 'focus').and.callThrough();

 

   fixture.detectChanges();

   const event = {

     preventDefault: () => {},

     stopPropagation: () => {},

   };

   host.triggerEventHandler('focus', event);

   expect(el.focus).toHaveBeenCalled();

   expect(service.findFirstFocusable).toHaveBeenCalled();

   component.jerry = 2;

   fixture.detectChanges();

 });

});image.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.png

目录
相关文章
|
1月前
Angular启动/node_modules/@types/node/index.d.ts (20,1): Invalid ‘reference‘ directive syntax.
Angular启动/node_modules/@types/node/index.d.ts (20,1): Invalid ‘reference‘ directive syntax.
20 2
|
1月前
|
JavaScript 索引
在使用`Vue.$set`方法时,需要注意哪些事项?
在使用`Vue.$set`方法时,需要注意哪些事项?
13 0
|
1月前
|
JavaScript
Vue.js 实用技巧:深入理解 Vue.set 方法
Vue.js 实用技巧:深入理解 Vue.set 方法
|
2月前
|
存储 Java
java set集合相关介绍和方法使用操作
java set集合相关介绍和方法使用操作
27 2
|
3月前
|
JavaScript 索引
Vue.set 方法原理
Vue.set 方法原理
12 0
|
3月前
|
人工智能 JavaScript 前端开发
让OpenAi给我写个JS的set对象的笔记和快速去重方法
让OpenAi给我写个JS的set对象的笔记和快速去重方法
21 0
|
3月前
|
C++
c++ set、map的四种自定义排序方法
c++ set、map的四种自定义排序方法
83 0
|
8月前
|
SQL 关系型数据库 MySQL
使用IntelliJ IDEA连接数据库,快速创建实体类对象,自动生成get,set,toString方法以及无参有参构造器
使用IntelliJ IDEA连接数据库,快速创建实体类对象,自动生成get,set,toString方法以及无参有参构造器
|
5月前
IDEA 自动生成set方法(GenerateAllSetter插件)
IDEA 自动生成set方法(GenerateAllSetter插件)
515 0
|
6月前
|
JavaScript 前端开发 Java
18jqGrid - 表格数据SET类方法
18jqGrid - 表格数据SET类方法
22 0

热门文章

最新文章