Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
test: add test for staticMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
fikryfahrezy committed Nov 9, 2023
1 parent d6afb2e commit 4c7d8e6
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 51 deletions.
45 changes: 34 additions & 11 deletions src/lib/SveltePlayer.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
<script lang="ts" context="module">
import type { Player as PlayerType } from './players/types';
import players from './players';
const customPlayers: PlayerType[] = [];
export function addCustomPlayer(player: PlayerType) {
customPlayers.push(player);
}
export function removeCustomPlayers() {
customPlayers.length = 0;
}
export function canPlay(url: PlayerUrl) {
for (const Player of [...customPlayers, ...players]) {
if (Player.canPlay(url)) {
return true;
}
}
return false;
}
export function canEnablePIP(url: PlayerUrl) {
for (const Player of [...customPlayers, ...players]) {
if (Player.canEnablePIP && Player.canEnablePIP(url)) {
return true;
}
}
return false;
}
</script>

<script lang="ts">
import type { PlayerMediaRef, SeekToType, SveltePlayerDispatcher } from './types';
import type { RecursivePartial } from './players/utility.types';
Expand All @@ -13,7 +46,6 @@
import merge from 'deepmerge';
import memoize from 'memoize-one';
import Player from './PlayerMedia.svelte';
import players from './players';
import Preview from './Preview.svelte';
import { defaultConfig } from './props';
Expand Down Expand Up @@ -44,15 +76,6 @@
let playerRef: PlayerMediaRef;
export function canEnablePIP(url: PlayerUrl) {
for (const Player of [...players]) {
if (Player.canEnablePIP && Player.canEnablePIP(url)) {
return true;
}
}
return false;
}
$: showPreviewState = !!light;
function handleClickPreview(e: Event) {
Expand Down Expand Up @@ -138,7 +161,7 @@
</Preview>
{/if}
{:else}
{#each players as player}
{#each [...customPlayers, ...players] as player}
{#if player.canPlay(url)}
<Player
{url}
Expand Down
13 changes: 13 additions & 0 deletions src/lib/players/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { supportsWebKitPresentationMode } from './utils';
const players: Player[] = [
{
key: 'youtube',
name: 'YouTube',
canPlay: canPlayYoutube,
canEnablePIP: undefined,
loopOnEnded: undefined,
Expand All @@ -29,6 +30,7 @@ const players: Player[] = [
},
{
key: 'soundcloud',
name: 'SoundCloud',
canPlay: canPlaySoundCloud,
canEnablePIP: undefined,
loopOnEnded: true,
Expand All @@ -39,6 +41,7 @@ const players: Player[] = [
},
{
key: 'vimeo',
name: 'Vimeo',
canPlay: canPlayVimeo,
canEnablePIP: undefined,
loopOnEnded: undefined,
Expand All @@ -49,6 +52,7 @@ const players: Player[] = [
},
{
key: 'facebook',
name: 'Facebook',
canPlay: canPlayFacebook,
canEnablePIP: undefined,
loopOnEnded: true,
Expand All @@ -59,6 +63,7 @@ const players: Player[] = [
},
{
key: 'streamable',
name: 'Streamable',
canPlay: canPlayStreamable,
canEnablePIP: undefined,
loopOnEnded: undefined,
Expand All @@ -69,6 +74,7 @@ const players: Player[] = [
},
{
key: 'wistia',
name: 'Wistia',
canPlay: canPlayWistia,
canEnablePIP: undefined,
loopOnEnded: true,
Expand All @@ -79,6 +85,7 @@ const players: Player[] = [
},
{
key: 'twitch',
name: 'Twitch',
canPlay: canPlayTwitch,
canEnablePIP: undefined,
loopOnEnded: true,
Expand All @@ -89,6 +96,7 @@ const players: Player[] = [
},
{
key: 'dailymotion',
name: 'DailyMotion',
canPlay: canPlayDailyMotion,
canEnablePIP: undefined,
loopOnEnded: true,
Expand All @@ -99,6 +107,7 @@ const players: Player[] = [
},
{
key: 'mixcloud',
name: 'Mixcloud',
canPlay: canPlayMixcloud,
canEnablePIP: undefined,
loopOnEnded: true,
Expand All @@ -109,6 +118,7 @@ const players: Player[] = [
},
{
key: 'vidyard',
name: 'Vidyard',
canPlay: canPlayVidyard,
canEnablePIP: undefined,
loopOnEnded: undefined,
Expand All @@ -119,6 +129,7 @@ const players: Player[] = [
},
{
key: 'kaltura',
name: 'Kaltura',
canPlay: canPlayKaltura,
canEnablePIP: undefined,
loopOnEnded: undefined,
Expand All @@ -129,6 +140,7 @@ const players: Player[] = [
},
{
key: 'file',
name: 'FilePlayer',
canPlay: canPlayFile,
loopOnEnded: undefined,
forceLoad: undefined,
Expand All @@ -146,6 +158,7 @@ const players: Player[] = [
},
{
key: 'not-implemented',
name: 'NotImplemented',
loopOnEnded: undefined,
forceLoad: undefined,
canPlay() {
Expand Down
48 changes: 17 additions & 31 deletions src/lib/players/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,21 @@ export type PlayerInternalPlayer = Omit<FileInternalPlayer, 'player'> & {

export type PlayerGetPlayerKey = keyof PlayerInternalPlayer;

export type PlayerKey =
| 'youtube'
| 'soundcloud'
| 'vimeo'
| 'facebook'
| 'streamable'
| 'wistia'
| 'twitch'
| 'dailymotion'
| 'mixcloud'
| 'vidyard'
| 'kaltura'
| 'file'
| 'not-implemented';
export type PlayerConfig = {
youtube: YouTubeConfig;
soundcloud: SoundCloudConfig;
vimeo: ViemoConfig;
facebook: FacebookConfig;
streamable: undefined;
wistia: WistiaConfig;
twitch: TwitchConfig;
dailymotion: DailyMotionConfig;
mixcloud: MixcloudConfig;
vidyard: VidyardConfig;
kaltura: undefined;
file: FileConfig;
'not-implemented': NotImplementedConfig;
};

export type PlayerUrl = string | YouTubeUrl | FileUrl;

Expand Down Expand Up @@ -111,22 +112,6 @@ export type PlayerDispatcher = {
loaded: undefined;
};

export type PlayerConfig = {
youtube: YouTubeConfig;
soundcloud: SoundCloudConfig;
vimeo: ViemoConfig;
facebook: FacebookConfig;
streamable: undefined;
wistia: WistiaConfig;
twitch: TwitchConfig;
dailymotion: DailyMotionConfig;
mixcloud: MixcloudConfig;
vidyard: VidyardConfig;
kaltura: undefined;
file: FileConfig;
'not-implemented': NotImplementedConfig;
};

export type PlayerConfigKey = keyof PlayerConfig;

export type PlayerConfigObject = PlayerConfig[PlayerConfigKey];
Expand Down Expand Up @@ -175,7 +160,8 @@ export type PlayerRef = {
export type PlayerComponent = Constructor<SvelteComponent<Partial<PlayerProps>> & PlayerRef>;

export type Player = {
key: PlayerKey;
key: PlayerConfigKey;
name: string;
loopOnEnded?: boolean;
forceLoad?: boolean;
canPlay(url: PlayerUrl): boolean;
Expand Down
8 changes: 1 addition & 7 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
PlayerDispatcher,
PlayerUrl,
PlayerInternalPlayer,
PlayerGetPlayerKey
} from './players/types';
import type { PlayerDispatcher, PlayerInternalPlayer, PlayerGetPlayerKey } from './players/types';

export type SeekToType = 'seconds' | 'fraction';

Expand All @@ -21,7 +16,6 @@ export type PreviewDispatcher = {
};

export type SveltePlayerRef = PlayerMediaRef & {
canEnablePIP(url: PlayerUrl): boolean;
showPreview(): void;
};

Expand Down
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { SveltePlayerRef } from '../lib/types';
import type { OnProgressProps, PlayerUrl } from '../lib/players/types';
import SveltePlayer from '../lib/SveltePlayer.svelte';
import SveltePlayer, { canPlay } from '../lib/SveltePlayer.svelte';
import LoadButton from './LoadButton.svelte';
import Duration from './Duration.svelte';
import screenfull from 'screenfull';
Expand Down Expand Up @@ -166,7 +166,7 @@
Show preview
</button>
{/if}
{#if playerRef !== undefined && playerRef.canEnablePIP(url)}
{#if canPlay(url)}
<label>
<input type="checkbox" bind:checked={pip} />
{!pip ? 'Disable PiP' : 'Enable PiP'}
Expand Down
40 changes: 40 additions & 0 deletions src/test/SveltePlayer/staticMethods.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { canPlay, addCustomPlayer, removeCustomPlayers } from '../../lib/SveltePlayer.svelte';

import { test } from 'vitest';

test('canPlay()', function (t) {
t.expect(canPlay('https://www.youtube.com/watch?v=oUFJJNQGwhk')).toStrictEqual(true);
t.expect(canPlay('https://youtube.com/shorts/370kwJ-x5TY?feature=share')).toStrictEqual(true);
t.expect(canPlay('https://soundcloud.com/miami-nights-1984/accelerated')).toStrictEqual(true);
t.expect(canPlay('https://www.facebook.com/facebook/videos/10153231379946729')).toStrictEqual(
true
);
t.expect(canPlay('https://vimeo.com/90509568')).toStrictEqual(true);
t.expect(canPlay('https://www.twitch.tv/videos/106400740')).toStrictEqual(true);
t.expect(canPlay('https://streamable.com/moo')).toStrictEqual(true);
t.expect(canPlay('https://home.wistia.com/medias/e4a27b971d')).toStrictEqual(true);
t.expect(canPlay('https://www.dailymotion.com/video/x5e9eog')).toStrictEqual(true);
t.expect(canPlay('https://www.mixcloud.com/mixcloud/meet-the-curators')).toStrictEqual(true);
t.expect(canPlay('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4')).toStrictEqual(true);
t.expect(canPlay('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4#t=1')).toStrictEqual(true);
t.expect(canPlay('http://example.com/random/path')).toStrictEqual(false);
});

// TODO: check if player rendered
test('addCustomPlayer()', async (t) => {
t.expect.assertions(2);
addCustomPlayer({
key: 'not-implemented',
name: 'CustomPlayer',
canPlay(url) {
return /example\.com/.test(String(url));
},
loadComponent() {
return import('../../lib/players/NotImplemented.svelte');
}
});

t.expect(canPlay('http://example.com/random/path')).toStrictEqual(true);
removeCustomPlayers();
t.expect(canPlay('http://example.com/random/path')).toStrictEqual(false);
});
2 changes: 2 additions & 0 deletions src/test/players/FilePlayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ afterAll(function () {
});

test('listeners', async function (t) {
t.expect.assertions(4);
const addListeners = vi.fn();
const removeListeners = vi.fn();
const instance = new FilePlayerSvelte({
Expand Down Expand Up @@ -904,6 +905,7 @@ test('auto width/height', function (t) {
});

test('clear srcObject on url change', async function (t) {
t.expect.assertions(2);
const url = new window.MediaStream();
const instance = new FilePlayerSvelte({
target: document.body,
Expand Down

0 comments on commit 4c7d8e6

Please sign in to comment.