@playpilot/tpi 6.3.0-beta.5 → 6.3.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.
@@ -3,6 +3,10 @@ import { describe, expect, it, vi } from 'vitest'
3
3
 
4
4
  import PlaylinkIcon from '../../../../routes/components/Playlinks/PlaylinkIcon.svelte'
5
5
 
6
+ vi.mock('$lib/consent', () => ({
7
+ hasConsentedTo: vi.fn(() => true),
8
+ }))
9
+
6
10
  describe('PlaylinkIcon.svelte', () => {
7
11
  const playlink = { name: 'Some playlink', logo_url: 'logo', extra_info: { category: 'SVOD' } }
8
12
 
@@ -24,4 +28,12 @@ describe('PlaylinkIcon.svelte', () => {
24
28
  expect(getByAltText(playlink.name).getAttribute('width')).toBe('20')
25
29
  expect(getByAltText(playlink.name).getAttribute('height')).toBe('20')
26
30
  })
31
+
32
+ it('Should render given tracking pixels', () => {
33
+ const playlink = { name: 'Some playlink', logo_url: 'logo', pixels: ['https://some-pixel.com/a.jpg'], extra_info: { category: 'SVOD' } }
34
+ // @ts-ignore
35
+ const { container } = render(PlaylinkIcon, { playlink })
36
+
37
+ expect(container.querySelector('img[src*="https://some-pixel.com/a.jpg"]')).toBeTruthy()
38
+ })
27
39
  })
@@ -1,4 +1,4 @@
1
- import { render } from '@testing-library/svelte'
1
+ import { render, waitFor } from '@testing-library/svelte'
2
2
  import { describe, expect, it, vi } from 'vitest'
3
3
 
4
4
  import TrackingPixels from '../../../routes/components/TrackingPixels.svelte'
@@ -26,4 +26,22 @@ describe('TrackingPixels.svelte', () => {
26
26
 
27
27
  expect(queryByRole('presentation')).not.toBeTruthy()
28
28
  })
29
+
30
+ it('Should re-render when updateconsent is fired', async () => {
31
+ vi.mocked(hasConsentedTo).mockImplementation(() => false)
32
+
33
+ const pixels = ['https://image.com/a.jpg']
34
+ const { queryByRole } = render(TrackingPixels, { pixels })
35
+
36
+ expect(queryByRole('presentation')).not.toBeTruthy()
37
+
38
+ vi.mocked(hasConsentedTo).mockImplementation(() => true)
39
+ window.dispatchEvent(new CustomEvent('updateconsent', {
40
+ bubbles: true,
41
+ }))
42
+
43
+ await waitFor(() => {
44
+ expect(queryByRole('presentation')).toBeTruthy()
45
+ })
46
+ })
29
47
  })
@@ -1,89 +0,0 @@
1
- <script lang="ts">
2
- import { onMount } from 'svelte'
3
-
4
- const linksWithPosters = Array.from(document.querySelectorAll<HTMLElement>('[data-playpilot-poster-url]'))
5
- const linksWithCurrentlyVisiblePosters = new Set<HTMLElement>()
6
- const targetThreshold = 0.6
7
- const posterSelector = '[data-playpilot-poster]'
8
-
9
- let scrollTimeout: ReturnType<typeof setTimeout> | null = null
10
-
11
- onMount(() => {
12
- return destroyAllPosters
13
- })
14
-
15
- function onscroll(): void {
16
- setScrollTimeout()
17
-
18
- const windowHeight = window.innerHeight
19
- const linksWithinTargetViewport = new Set<HTMLElement>()
20
- const targetFromBottom = windowHeight * targetThreshold
21
- const targetFromTop = windowHeight - targetFromBottom
22
-
23
- for (const link of linksWithPosters) {
24
- const { top } = link.getBoundingClientRect()
25
-
26
- const isInsideTargetViewport = top < targetFromBottom && top > targetFromTop
27
-
28
- if (!isInsideTargetViewport) continue
29
-
30
- linksWithinTargetViewport.add(link)
31
- }
32
-
33
- for (const link of linksWithCurrentlyVisiblePosters) {
34
- if (linksWithinTargetViewport.has(link)) continue
35
-
36
- destroyPosterForLink(link)
37
- linksWithCurrentlyVisiblePosters.delete(link)
38
- }
39
-
40
- for (const link of linksWithinTargetViewport) {
41
- if (linksWithCurrentlyVisiblePosters.has(link)) continue
42
-
43
- createPosterForLink(link)
44
- linksWithCurrentlyVisiblePosters.add(link)
45
- }
46
- }
47
-
48
- function createPosterForLink(link: HTMLElement): void {
49
- const posterUrl = link.dataset.playpilotPosterUrl!
50
-
51
- const element = document.createElement('img')
52
- element.classList.add('playpilot-floating-poster')
53
- element.dataset.playpilotPoster = ''
54
- element.src = posterUrl
55
- element.alt = ''
56
- element.onload = () => element.classList.add('loaded')
57
-
58
- link.insertAdjacentElement('afterbegin', element)
59
- }
60
-
61
- function destroyPosterForLink(link: HTMLElement): void {
62
- const poster = link.querySelector(posterSelector)
63
-
64
- if (!poster) return
65
-
66
- poster.classList.add('animating-out')
67
-
68
- setTimeout(() => poster.remove(), 500)
69
- }
70
-
71
- function destroyAllPosters(): void {
72
- for (const poster of Array.from(document.querySelectorAll(posterSelector))) {
73
- poster.remove()
74
- }
75
- }
76
-
77
- function setScrollTimeout() {
78
- if (scrollTimeout) clearTimeout(scrollTimeout)
79
-
80
- scrollTimeout = setTimeout(() => {
81
- for (const link of linksWithCurrentlyVisiblePosters) {
82
- destroyPosterForLink(link)
83
- linksWithCurrentlyVisiblePosters.delete(link)
84
- }
85
- }, 800)
86
- }
87
- </script>
88
-
89
- <svelte:window {onscroll} />
@@ -1,107 +0,0 @@
1
- import { render } from '@testing-library/svelte'
2
- import { describe, it, expect, vi, beforeEach } from 'vitest'
3
- import PostersScrollReveal from '../../../routes/components/PostersScrollReveal.svelte'
4
-
5
- function createPosterLink(top = 0, url = 'poster.jpg') {
6
- const link = document.createElement('a')
7
- link.dataset.playpilotPosterUrl = url
8
-
9
- // @ts-ignore
10
- link.getBoundingClientRect = () => ({ top })
11
-
12
- document.body.appendChild(link)
13
- return link
14
- }
15
-
16
- describe('FloatingPoster', () => {
17
- beforeEach(() => {
18
- document.body.innerHTML = ''
19
-
20
- Object.defineProperty(window, 'innerHeight', {
21
- value: 1000,
22
- writable: true,
23
- })
24
-
25
- vi.useFakeTimers()
26
- vi.resetAllMocks()
27
- })
28
-
29
- it('Should create poster when link is in viewport', async () => {
30
- createPosterLink(500)
31
-
32
- render(PostersScrollReveal)
33
-
34
- window.dispatchEvent(new Event('scroll'))
35
-
36
- const poster = document.querySelector('[data-playpilot-poster]')
37
- expect(poster).toBeTruthy()
38
- expect(poster?.getAttribute('src')).toBe('poster.jpg')
39
- })
40
-
41
- it('Should not create poster when link is below in viewport', async () => {
42
- createPosterLink(2000)
43
-
44
- render(PostersScrollReveal)
45
-
46
- window.dispatchEvent(new Event('scroll'))
47
-
48
- expect(document.querySelector('[data-playpilot-poster]')).not.toBeTruthy()
49
- })
50
-
51
- it('Should not create poster when link is above in viewport', async () => {
52
- createPosterLink(-100)
53
-
54
- render(PostersScrollReveal)
55
-
56
- window.dispatchEvent(new Event('scroll'))
57
-
58
- expect(document.querySelector('[data-playpilot-poster]')).not.toBeTruthy()
59
- })
60
-
61
- it('Should not create poster when link is in viewport but before threshold', async () => {
62
- createPosterLink(50)
63
- createPosterLink(900)
64
-
65
- render(PostersScrollReveal)
66
-
67
- window.dispatchEvent(new Event('scroll'))
68
-
69
- expect(document.querySelector('[data-playpilot-poster]')).not.toBeTruthy()
70
- })
71
-
72
- it('Should create poster for each link in viewport', async () => {
73
- createPosterLink(10)
74
- createPosterLink(500)
75
- createPosterLink(500)
76
- createPosterLink(510)
77
- createPosterLink(520)
78
- createPosterLink(2000)
79
-
80
- render(PostersScrollReveal)
81
-
82
- window.dispatchEvent(new Event('scroll'))
83
-
84
- expect(document.querySelectorAll('[data-playpilot-poster]')).toHaveLength(4)
85
- })
86
-
87
- it('Should hide poster after not scrolling for given timeout', async () => {
88
- createPosterLink(500)
89
-
90
- render(PostersScrollReveal)
91
-
92
- window.dispatchEvent(new Event('scroll'))
93
- expect(document.querySelector('[data-playpilot-poster]')).toBeTruthy()
94
-
95
- vi.advanceTimersByTime(200)
96
- expect(document.querySelector('[data-playpilot-poster]')).toBeTruthy()
97
-
98
- vi.advanceTimersByTime(400)
99
- expect(document.querySelector('[data-playpilot-poster]')).toBeTruthy()
100
-
101
- vi.advanceTimersByTime(1000)
102
- expect(document.querySelector('[data-playpilot-poster]')).not.toBeTruthy()
103
-
104
- window.dispatchEvent(new Event('scroll'))
105
- expect(document.querySelector('[data-playpilot-poster]')).toBeTruthy()
106
- })
107
- })