Skip to content

Commit df6718b

Browse files
committed
refactor(youtube-player): avoid build errors
Reworks how we declare globals for the YouTube player so it doesn't break with recent dependency updates. (cherry picked from commit 05386e3)
1 parent 7db3d11 commit df6718b

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/youtube-player/youtube-player.spec.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const TEST_PROVIDERS: (Provider | EnvironmentProviders)[] = [
2525
];
2626

2727
describe('YoutubePlayer', () => {
28+
const ytWindow = window as Window &
29+
typeof globalThis & {
30+
YT?: typeof YT | undefined;
31+
onYouTubeIframeAPIReady?: (() => void) | undefined;
32+
};
2833
let playerCtorSpy: jasmine.Spy;
2934
let playerSpy: jasmine.SpyObj<YT.Player>;
3035
let fixture: ComponentFixture<TestApp>;
@@ -35,7 +40,7 @@ describe('YoutubePlayer', () => {
3540
const fake = createFakeYtNamespace();
3641
playerCtorSpy = fake.playerCtorSpy;
3742
playerSpy = fake.playerSpy;
38-
window.YT = fake.namespace;
43+
ytWindow.YT = fake.namespace;
3944
events = fake.events;
4045
}));
4146

@@ -58,8 +63,8 @@ describe('YoutubePlayer', () => {
5863
});
5964

6065
afterEach(() => {
61-
window.YT = undefined;
62-
window.onYouTubeIframeAPIReady = undefined;
66+
ytWindow.YT = undefined!;
67+
ytWindow.onYouTubeIframeAPIReady = undefined;
6368
});
6469

6570
it('initializes a youtube player when the placeholder is clicked', () => {
@@ -215,7 +220,7 @@ describe('YoutubePlayer', () => {
215220

216221
expect(playerSpy.cueVideoById).not.toHaveBeenCalled();
217222

218-
playerSpy.getPlayerState.and.returnValue(window.YT!.PlayerState.CUED);
223+
playerSpy.getPlayerState.and.returnValue(ytWindow.YT!.PlayerState.CUED);
219224
events.onReady({target: playerSpy});
220225

221226
expect(playerSpy.cueVideoById).toHaveBeenCalledWith(
@@ -542,17 +547,17 @@ describe('YoutubePlayer', () => {
542547
let api: typeof YT;
543548

544549
beforeEach(() => {
545-
api = window.YT!;
546-
window.YT = undefined;
550+
api = ytWindow.YT!;
551+
ytWindow.YT = undefined!;
547552
});
548553

549554
afterEach(() => {
550-
window.YT = undefined;
551-
window.onYouTubeIframeAPIReady = undefined;
555+
ytWindow.YT = undefined!;
556+
ytWindow.onYouTubeIframeAPIReady = undefined;
552557
});
553558

554559
it('waits until the api is ready before initializing', () => {
555-
window.YT = YT_LOADING_STATE_MOCK;
560+
ytWindow.YT = YT_LOADING_STATE_MOCK;
556561
TestBed.configureTestingModule({providers: TEST_PROVIDERS});
557562
fixture = TestBed.createComponent(TestApp);
558563
testComponent = fixture.debugElement.componentInstance;
@@ -562,8 +567,8 @@ describe('YoutubePlayer', () => {
562567

563568
expect(playerCtorSpy).not.toHaveBeenCalled();
564569

565-
window.YT = api;
566-
window.onYouTubeIframeAPIReady!();
570+
ytWindow.YT = api;
571+
ytWindow.onYouTubeIframeAPIReady!();
567572

568573
expect(playerCtorSpy).toHaveBeenCalledWith(
569574
getVideoHost(fixture),
@@ -577,7 +582,7 @@ describe('YoutubePlayer', () => {
577582

578583
it('should not override any pre-existing API loaded callbacks', () => {
579584
const spy = jasmine.createSpy('other API loaded spy');
580-
window.onYouTubeIframeAPIReady = spy;
585+
ytWindow.onYouTubeIframeAPIReady = spy;
581586
TestBed.configureTestingModule({providers: TEST_PROVIDERS});
582587
fixture = TestBed.createComponent(TestApp);
583588
testComponent = fixture.debugElement.componentInstance;
@@ -587,8 +592,8 @@ describe('YoutubePlayer', () => {
587592

588593
expect(playerCtorSpy).not.toHaveBeenCalled();
589594

590-
window.YT = api;
591-
window.onYouTubeIframeAPIReady!();
595+
ytWindow.YT = api;
596+
ytWindow.onYouTubeIframeAPIReady!();
592597

593598
expect(spy).toHaveBeenCalled();
594599
});
@@ -603,7 +608,7 @@ describe('YoutubePlayer', () => {
603608
});
604609

605610
afterEach(() => {
606-
fixture = testComponent = window.YT = window.onYouTubeIframeAPIReady = undefined!;
611+
fixture = testComponent = ytWindow.YT = ytWindow.onYouTubeIframeAPIReady = undefined!;
607612
});
608613

609614
it('should show a placeholder', () => {
@@ -687,7 +692,7 @@ describe('YoutubePlayer', () => {
687692
fixture.detectChanges();
688693

689694
// Simulate player state being PLAYING (autoplay has started the video)
690-
playerSpy.getPlayerState.and.returnValue(window.YT!.PlayerState.PLAYING);
695+
playerSpy.getPlayerState.and.returnValue(ytWindow.YT!.PlayerState.PLAYING);
691696
events.onReady({target: playerSpy});
692697

693698
// Should use seekTo instead of cueVideoById when player is already playing
@@ -724,7 +729,7 @@ describe('YoutubePlayer', () => {
724729
getPlaceholder(staticSecondsApp).click();
725730
staticSecondsApp.detectChanges();
726731

727-
playerSpy.getPlayerState.and.returnValue(window.YT!.PlayerState.CUED);
732+
playerSpy.getPlayerState.and.returnValue(ytWindow.YT!.PlayerState.CUED);
728733
events.onReady({target: playerSpy});
729734

730735
expect(playerSpy.cueVideoById).toHaveBeenCalledWith(

src/youtube-player/youtube-player.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,9 @@ import {Observable, of as observableOf, Subject, BehaviorSubject, fromEventPatte
3838
import {takeUntil, switchMap} from 'rxjs/operators';
3939
import {PlaceholderImageQuality, YouTubePlayerPlaceholder} from './youtube-player-placeholder';
4040

41-
declare global {
42-
interface Window {
43-
YT: typeof YT | undefined;
44-
onYouTubeIframeAPIReady: (() => void) | undefined;
45-
}
46-
}
41+
type YoutubeWindow = {
42+
onYouTubeIframeAPIReady?: (() => void) | undefined;
43+
};
4744

4845
/** Injection token used to configure the `YouTubePlayer`. */
4946
export const YOUTUBE_PLAYER_CONFIG = new InjectionToken<YouTubePlayerConfig>(
@@ -292,7 +289,7 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
292289

293290
if (this._player) {
294291
this._player.destroy();
295-
window.onYouTubeIframeAPIReady = this._existingApiReadyCallback;
292+
(window as YoutubeWindow).onYouTubeIframeAPIReady = this._existingApiReadyCallback;
296293
}
297294

298295
this._playerChanges.complete();
@@ -513,9 +510,9 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
513510
);
514511
}
515512

516-
this._existingApiReadyCallback = window.onYouTubeIframeAPIReady;
513+
this._existingApiReadyCallback = (window as YoutubeWindow).onYouTubeIframeAPIReady;
517514

518-
window.onYouTubeIframeAPIReady = () => {
515+
(window as YoutubeWindow).onYouTubeIframeAPIReady = () => {
519516
this._existingApiReadyCallback?.();
520517
this._ngZone.run(() => this._createPlayer(playVideo));
521518
};

0 commit comments

Comments
 (0)