-
-
Notifications
You must be signed in to change notification settings - Fork 943
/
Copy pathimage.spec.ts
499 lines (420 loc) · 14.9 KB
/
image.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import { describe, expect, it } from 'vitest';
import { faker } from '../../src';
import { seededTests } from './../support/seededRuns';
describe('image', () => {
seededTests(faker, 'image', (t) => {
t.itEach('avatar', 'avatarGitHub', 'avatarLegacy');
t.describe('url', (t) => {
t.it('noArgs')
.it('with width', { width: 128 })
.it('with height', { height: 128 })
.it('with width and height', { width: 128, height: 128 });
});
t.describe('urlLoremFlickr', (t) => {
t.it('noArgs')
.it('with width', { width: 128 })
.it('with height', { height: 128 })
.it('with width and height', { width: 128, height: 128 })
.it('with category', { category: 'cats' })
.it('with all options', {
width: 128,
height: 128,
category: 'cats',
});
});
t.describe('urlPicsumPhotos', (t) => {
t.it('noArgs')
.it('with width', { width: 128 })
.it('with height', { height: 128 })
.it('with width and height', { width: 128, height: 128 })
.it('with blur', { blur: 6 })
.it('with blur and grayscale', { blur: 3, grayscale: true })
.it('with all options', {
width: 128,
height: 128,
blur: 4,
grayscale: true,
});
});
t.describe('urlPlaceholder', (t) => {
t.it('noArgs')
.it('with width', { width: 128 })
.it('with height', { height: 128 })
.it('with width and height', { width: 128, height: 128 })
.it('with backgroundColor', { backgroundColor: 'FF0000' })
.it('with textColor', { textColor: '0000FF' })
.it('with format', { format: 'webp' })
.it('with text', { text: 'Hello' })
.it('with all options', {
width: 128,
height: 128,
backgroundColor: 'FF0000',
textColor: '0000FF',
format: 'png',
text: 'hello',
})
.it('with empty colors and text', {
width: 128,
height: 128,
backgroundColor: '',
textColor: '',
format: 'png',
text: '',
});
});
t.describe('dataUri', (t) => {
t.it('noArgs')
.it('with width', { width: 128 })
.it('with height', { height: 128 })
.it('with width and height', { width: 128, height: 128 })
.it('with color', { color: 'blue' })
.it('with all options', {
width: 128,
height: 128,
color: 'blue',
});
});
t.skip('abstract');
t.skip('animals');
t.skip('business');
t.skip('cats');
t.skip('city');
t.skip('fashion');
t.skip('food');
t.skip('image');
t.skip('imageUrl');
t.skip('nature');
t.skip('nightlife');
t.skip('people');
t.skip('sports');
t.skip('technics');
t.skip('transport');
});
describe('lorempicsum', () => {
describe('imageUrl()', () => {
it('should return a random image url from lorem picsum', () => {
const imageUrl = faker.image.lorempicsum.imageUrl();
expect(imageUrl).toBe('https://picsum.photos/640/480');
});
it('should return a random image url from lorem picsum with width and height', () => {
const imageUrl = faker.image.lorempicsum.imageUrl(100, 100);
expect(imageUrl).toBe('https://picsum.photos/100/100');
});
it('should return a random image url grayscaled', () => {
const imageUrl = faker.image.lorempicsum.imageUrl(100, 100, true);
expect(imageUrl).toBe('https://picsum.photos/100/100?grayscale');
});
it('should return a random image url grayscaled and blurred', () => {
const imageUrl = faker.image.lorempicsum.imageUrl(100, 100, true, 2);
expect(imageUrl).toBe('https://picsum.photos/100/100?grayscale&blur=2');
});
it('should return a random image url blurred', () => {
const imageUrl = faker.image.lorempicsum.imageUrl(
100,
100,
undefined,
2
);
expect(imageUrl).toBe('https://picsum.photos/100/100?blur=2');
});
it('should return a random image url with seed', () => {
const imageUrl = faker.image.lorempicsum.imageUrl(
100,
100,
undefined,
undefined,
'picsum'
);
expect(imageUrl).toBe('https://picsum.photos/seed/picsum/100/100');
});
});
describe('imageGrayscale()', () => {
it('should return a random URL with grayscale image', () => {
const imageUrl = faker.image.lorempicsum.imageGrayscale(100, 100, true);
expect(imageUrl).toBe('https://picsum.photos/100/100?grayscale');
});
});
describe('imageBlurred()', () => {
it('should return a random image url blurred', () => {
const imageUrl = faker.image.lorempicsum.imageBlurred(100, 100, 2);
expect(imageUrl).toBe('https://picsum.photos/100/100?blur=2');
});
});
describe('imageRandomSeeded()', () => {
it('should return a random image url blurred', () => {
const imageUrl = faker.image.lorempicsum.imageRandomSeeded(
100,
100,
undefined,
undefined,
'picsum'
);
expect(imageUrl).toBe('https://picsum.photos/seed/picsum/100/100');
});
});
});
describe('unsplash', () => {
describe('imageUrl()', () => {
it('should return a random image url from unsplash', () => {
const imageUrl = faker.image.unsplash.imageUrl();
expect(imageUrl).toBe('https://source.unsplash.com/640x480');
});
it('should return a random image url from unsplash with width and height', () => {
const imageUrl = faker.image.unsplash.imageUrl(100, 100);
expect(imageUrl).toBe('https://source.unsplash.com/100x100');
});
it('should return a random image url for a specified category', () => {
const imageUrl = faker.image.unsplash.imageUrl(100, 100, 'food');
expect(imageUrl).toBe(
'https://source.unsplash.com/category/food/100x100'
);
});
it('should return a random image url with correct keywords for a specified category', () => {
const imageUrl = faker.image.unsplash.imageUrl(
100,
100,
'food',
'keyword1,keyword2'
);
expect(imageUrl).toBe(
'https://source.unsplash.com/category/food/100x100?keyword1,keyword2'
);
});
it('should return a random image url without keyword which format is wrong for a specified category', () => {
const imageUrl = faker.image.unsplash.imageUrl(
100,
100,
'food',
'keyword1,?ds)0123$*908932409'
);
expect(imageUrl).toBe(
'https://source.unsplash.com/category/food/100x100'
);
});
});
describe('image()', () => {
it('should return a searching image url with keyword', () => {
const imageUrl = faker.image.unsplash.image(
100,
200,
'keyword1,keyword2,keyword3'
);
expect(imageUrl).toBe(
'https://source.unsplash.com/100x200?keyword1,keyword2,keyword3'
);
});
});
const categories = [
'buildings',
'food',
'nature',
'objects',
'people',
'technology',
];
describe.each(categories)(`%s()`, (category) => {
it(`should return a random ${category} image url`, () => {
const actual = faker.image.unsplash[category]();
expect(actual).toBe(
`https://source.unsplash.com/category/${category}/640x480`
);
});
});
});
describe('placeholder', () => {
describe('imageUrl()', () => {
it('should return a random image url from placeholder', () => {
const imageUrl = faker.image.placeholder.imageUrl();
expect(imageUrl).toBe('https://via.placeholder.com/640x640');
});
it('should return a square random image url from placeholder with width and height', () => {
const imageUrl = faker.image.placeholder.imageUrl(100);
expect(imageUrl).toBe('https://via.placeholder.com/100x100');
});
it('should return a random image url with a gif format', () => {
const imageUrl = faker.image.placeholder.imageUrl(
100,
100,
undefined,
'gif'
);
expect(imageUrl).toBe('https://via.placeholder.com/100x100.gif');
});
it('should return a random image url with correct text for a specified format', () => {
const imageUrl = faker.image.placeholder.imageUrl(
100,
100,
'I love food',
'png'
);
expect(imageUrl).toBe(
'https://via.placeholder.com/100x100.png?text=I+love+food'
);
});
it('should return a random image url with specified background color and text color', () => {
const imageUrl = faker.image.placeholder.imageUrl(
100,
100,
undefined,
undefined,
'000000',
'ffffff'
);
expect(imageUrl).toBe(
'https://via.placeholder.com/100x100/000000/FFFFFF'
);
});
it('should return a random image url with specified background color and color without the #', () => {
const imageUrl = faker.image.placeholder.imageUrl(
100,
100,
undefined,
undefined,
'#000000',
'#ffffff'
);
expect(imageUrl).toBe(
'https://via.placeholder.com/100x100/000000/FFFFFF'
);
});
it('should return a random image url given all parameter', () => {
const imageUrl = faker.image.placeholder.imageUrl(
100,
200,
'I love food',
'jpg',
'000000',
'ffffff'
);
expect(imageUrl).toBe(
'https://via.placeholder.com/100x200/000000/FFFFFF.jpg?text=I+love+food'
);
});
});
describe('randomUrl()', () => {
it('should return a random url with specified width and height', () => {
const imageUrl = faker.image.placeholder.randomUrl(200, 150);
// https://via.placeholder.com/150/000000/FFFFFF/
const urlSpilt = imageUrl.split('/');
expect(urlSpilt[0]).toBe('https:');
expect(urlSpilt[2]).toBe('via.placeholder.com');
expect(urlSpilt[3]).toBe('200x150');
expect(urlSpilt[4]).toHaveLength(6);
expect(urlSpilt[5].split('?')[0]).toHaveLength(6);
expect(urlSpilt[5].split('?')[1]).toContain('text=');
});
it('should return a random url with specified width and height and format', () => {
const imageUrl = faker.image.placeholder.randomUrl(200, 150, 'png');
const urlSpilt = imageUrl.split('/');
expect(urlSpilt[0]).toBe('https:');
expect(urlSpilt[2]).toBe('via.placeholder.com');
expect(urlSpilt[3]).toBe('200x150');
expect(urlSpilt[4]).toHaveLength(6);
expect(urlSpilt[5].split('?')[0]).toHaveLength(10);
expect(urlSpilt[5].split('?')[0]).toContain('.png');
expect(urlSpilt[5].split('?')[1]).toContain('text=');
});
});
});
describe('avatar', () => {
it('should return a random avatar url', () => {
const avatarUrl = faker.image.avatar();
expect(avatarUrl).toBeTypeOf('string');
expect(avatarUrl).toMatch(/^https:\/\//);
expect(() => new URL(avatarUrl)).not.toThrow();
});
});
describe('avatarGitHub', () => {
it('should return a random avatar url from GitHub', () => {
const avatarUrl = faker.image.avatarGitHub();
expect(avatarUrl).toBeTypeOf('string');
expect(avatarUrl).toMatch(
/^https:\/\/avatars\.githubusercontent\.com\/u\/\d+$/
);
});
});
describe('avatarLegacy', () => {
it('should return a random avatar url from cloudflare-ipfs', () => {
const avatarUrl = faker.image.avatarLegacy();
expect(avatarUrl).toBeTypeOf('string');
expect(avatarUrl).toMatch(
/^https:\/\/cloudflare\-ipfs\.com\/ipfs\/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye\/avatar\/\d{1,4}\.jpg$/
);
});
});
describe('url', () => {
it('should return a random image url', () => {
const imageUrl = faker.image.url();
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(/^https:\/\//);
expect(() => new URL(imageUrl)).not.toThrow();
});
it('should return a random image url with a width', () => {
const width = 100;
const imageUrl = faker.image.url({ width });
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(/^https:\/\//);
expect(() => new URL(imageUrl)).not.toThrow();
expect(imageUrl).include(`${width}`);
});
it('should return a random image url with a height', () => {
const height = 100;
const imageUrl = faker.image.url({ height });
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(/^https:\/\//);
expect(() => new URL(imageUrl)).not.toThrow();
expect(imageUrl).include(`${height}`);
});
it('should return a random image url with a width and height', () => {
const width = 128;
const height = 64;
const imageUrl = faker.image.url({ width, height });
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(/^https:\/\//);
expect(() => new URL(imageUrl)).not.toThrow();
expect(imageUrl).include(`${width}`);
expect(imageUrl).include(`${height}`);
});
});
describe('urlLoremFlickr', () => {
it('should return a random image url from LoremFlickr', () => {
const imageUrl = faker.image.urlLoremFlickr();
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https\:\/\/loremflickr\.com\/\d+\/\d+\?lock=\d+$/
);
});
});
describe('urlPicsumPhotos', () => {
it('should return a random image url from PicsumPhotos', () => {
const imageUrl = faker.image.urlPicsumPhotos();
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https\:\/\/picsum\.photos\/seed\/[0-9a-zA-Z]+\/\d+\/\d+$/
);
});
});
describe('urlPlaceholder', () => {
it('should return a random image url from Placeholder', () => {
const imageUrl = faker.image.urlPlaceholder();
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https\:\/\/via\.placeholder\.com\/\d+x\d+\/[0-9a-fA-F]{6}\/[0-9a-fA-F]{6}\.[a-z]{3,4}\?text=.+$/
);
});
});
describe('dataUri', () => {
it('should return a blank data', () => {
const dataUri = faker.image.dataUri({ width: 200, height: 300 });
expect(dataUri).toMatchSnapshot();
});
it('should return a background color data URI', () => {
const dataUri = faker.image.dataUri({
width: 200,
height: 300,
color: 'red',
});
expect(dataUri).toMatchSnapshot();
});
});
});