Skip to content

Commit 9995e75

Browse files
committed
fix(youtube-player): Eliminate any casts
1 parent 47eaf45 commit 9995e75

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/youtube-player/fake-youtube-player.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function createFakeYtNamespace(): FakeYtNamespace {
6363
]);
6464

6565
let playerConfig: YT.PlayerOptions | undefined;
66-
const boundListeners = new Map<keyof YT.Events, Set<(event: any) => void>>();
66+
const boundListeners = new Map<keyof YT.Events, Set<(event: unknown) => void>>();
6767
const playerCtorSpy = jasmine.createSpy('Player Constructor');
6868

6969
// The spy target function cannot be an arrow-function as this breaks when created through `new`.
@@ -72,28 +72,28 @@ export function createFakeYtNamespace(): FakeYtNamespace {
7272
return playerSpy;
7373
});
7474

75-
playerSpy.addEventListener.and.callFake((name: keyof YT.Events, listener: (e: any) => any) => {
76-
if (!boundListeners.has(name)) {
77-
boundListeners.set(name, new Set());
78-
}
79-
boundListeners.get(name)!.add(listener);
80-
});
75+
playerSpy.addEventListener.and.callFake(
76+
(name: keyof YT.Events, listener: (e: unknown) => unknown) => {
77+
if (!boundListeners.has(name)) {
78+
boundListeners.set(name, new Set());
79+
}
80+
boundListeners.get(name)!.add(listener);
81+
},
82+
);
8183

82-
playerSpy.removeEventListener.and.callFake((name: keyof YT.Events, listener: (e: any) => any) => {
83-
if (boundListeners.has(name)) {
84-
boundListeners.get(name)!.delete(listener);
85-
}
86-
});
84+
playerSpy.removeEventListener.and.callFake(
85+
(name: keyof YT.Events, listener: (e: unknown) => unknown) => {
86+
boundListeners.get(name)?.delete(listener);
87+
},
88+
);
8789

8890
function eventHandlerFactory(name: keyof YT.Events) {
8991
return (arg: Object = {}) => {
9092
if (!playerConfig) {
9193
throw new Error(`Player not initialized before ${name} called`);
9294
}
9395

94-
if (boundListeners.has(name)) {
95-
boundListeners.get(name)!.forEach(callback => callback(arg));
96-
}
96+
boundListeners.get(name)?.forEach(callback => callback(arg));
9797
};
9898
}
9999

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {
1010
} from './youtube-player';
1111
import {PlaceholderImageQuality} from './youtube-player-placeholder';
1212

13+
declare var window: Window;
14+
1315
const VIDEO_ID = 'a12345';
14-
const YT_LOADING_STATE_MOCK = {loading: 1, loaded: 0};
16+
const YT_LOADING_STATE_MOCK = {loading: 1, loaded: 0} as unknown as typeof YT;
1517
const TEST_PROVIDERS: (Provider | EnvironmentProviders)[] = [
1618
{
1719
provide: YOUTUBE_PLAYER_CONFIG,
@@ -56,7 +58,7 @@ describe('YoutubePlayer', () => {
5658
});
5759

5860
afterEach(() => {
59-
(window as any).YT = undefined;
61+
window.YT = undefined;
6062
window.onYouTubeIframeAPIReady = undefined;
6163
});
6264

@@ -540,17 +542,17 @@ describe('YoutubePlayer', () => {
540542
let api: typeof YT;
541543

542544
beforeEach(() => {
543-
api = window.YT;
544-
(window as any).YT = undefined;
545+
api = window.YT!;
546+
window.YT = undefined;
545547
});
546548

547549
afterEach(() => {
548-
(window as any).YT = undefined;
550+
window.YT = undefined;
549551
window.onYouTubeIframeAPIReady = undefined;
550552
});
551553

552554
it('waits until the api is ready before initializing', () => {
553-
(window.YT as any) = YT_LOADING_STATE_MOCK;
555+
window.YT = YT_LOADING_STATE_MOCK;
554556
TestBed.configureTestingModule({providers: TEST_PROVIDERS});
555557
fixture = TestBed.createComponent(TestApp);
556558
testComponent = fixture.debugElement.componentInstance;
@@ -560,7 +562,7 @@ describe('YoutubePlayer', () => {
560562

561563
expect(playerCtorSpy).not.toHaveBeenCalled();
562564

563-
window.YT = api!;
565+
window.YT = api;
564566
window.onYouTubeIframeAPIReady!();
565567

566568
expect(playerCtorSpy).toHaveBeenCalledWith(
@@ -585,7 +587,7 @@ describe('YoutubePlayer', () => {
585587

586588
expect(playerCtorSpy).not.toHaveBeenCalled();
587589

588-
window.YT = api!;
590+
window.YT = api;
589591
window.onYouTubeIframeAPIReady!();
590592

591593
expect(spy).toHaveBeenCalled();
@@ -601,7 +603,7 @@ describe('YoutubePlayer', () => {
601603
});
602604

603605
afterEach(() => {
604-
fixture = testComponent = (window as any).YT = window.onYouTubeIframeAPIReady = undefined!;
606+
fixture = testComponent = window.YT = window.onYouTubeIframeAPIReady = undefined!;
605607
});
606608

607609
it('should show a placeholder', () => {

0 commit comments

Comments
 (0)