@playpilot/tpi 8.29.10 → 8.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,72 +1,148 @@
1
1
  import { render } from '@testing-library/svelte'
2
- import { describe, expect, it } from 'vitest'
3
-
2
+ import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
4
3
  import YouTubeEmbed from '../../../routes/components/YouTubeEmbed.svelte'
5
- import { beforeEach } from 'node:test'
6
4
  import { fakeFetch } from '../../helpers'
7
5
 
6
+ function loadPlyr() {
7
+ const script = document.head.querySelector('script[src*="plyr"]')
8
+ script?.dispatchEvent(new Event('load'))
9
+ }
10
+
8
11
  describe('YouTubeEmbed.svelte', () => {
12
+ /** @type {any} */ let Plyr
13
+ /** @type {any} */ let player
14
+ /** @type {any} */ let onready
15
+ /** @type {any} */ let statechange
16
+
9
17
  beforeEach(() => {
10
18
  fakeFetch()
19
+ onready = undefined
20
+ statechange = undefined
21
+
22
+ player = {
23
+ on: vi.fn((eventName, callback) => {
24
+ if (eventName === 'ready') onready = callback
25
+ if (eventName === 'statechange') statechange = callback
26
+ }),
27
+ play: vi.fn(),
28
+ pause: vi.fn(),
29
+ muted: false,
30
+ volume: 1,
31
+ currentTime: 0,
32
+ }
33
+
34
+ Plyr = vi.fn(() => player)
35
+ window.Plyr = Plyr
11
36
  })
12
37
 
13
- it('Should render embed iframe with given video url and default options', () => {
14
- const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc' })
38
+ afterEach(() => {
39
+ delete window.Plyr
40
+ document.head.innerHTML = ''
41
+ })
15
42
 
16
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toBe('https://video.playpilot.net/?video_id=abc&color=fa548a&muted=false&loop=false&captions=false&controls=&start_time=0&autoplay=true&playsinline=true')
43
+ it('Should render player element and initialize Plyr with default options', () => {
44
+ const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc' })
45
+ loadPlyr()
46
+
47
+ /** @type {HTMLElement | null} */
48
+ const playerElement = container.querySelector('.player')
49
+
50
+ expect(playerElement).toBeTruthy()
51
+ expect(playerElement?.dataset.plyrEmbedId).toBe('abc')
52
+ expect(playerElement?.dataset.plyrProvider).toBe('youtube')
53
+
54
+ expect(Plyr).toHaveBeenCalledWith(
55
+ playerElement,
56
+ expect.objectContaining({
57
+ controls: [],
58
+ autoplay: true,
59
+ muted: false,
60
+ clickToPlay: false,
61
+ playsinline: true,
62
+ youtube: expect.objectContaining({ noCookie: true, cc_load_policy: 0, cc_lang_pref: 'auto' }),
63
+ }),
64
+ )
17
65
  })
18
66
 
19
- it('Should render embed iframe with given controls', () => {
20
- const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', controls: ['fullscreen', 'mute'] })
67
+ it('Should initialize Plyr with given controls', () => {
68
+ const { container } = render(YouTubeEmbed, {
69
+ embeddable_url: 'youtube.com/watch?v=abc',
70
+ controls: ['fullscreen', 'mute'],
71
+ })
21
72
 
22
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toContain('&controls=fullscreen,mute')
73
+ loadPlyr()
74
+
75
+ expect(Plyr).toHaveBeenCalledWith(
76
+ container.querySelector('.player'),
77
+ expect.objectContaining({ controls: ['fullscreen', 'mute'] }),
78
+ )
23
79
  })
24
80
 
25
- it('Should render embed iframe with muted if given', () => {
81
+ it('Should initialize Plyr muted if given', () => {
26
82
  const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', muted: true })
27
83
 
28
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toContain('&muted=true')
84
+ loadPlyr()
85
+
86
+ expect(Plyr).toHaveBeenCalledWith(
87
+ container.querySelector('.player'),
88
+ expect.objectContaining({ muted: true }),
89
+ )
90
+ })
91
+
92
+ it('Should set current time on ready to given fallbackStartTime', () => {
93
+ render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', fallbackStartTime: 8 })
94
+
95
+ loadPlyr()
96
+ onready?.()
97
+
98
+ expect(player.currentTime).toBe(8)
29
99
  })
30
100
 
31
- it('Should render embed iframe with loop if given', () => {
32
- const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', loop: true })
101
+ it('Should use stored playtime for video id, minus 1 second', () => {
102
+ // @ts-ignore
103
+ window.PlayPilotLinkInjections = {
104
+ video_playtimes: { abc: 5 },
105
+ }
106
+
107
+ render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc' })
108
+
109
+ loadPlyr()
110
+ onready?.()
33
111
 
34
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toContain('&loop=true')
112
+ expect(player.currentTime).toBe(4)
35
113
  })
36
114
 
37
- it('Should render embed iframe with captions if given', () => {
115
+ it('Should enable captions in youtube options if given', () => {
38
116
  const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', captions: true })
117
+ loadPlyr()
39
118
 
40
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toContain('&captions=true')
119
+ expect(Plyr).toHaveBeenCalledWith(container.querySelector('.player'), expect.objectContaining({ youtube: expect.objectContaining({ cc_load_policy: 1 }) }))
41
120
  })
42
121
 
43
- it('Should render embed iframe with autoplay set to false if given', () => {
122
+ it('Should initialize Plyr with autoplay set to false if given', () => {
44
123
  const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', autoplay: false })
45
124
 
46
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toContain('&autoplay=false')
125
+ loadPlyr()
126
+
127
+ expect(Plyr).toHaveBeenCalledWith(container.querySelector('.player'), expect.objectContaining({ autoplay: false }))
47
128
  })
48
129
 
49
- it('Should start at given fallbackStartTime', () => {
50
- const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', fallbackStartTime: 8 })
130
+ it('Should replay and re-mute on loop when video ends', () => {
131
+ render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', loop: true, muted: true })
132
+
133
+ loadPlyr()
134
+ global.requestAnimationFrame = (callback) => (callback(0), 0)
51
135
 
52
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toContain('&start_time=8')
136
+ statechange?.({ detail: { code: 0 } })
137
+
138
+ expect(player.play).toHaveBeenCalled()
139
+ expect(player.muted).toBe(true)
53
140
  })
54
141
 
55
142
  it('Should render error message if embeddable_url is invalid', () => {
56
143
  const { container, getByText } = render(YouTubeEmbed, { embeddable_url: '-' })
57
144
 
58
- expect(container.querySelector('iframe')).not.toBeTruthy()
145
+ expect(container.querySelector('.player')).not.toBeTruthy()
59
146
  expect(getByText('Something went wrong')).toBeTruthy()
60
147
  })
61
-
62
- it('Should include start_time for given video id if present, minus 1 second', () => {
63
- // @ts-ignore
64
- window.PlayPilotLinkInjections = {
65
- video_playtimes: { abc: 5 },
66
- }
67
-
68
- const { container } = render(YouTubeEmbed, { embeddable_url: 'youtube.com/watch?v=abc', captions: true })
69
-
70
- expect(/** @type {HTMLIFrameElement} */ (container.querySelector('iframe')).src).toContain('&start_time=4')
71
- })
72
148
  })
@@ -5,9 +5,8 @@ import YouTubeEmbedBackground from '../../../routes/components/YouTubeEmbedBackg
5
5
 
6
6
  describe('YouTubeEmbedBackground.svelte', () => {
7
7
  it('Should render embed iframe with given video url', () => {
8
- const { container } = render(YouTubeEmbedBackground, { embeddable_url: 'youtube.com/watch?v=abc' })
8
+ const { getByTestId } = render(YouTubeEmbedBackground, { embeddable_url: 'youtube.com/watch?v=abc' })
9
9
 
10
- // @ts-ignore
11
- expect(container.querySelector('iframe').src).toBe('https://video.playpilot.net/?video_id=abc&color=fa548a&muted=true&loop=true&captions=false&controls=&start_time=5&autoplay=true&playsinline=true')
10
+ expect(getByTestId('youtube.com/watch?v=abc')).toBeTruthy()
12
11
  })
13
12
  })
@@ -5,10 +5,9 @@ import YouTubeEmbedOverlay from '../../../routes/components/YouTubeEmbedOverlay.
5
5
 
6
6
  describe('YouTubeEmbedOverlay.svelte', () => {
7
7
  it('Should render embed iframe with given video url', () => {
8
- const { container } = render(YouTubeEmbedOverlay, { embeddable_url: 'youtube.com/watch?v=abc', onclose: () => null })
8
+ const { getByTestId } = render(YouTubeEmbedOverlay, { embeddable_url: 'youtube.com/watch?v=abc', onclose: () => null })
9
9
 
10
- // @ts-ignore
11
- expect(container.querySelector('iframe').src).toBe('https://video.playpilot.net/?video_id=abc&color=fa548a&muted=false&loop=false&captions=false&controls=current-time,fullscreen,mute,play,progress&start_time=0&autoplay=true&playsinline=true')
10
+ expect(getByTestId('youtube.com/watch?v=abc')).toBeTruthy()
12
11
  })
13
12
 
14
13
  it('Should fire given onclose function on click of close button and backdrop', async () => {