|
8 | 8 |
|
9 | 9 |
|
10 | 10 | import {CommonModule} from '@angular/common'; |
11 | | -import {ApplicationRef, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentRef, createComponent, Directive, DoCheck, EmbeddedViewRef, ErrorHandler, EventEmitter, inject, Input, NgModule, OnInit, Output, QueryList, TemplateRef, Type, ViewChild, ViewChildren, ViewContainerRef} from '@angular/core'; |
| 11 | +import {ApplicationRef, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentRef, Directive, DoCheck, EmbeddedViewRef, ErrorHandler, EventEmitter, inject, Input, NgModule, OnInit, Output, QueryList, TemplateRef, Type, ViewChild, ViewChildren, ViewContainerRef, ɵgetEnsureDirtyViewsAreAlwaysReachable, ɵsetEnsureDirtyViewsAreAlwaysReachable} from '@angular/core'; |
12 | 12 | import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; |
13 | 13 | import {expect} from '@angular/platform-browser/testing/src/matchers'; |
14 | 14 | import {BehaviorSubject} from 'rxjs'; |
@@ -184,57 +184,68 @@ describe('change detection', () => { |
184 | 184 |
|
185 | 185 | describe('markForCheck', () => { |
186 | 186 | it('should mark OnPush ancestor of dynamically created component views as dirty', () => { |
| 187 | + const previous = ɵgetEnsureDirtyViewsAreAlwaysReachable(); |
| 188 | + ɵsetEnsureDirtyViewsAreAlwaysReachable(true); |
187 | 189 | @Component({ |
188 | 190 | selector: `test-cmpt`, |
189 | 191 | template: `{{counter}}|<ng-template #vc></ng-template>`, |
190 | | - changeDetection: ChangeDetectionStrategy.OnPush |
| 192 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 193 | + standalone: true, |
191 | 194 | }) |
192 | 195 | class TestCmpt { |
193 | 196 | counter = 0; |
194 | 197 | @ViewChild('vc', {read: ViewContainerRef}) vcRef!: ViewContainerRef; |
195 | 198 |
|
196 | | - constructor() {} |
197 | | - |
198 | 199 | createComponentView<T>(cmptType: Type<T>): ComponentRef<T> { |
199 | 200 | return this.vcRef.createComponent(cmptType); |
200 | 201 | } |
201 | 202 | } |
202 | 203 |
|
203 | 204 | @Component({ |
204 | 205 | selector: 'dynamic-cmpt', |
205 | | - template: `dynamic`, |
| 206 | + template: `dynamic|{{binding}}`, |
| 207 | + standalone: true, |
206 | 208 | changeDetection: ChangeDetectionStrategy.OnPush |
207 | 209 | }) |
208 | 210 | class DynamicCmpt { |
| 211 | + @Input() binding = 'binding'; |
209 | 212 | } |
210 | 213 |
|
211 | | - @NgModule({declarations: [DynamicCmpt]}) |
212 | | - class DynamicModule { |
213 | | - } |
214 | | - |
215 | | - TestBed.configureTestingModule({imports: [DynamicModule], declarations: [TestCmpt]}); |
216 | | - |
217 | 214 | const fixture = TestBed.createComponent(TestCmpt); |
218 | 215 |
|
219 | 216 | // initial CD to have query results |
220 | 217 | // NOTE: we call change detection without checkNoChanges to have clearer picture |
221 | 218 | fixture.detectChanges(false); |
222 | 219 | expect(fixture.nativeElement).toHaveText('0|'); |
223 | 220 |
|
224 | | - // insert a dynamic component |
| 221 | + // insert a dynamic component, but do not specifically mark parent dirty |
| 222 | + // (dynamic components with OnPush flag are created with the `Dirty` flag) |
225 | 223 | const dynamicCmptRef = fixture.componentInstance.createComponentView(DynamicCmpt); |
226 | 224 | fixture.detectChanges(false); |
227 | | - expect(fixture.nativeElement).toHaveText('0|dynamic'); |
| 225 | + expect(fixture.nativeElement).toHaveText('0|dynamic|binding'); |
228 | 226 |
|
229 | 227 | // update model in the OnPush component - should not update UI |
230 | 228 | fixture.componentInstance.counter = 1; |
231 | 229 | fixture.detectChanges(false); |
232 | | - expect(fixture.nativeElement).toHaveText('0|dynamic'); |
| 230 | + expect(fixture.nativeElement).toHaveText('0|dynamic|binding'); |
233 | 231 |
|
234 | 232 | // now mark the dynamically inserted component as dirty |
235 | 233 | dynamicCmptRef.changeDetectorRef.markForCheck(); |
236 | 234 | fixture.detectChanges(false); |
237 | | - expect(fixture.nativeElement).toHaveText('1|dynamic'); |
| 235 | + expect(fixture.nativeElement).toHaveText('1|dynamic|binding'); |
| 236 | + |
| 237 | + // Update, mark for check, and detach before change detection, should not update |
| 238 | + dynamicCmptRef.setInput('binding', 'updatedBinding'); |
| 239 | + dynamicCmptRef.changeDetectorRef.markForCheck(); |
| 240 | + dynamicCmptRef.changeDetectorRef.detach(); |
| 241 | + fixture.detectChanges(false); |
| 242 | + expect(fixture.nativeElement).toHaveText('1|dynamic|binding'); |
| 243 | + |
| 244 | + // reattaching and run CD from the top should update |
| 245 | + dynamicCmptRef.changeDetectorRef.reattach(); |
| 246 | + fixture.detectChanges(false); |
| 247 | + expect(fixture.nativeElement).toHaveText('1|dynamic|updatedBinding'); |
| 248 | + ɵsetEnsureDirtyViewsAreAlwaysReachable(previous); |
238 | 249 | }); |
239 | 250 |
|
240 | 251 | it('should support re-enterant change detection', () => { |
|
0 commit comments