-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathcloudinary-sdk-analytics.spec.ts
263 lines (220 loc) · 10.7 KB
/
cloudinary-sdk-analytics.spec.ts
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { Component, DebugElement } from '@angular/core';
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Cloudinary } from './cloudinary.service';
import CloudinaryConfiguration from './cloudinary-configuration.class';
import { CloudinaryImage } from './cloudinary-image.component';
import { CloudinaryTransformationDirective } from './cloudinary-transformation.directive';
import {LazyLoadDirective } from './cloudinary-lazy-load.directive';
import { CloudinaryPlaceHolder } from'./cloudinary-placeholder.component';
import { SDKAnalyticsConstants } from './SDKAnalyticsConstants';
import { APP_VERSION } from './version';
describe('Tests for sdk versionID on image tag', () => {
describe('Config with urlAnalytics not set', () => {
let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular_sdk@@' } as CloudinaryConfiguration);
beforeEach(() => {
spyOn(localCloudinary, 'toCloudinaryAttributes').and.callThrough();
spyOn(localCloudinary, 'url').and.callThrough();
spyOn(localCloudinary, 'responsive').and.callThrough();
});
@Component({
template: `<cl-image responsive public-id="sample"></cl-image>`
})
class TestComponent { }
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryImage, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
expect(localCloudinary.responsive).toHaveBeenCalled();
// all elements with an attached CloudinaryImage
des = fixture.debugElement.query(By.directive(CloudinaryImage));
});
it('creates an img without encoding', () => {
const img = des.children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular_sdk@@/image/upload/sample');
});
});
describe('Config with urlAnalytics set to true', () => {
let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular_sdk@@', urlAnalytics: true} as CloudinaryConfiguration);
beforeEach(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
spyOn(localCloudinary, 'toCloudinaryAttributes').and.callThrough();
spyOn(localCloudinary, 'url').and.callThrough();
});
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
@Component({
template: `<cl-image public-id="sample"></cl-image>`
})
class TestComponent { }
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryImage, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// all elements with an attached CloudinaryImage
des = fixture.debugElement.query(By.directive(CloudinaryImage));
});
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
it('creates an img without a feature- resulting in 0', () => {
const img = des.children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular_sdk@@/image/upload/sample?_a=AKHZdAH0');
});
});
describe('Accessibility with urlAnalytics set to true', () => {
let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular_sdk@@', urlAnalytics: true} as CloudinaryConfiguration);
beforeEach(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
spyOn(localCloudinary, 'toCloudinaryAttributes').and.callThrough();
spyOn(localCloudinary, 'url').and.callThrough();
spyOn(SDKAnalyticsConstants, 'sdkSemver').and.returnValue('1.3.3');
});
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
@Component({
template: `<cl-image accessibility="darkmode" public-id="sample"></cl-image>`
})
class TestComponent { }
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryImage, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// all elements with an attached CloudinaryImage
des = fixture.debugElement.query(By.directive(CloudinaryImage));
});
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
it('creates an img with feature accessibility D', () => {
const img = des.children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular_sdk@@/image/upload/e_tint:75:black/sample?_a=AKHZdAHD');
});
});
describe('Responsive with urlAnalytics set to true', () => {
let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular_sdk@@', urlAnalytics: true } as CloudinaryConfiguration);
beforeEach(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
spyOn(localCloudinary, 'toCloudinaryAttributes').and.callThrough();
spyOn(localCloudinary, 'url').and.callThrough();
spyOn(localCloudinary, 'responsive').and.callThrough();
});
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
@Component({
template: `<cl-image responsive public-id="sample"></cl-image>`
})
class TestComponent { }
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement; // the elements w/ the directive
beforeEach(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryImage, TestComponent],
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
expect(localCloudinary.responsive).toHaveBeenCalled();
// all elements with an attached CloudinaryImage
des = fixture.debugElement.query(By.directive(CloudinaryImage));
});
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
it('creates an img with feature responsive A', () => {
const img = des.children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular_sdk@@/image/upload/sample?_a=AKHZdAHA');
});
});
describe('Placeholder with urlAnalytics set to true', async () => {
@Component({
template: `<cl-image public-id="sample">
<cl-placeholder></cl-placeholder>
</cl-image>`
})
class TestComponent {}
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement[]; // the elements w/ the directive
let placeholder: DebugElement[];
let testLocalCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular_sdk@@', urlAnalytics: true } as CloudinaryConfiguration);
beforeEach(fakeAsync(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent, LazyLoadDirective, CloudinaryPlaceHolder],
providers: [{ provide: Cloudinary, useValue: testLocalCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// all elements with an attached CloudinaryImage
des = fixture.debugElement.queryAll(By.directive(CloudinaryImage));
placeholder = fixture.debugElement.queryAll(By.directive(CloudinaryPlaceHolder));
tick();
fixture.detectChanges();
}));
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
it('placeholder img should encode with B', async () => {
const placeholderimg = placeholder[0].children[0].nativeElement as HTMLImageElement;
expect(placeholderimg.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular_sdk@@/image/upload/e_blur:2000,f_auto,q_1/sample?_a=AKHZdAHB');
});
it('original img should encode with 0', async () => {
const img = des[0].children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular_sdk@@/image/upload/sample?_a=AKHZdAH0');
});
});
describe('Lazy-load with urlAnalytics set to true', async () => {
@Component({
template: `<cl-image public-id="sample" loading="lazy"></cl-image>`
})
class TestComponent {}
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement[]; // the elements w/ the directive
let placeholder: DebugElement[];
let testLocalCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular_sdk@@', urlAnalytics: true } as CloudinaryConfiguration);
beforeEach(fakeAsync(() => {
SDKAnalyticsConstants.sdkSemver = '1.3.3';
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent, LazyLoadDirective, CloudinaryPlaceHolder],
providers: [{ provide: Cloudinary, useValue: testLocalCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
// all elements with an attached CloudinaryImage
des = fixture.debugElement.queryAll(By.directive(CloudinaryImage));
placeholder = fixture.debugElement.queryAll(By.directive(CloudinaryPlaceHolder));
tick();
fixture.detectChanges();
}));
afterEach(() => {
SDKAnalyticsConstants.sdkSemver = APP_VERSION;
});
it('creates an img with feature lazy load C', async () => {
const wait = (ms) => new Promise(res => setTimeout(res, ms));
await wait(300);
const img = des[0].children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual('http://res.cloudinary.com/@@fake_angular_sdk@@/image/upload/sample?_a=AKHZdAHC');
});
});
});