-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathcloudinary-video.component.spec.ts
More file actions
229 lines (185 loc) Β· 9.47 KB
/
cloudinary-video.component.spec.ts
File metadata and controls
229 lines (185 loc) Β· 9.47 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Cloudinary } from './cloudinary.service';
import CloudinaryConfiguration from './cloudinary-configuration.class';
import { CloudinaryVideo } from './cloudinary-video.component';
import { CloudinaryTransformationDirective } from './cloudinary-transformation.directive';
describe('CloudinaryVideo', () => {
let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular2_sdk@@' } as CloudinaryConfiguration);
beforeEach(() => {
spyOn(localCloudinary, 'toCloudinaryAttributes').and.callThrough();
spyOn(localCloudinary, 'videoTag').and.callThrough();
});
describe('testing phantomjs', () => {
it('it uses createElement to create <source> and append to <video>', () => {
function addSourceToVideo(element, src, type) {
const source = document.createElement('source');
source.src = src;
source.type = type;
element.appendChild(source);
}
const video = document.createElement('video');
document.body.appendChild(video);
addSourceToVideo(video, 'http://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv', 'video/ogg');
expect(video.childElementCount).toBe(1);
expect((video.children[0] as HTMLSourceElement).src).toEqual('http://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv');
});
it('it creates <source> elements using innerHTML', () => {
const video = document.createElement('video');
document.body.appendChild(video);
const source = `<source src="http://res.cloudinary.com/@@fake_angular2_sdk@@/video/upload/c_scale,l_text:roboto_35_bold:SDK,w_300/e_art:hokusai/f_auto/sample_video.mp4" type="video/mp4">
<source src="http://res.cloudinary.com/@@fake_angular2_sdk@@/video/upload/c_scale,l_text:roboto_35_bold:SDK,w_300/e_art:hokusai/f_auto/sample_video.mp4" type="video/mp4">`;
video.innerHTML = source;
expect(video.childElementCount).toBe(2);
expect(video.children[0].attributes.getNamedItem('src')).toBeDefined();
expect(video.children[0].attributes.getNamedItem('src').value)
.toEqual('http://res.cloudinary.com/@@fake_angular2_sdk@@/video/upload/c_scale,l_text:roboto_35_bold:SDK,w_300/e_art:hokusai/f_auto/sample_video.mp4');
});
});
describe('missing public-id', () => {
@Component({
template: '<cl-video id="video1"></cl-video>'
})
class TestComponent {
}
it('throws if the directive is missing a public-id attribute', () => {
const fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryVideo, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
expect(() => {
fixture.detectChanges();
}).toThrowError(/You must set the public id of the video to load/i);
});
});
describe('videos with nested transformations', () => {
@Component({
template: `<cl-video id="video1" public-id="sample_video">
<cl-transformation width="300" crop="scale" overlay="text:roboto_35_bold:SDK"></cl-transformation>
<cl-transformation effect="art:hokusai"></cl-transformation>
<cl-transformation fetch_format="auto"></cl-transformation>
</cl-video>`
})
class TestComponent {
}
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryVideo, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// all elements with an attached CloudinaryVideo
des = fixture.debugElement.query(By.directive(CloudinaryVideo));
});
it('creates a <video> element which encodes the directive attributes to the URL', () => {
const video = des.children[0].nativeElement as HTMLVideoElement;
// Created <video> element should have 3 child <source> elements for mp4, webm, ogg
expect(video.childElementCount).toBe(3);
for (let i = 0; i < 3; i++) {
expect(video.children[i].attributes.getNamedItem('src')).toBeDefined();
expect(video.children[i].attributes.getNamedItem('src').value).toEqual(
jasmine.stringMatching
(/c_scale,l_text:roboto_35_bold:SDK,w_300\/e_art:hokusai\/f_auto\/sample_video/));
expect(video.children[i].attributes.getNamedItem('src').value).toEqual(
jasmine.stringMatching(/video\/upload/));
}
// verify interaction with underlying cloudinary-core lib
expect(localCloudinary.videoTag).toHaveBeenCalledTimes(1);
});
});
describe('Video with poster using kebab-case', () => {
@Component({
template: `
<cl-video cloud-name="my_other_cloud" public-id="watchme" secure="true" class="my-videos"
poster='{"cloud-name": "cloudinary", "gravity": "north", "start-offset": "28",
"transformation": [{"effect": "sepia", "fetch-format": "auto"}]}'>
</cl-video>
`
})
class TestComponent {
}
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryVideo, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// Our element under test, which is attached to CloudinaryVideo
des = fixture.debugElement.query(By.directive(CloudinaryVideo));
});
it('creates a <video> element which encodes the directive attributes to the URL', () => {
const video = des.children[0].nativeElement as HTMLVideoElement;
// Created <video> element should have 3 child <source> elements for mp4, webm, ogg
expect(video.attributes.getNamedItem('poster').value).toEqual(
jasmine.stringMatching(/cloudinary\/video\/upload\/e_sepia,f_auto\/g_north,so_28\/watchme.jpg/));
});
});
describe('Video with poster using snake_case', () => {
@Component({
template: `
<cl-video cloud_name="my_other_cloud" public-id="watchme" secure="true" class="my-videos"
poster='{ "cloud_name": "cloudinary", "gravity": "north", "start_offset": "28",
"transformation": [{"effect": "sepia", "fetch_format": "auto"}]}'>
</cl-video>
`
})
class TestComponent {
}
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryVideo, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// Our element under test, which is attached to CloudinaryVideo
des = fixture.debugElement.query(By.directive(CloudinaryVideo));
});
it('creates a <video> element which encodes the directive attributes to the URL', () => {
const video = des.children[0].nativeElement as HTMLVideoElement;
// Created <video> element should have 3 child <source> elements for mp4, webm, ogg
expect(video.attributes.getNamedItem('poster').value).toEqual(
jasmine.stringMatching(/cloudinary\/video\/upload\/e_sepia,f_auto\/g_north,so_28\/watchme.jpg/));
});
});
describe('Sample code presented in README', () => {
@Component({
template: `
<cl-video cloud-name="my_other_cloud" public-id="watchme" secure="true" class="my-videos">
<cl-transformation overlay="text:arial_60:watchme" gravity="north" y="20"></cl-transformation>
</cl-video>
`
})
class TestComponent {
}
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryVideo, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// Our element under test, which is attached to CloudinaryVideo
des = fixture.debugElement.query(By.directive(CloudinaryVideo));
});
it('creates a <video> element which encodes the directive attributes to the URL', () => {
const video = des.children[0].nativeElement as HTMLVideoElement;
// Created <video> element should have 3 child <source> elements for mp4, webm, ogg
expect(video.childElementCount).toBe(3);
for (let i = 0; i < 3; i++) {
expect(video.children[i].attributes.getNamedItem('src')).toBeDefined();
expect(video.children[i].attributes.getNamedItem('src').value).toEqual(
jasmine.stringMatching
(/https:\/\/res.cloudinary.com\/my_other_cloud\/video\/upload\/g_north,l_text:arial_60:watchme,y_20\/watchme/));
}
});
});
});