日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于Angular Component changeDetection策略设置成OnPush的一个单元测试局限性

發布時間:2023/12/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于Angular Component changeDetection策略设置成OnPush的一个单元测试局限性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

局限性:如果Component的changeDetection的值為ChangeDetectionStrategy.OnPush而不是Default,那么fixture.detectChanges()只有在一個test spec里第一次調用才會生效。

這是Angular一個已知的bug:https://github.com/angular/angular/issues/12313

解決方案:

beforeEach(() => {TestBed.configureTestingModule({imports: [OutletModule],declarations: [TableComponent],providers: [{ provide: TableRendererService, useClass: MockTableRendererService },],}).overrideComponent(TableComponent, {set: { changeDetection: ChangeDetectionStrategy.Default },}).compileComponents();fixture = TestBed.createComponent(TableComponent);tableComponent = fixture.componentInstance;tableRendererService = TestBed.inject(TableRendererService);fixture.detectChanges();});

ChangeDetectionStrategy.OnPush實際是一個枚舉值,在Angular官網里有介紹:

enum ChangeDetectionStrategy {OnPush: 0Default: 1 }

區別

  • OnPush: 0

Use the CheckOnce strategy, meaning that automatic change detection is deactivated until reactivated by setting the strategy to Default (CheckAlways). Change detection can still be explicitly invoked. This strategy applies to all child directives and cannot be overridden.

注意:仍然可以顯式觸發。

  • Default: 1

Use the default CheckAlways strategy, in which change detection is automatic until explicitly deactivated.

我們甚至可以更改某個Component的change detection頻率。

Detach change detector to limit how often check occurs
The following example defines a component with a large list of read-only data that is expected to change constantly, many times per second. To improve performance, we want to check and update the list less often than the changes actually occur. To do that, we detach the component’s change detector and perform an explicit local check every five seconds.

class DataListProvider {// in a real application the returned data will be different every timeget data() {return [1, 2, 3, 4, 5];} }@Component({selector: 'giant-list',template: `<li *ngFor="let d of dataProvider.data">Data {ozvdkddzhkzd}</li>`, }) class GiantList {constructor(private ref: ChangeDetectorRef, public dataProvider: DataListProvider) {ref.detach();setInterval(() => {this.ref.detectChanges();}, 5000);} }@Component({selector: 'app',providers: [DataListProvider],template: `<giant-list></giant-list>`, }) class App { }

Angular應用 changeDetection模式的設置框架代碼

根據關鍵字ChangeDetectionStrategy搜索即可。

const createInject = makeMetadataFactory('Inject', (token) => ({ token })); const createInjectionToken = makeMetadataFactory('InjectionToken', (desc) => ({ _desc: desc, ?prov: undefined })); const createAttribute = makeMetadataFactory('Attribute', (attributeName) => ({ attributeName })); const createContentChildren = makeMetadataFactory('ContentChildren', (selector, data = {}) => (Object.assign({ selector, first: false, isViewQuery: false, descendants: false }, data))); const createContentChild = makeMetadataFactory('ContentChild', (selector, data = {}) => (Object.assign({ selector, first: true, isViewQuery: false, descendants: true }, data))); const createViewChildren = makeMetadataFactory('ViewChildren', (selector, data = {}) => (Object.assign({ selector, first: false, isViewQuery: true, descendants: true }, data))); const createViewChild = makeMetadataFactory('ViewChild', (selector, data) => (Object.assign({ selector, first: true, isViewQuery: true, descendants: true }, data))); const createDirective = makeMetadataFactory('Directive', (dir = {}) => dir); var ViewEncapsulation; (function (ViewEncapsulation) {ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";ViewEncapsulation[ViewEncapsulation["Native"] = 1] = "Native";ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom"; })(ViewEncapsulation || (ViewEncapsulation = {})); var ChangeDetectionStrategy; (function (ChangeDetectionStrategy) {ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default"; })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {})); const createComponent = makeMetadataFactory('Component', (c = {}) => (Object.assign({ changeDetection: ChangeDetectionStrategy.Default }, c))); const createPipe = makeMetadataFactory('Pipe', (p) => (Object.assign({ pure: true }, p))); const createInput = makeMetadataFactory('Input', (bindingPropertyName) => ({ bindingPropertyName })); const createOutput = makeMetadataFactory('Output', (bindingPropertyName) => ({ bindingPropertyName })); const createHostBinding = makeMetadataFactory('HostBinding', (hostPropertyName) => ({ hostPropertyName })); const createHostListener = makeMetadataFactory('HostListener', (eventName, args) => ({ eventName, args })); const createNgModule = makeMetadataFactory('NgModule', (ngModule) => ngModule); const createInjectable = makeMetadataFactory('Injectable', (injectable = {}) => injectable);//告訴Angular,你正在使用自定義元素 const CUSTOM_ELEMENTS_SCHEMA = {name: 'custom-elements' }; ```![在這里插入圖片描述](https://img-blog.csdnimg.cn/20210213110729716.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2kwNDI0MTY=,size_16,color_FFFFFF,t_70)

總結

以上是生活随笔為你收集整理的关于Angular Component changeDetection策略设置成OnPush的一个单元测试局限性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。