forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia.test.ts
More file actions
166 lines (142 loc) · 5.87 KB
/
Copy pathmedia.test.ts
File metadata and controls
166 lines (142 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { findMediaElement } from '../src/findElem';
import { dataUrlHandler, getDuration, getUriFileExtension } from '../src/helperFunctions';
describe('element searcher', () => {
it('finds a video with id', () => {
document.body.innerHTML = '<div><video id="videoElem" src="test.mp4"</video></div>';
const output = findMediaElement('videoElem');
expect(output.el);
expect(output.el?.tagName).toBe('VIDEO');
expect(output.el?.id).toBe('videoElem');
});
it("returns error if the element doesn't have the id", () => {
document.body.innerHTML = '<div><video src="test.mp4"</video></div>';
const output = findMediaElement('videoElem');
expect(output).toStrictEqual({ err: 'Media element not found' });
});
it('finds a child video element in parent with id', () => {
document.body.innerHTML = '<div id="parentElem"><video></video></div>';
debugger;
const output = findMediaElement('parentElem');
expect(output.el?.tagName).toBe('VIDEO');
});
it('returns an error if multiple child audio elements exist in a parent', () => {
document.body.innerHTML = '<div id="parentElem"><audio></audio><audio></audio></div>';
const output = findMediaElement('parentElem');
expect(output).toStrictEqual({ err: 'More than one media element in the provided node' });
});
it('returns the first video element if exactly two exist in the same parent. Covers the cover-video case.', () => {
document.body.innerHTML = '<div id="parentElem"><video id="test-id"></video><video></video></div>';
const output = findMediaElement('parentElem');
expect(output.el?.tagName).toBe('VIDEO');
expect(output.el?.id).toBe('test-id');
});
it('returns an error if multiple child video elements exist in a parent', () => {
document.body.innerHTML = '<div id="parentElem"><video></video><video></video><video></video></div>';
const output = findMediaElement('parentElem');
expect(output).toStrictEqual({ err: 'More than one media element in the provided node' });
});
});
describe('dataUrlHandler', () => {
it('returns a non-data uri', () => {
const test_url = 'http://example.com/example.mp4';
const output = dataUrlHandler(test_url);
expect(output).toBe(test_url);
});
it('returns "DATA_URL" in event of data uri', () => {
const test_url = 'data:image/png;base64,iVBORw0KGgoAA5ErkJggg==';
const output = dataUrlHandler(test_url);
expect(output).toBe('DATA_URL');
});
});
describe('getUriFileExtension', () => {
it('parses simple URLs', () => {
const test_url = 'http://example.com/example.mp4';
const output = getUriFileExtension(test_url);
expect(output).toBe('mp4');
});
it('ignores uri cruft', () => {
let test_url = 'http://example.com/example.wmv?abc=123';
let output = getUriFileExtension(test_url);
expect(output).toBe('wmv');
test_url = 'http://example.com/example.avi#fragment';
output = getUriFileExtension(test_url);
expect(output).toBe('avi');
});
it('prefers the uri pathname', () => {
// there is ambiguity here, taking only from the path if possible
const test_url = 'http://example.com/example.mp4?token=123.abc';
const output = getUriFileExtension(test_url);
expect(output).toBe('mp4');
});
it('falls back to rest of uri', () => {
const test_url = 'http://example.com/media?file=test.mov';
const output = getUriFileExtension(test_url);
expect(output).toBe('mov');
});
it('ignores data uris', () => {
/*
schema description is "The media file format", so could make an argument
for pulling the MIME type here, but the name fileExtension implies this
should be derived from the name, which we do not have for data URIs
*/
const test_url = 'data:image/png;base64,iVBORw0KGgoAA5ErkJggg==';
const output = getUriFileExtension(test_url);
expect(output).toBe(null);
});
});
describe('getDuration of a ', () => {
describe('video element', () => {
it('returns the duration if valid', () => {
const video = { duration: 10 } as HTMLVideoElement;
const output = getDuration(video);
expect(output).toBe(10);
});
it('returns null if the duration is Infinity', () => {
const video = { duration: Infinity } as HTMLVideoElement;
const output = getDuration(video);
expect(output).toBe(null);
});
it('returns null if the duration is +Infinity', () => {
const video = { duration: +Infinity } as HTMLVideoElement;
const output = getDuration(video);
expect(output).toBe(null);
});
it('returns null if the duration is NaN', () => {
const video = { duration: NaN } as HTMLVideoElement;
const output = getDuration(video);
expect(output).toBe(null);
});
it('returns null if the duration is not available', () => {
const video = {} as HTMLVideoElement;
const output = getDuration(video);
expect(output).toBe(null);
});
});
describe('audio element', () => {
it('returns the duration if valid', () => {
const audio = { duration: 10 } as HTMLAudioElement;
const output = getDuration(audio);
expect(output).toBe(10);
});
it('returns null if the duration is Infinity', () => {
const audio = { duration: Infinity } as HTMLAudioElement;
const output = getDuration(audio);
expect(output).toBe(null);
});
it('returns null if the duration is +Infinity', () => {
const audio = { duration: +Infinity } as HTMLAudioElement;
const output = getDuration(audio);
expect(output).toBe(null);
});
it('returns null if the duration is NaN', () => {
const audio = { duration: NaN } as HTMLAudioElement;
const output = getDuration(audio);
expect(output).toBe(null);
});
it('returns null if the duration is not available', () => {
const audio = {} as HTMLAudioElement;
const output = getDuration(audio);
expect(output).toBe(null);
});
});
});