-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Extend zoneless lint to cover tests (#29193)
Only allow `provideZoneChangeDetection` and `NgZone` in tests that explicitly test integration with Zone.js (and tests which have not yet been migrated to zoneless). (cherry picked from commit df8adda)
- Loading branch information
Showing
9 changed files
with
147 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {Component, ElementRef, ViewChild, provideZoneChangeDetection} from '@angular/core'; | ||
import {ComponentFixture, TestBed, inject, waitForAsync} from '@angular/core/testing'; | ||
import {dispatchFakeEvent} from '../testing/private'; | ||
import {ScrollDispatcher} from './scroll-dispatcher'; | ||
import {CdkScrollable} from './scrollable'; | ||
import {ScrollingModule} from './scrolling-module'; | ||
|
||
describe('ScrollDispatcher Zone.js integration', () => { | ||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ScrollingModule, ScrollingComponent], | ||
providers: [provideZoneChangeDetection()], | ||
}); | ||
|
||
TestBed.compileComponents(); | ||
})); | ||
|
||
describe('Basic usage', () => { | ||
let scroll: ScrollDispatcher; | ||
let fixture: ComponentFixture<ScrollingComponent>; | ||
|
||
beforeEach(inject([ScrollDispatcher], (s: ScrollDispatcher) => { | ||
scroll = s; | ||
|
||
fixture = TestBed.createComponent(ScrollingComponent); | ||
fixture.detectChanges(); | ||
})); | ||
|
||
it('should not execute the global events in the Angular zone', () => { | ||
scroll.scrolled(0).subscribe(() => {}); | ||
dispatchFakeEvent(document, 'scroll', false); | ||
|
||
expect(fixture.ngZone!.isStable).toBe(true); | ||
}); | ||
|
||
it('should not execute the scrollable events in the Angular zone', () => { | ||
dispatchFakeEvent(fixture.componentInstance.scrollingElement.nativeElement, 'scroll'); | ||
expect(fixture.ngZone!.isStable).toBe(true); | ||
}); | ||
}); | ||
}); | ||
|
||
/** Simple component that contains a large div and can be scrolled. */ | ||
@Component({ | ||
template: `<div #scrollingElement cdkScrollable style="height: 9999px"></div>`, | ||
standalone: true, | ||
imports: [ScrollingModule], | ||
}) | ||
class ScrollingComponent { | ||
@ViewChild(CdkScrollable) scrollable: CdkScrollable; | ||
@ViewChild('scrollingElement') scrollingElement: ElementRef<HTMLElement>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {NgZone, provideZoneChangeDetection} from '@angular/core'; | ||
import {TestBed, inject} from '@angular/core/testing'; | ||
import {Subscription} from 'rxjs'; | ||
import {dispatchFakeEvent} from '../testing/private'; | ||
import {ScrollingModule} from './scrolling-module'; | ||
import {ViewportRuler} from './viewport-ruler'; | ||
|
||
describe('ViewportRuler', () => { | ||
let viewportRuler: ViewportRuler; | ||
let ngZone: NgZone; | ||
|
||
// Create a very large element that will make the page scrollable. | ||
let veryLargeElement: HTMLElement = document.createElement('div'); | ||
veryLargeElement.style.width = '6000px'; | ||
veryLargeElement.style.height = '6000px'; | ||
|
||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [ScrollingModule], | ||
providers: [provideZoneChangeDetection(), ViewportRuler], | ||
}), | ||
); | ||
|
||
beforeEach(inject([ViewportRuler, NgZone], (v: ViewportRuler, n: NgZone) => { | ||
viewportRuler = v; | ||
ngZone = n; | ||
scrollTo(0, 0); | ||
})); | ||
|
||
describe('changed event', () => { | ||
it('should run the resize event outside the NgZone', () => { | ||
const spy = jasmine.createSpy('viewport changed spy'); | ||
const subscription = viewportRuler.change(0).subscribe(() => spy(NgZone.isInAngularZone())); | ||
|
||
dispatchFakeEvent(window, 'resize'); | ||
expect(spy).toHaveBeenCalledWith(false); | ||
subscription.unsubscribe(); | ||
}); | ||
|
||
it('should run events outside of the NgZone, even if the subcription is from inside', () => { | ||
const spy = jasmine.createSpy('viewport changed spy'); | ||
let subscription: Subscription; | ||
|
||
ngZone.run(() => { | ||
subscription = viewportRuler.change(0).subscribe(() => spy(NgZone.isInAngularZone())); | ||
dispatchFakeEvent(window, 'resize'); | ||
}); | ||
|
||
expect(spy).toHaveBeenCalledWith(false); | ||
subscription!.unsubscribe(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters