/
image.spec.ts
371 lines (308 loc) · 11.7 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
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
describe('image', () => {
describe('lorempicsum', () => {
describe('imageUrl()', () => {
it('should return a random image url from lorempixel', () => {
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('avatar()', () => {
it('should return a random avatar from cloudflare-ipfs', () => {
expect(
faker.image.lorempicsum
.avatar()
.includes(
'cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar'
)
).toBeTruthy();
});
});
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('lorempixel', () => {
describe('imageUrl()', () => {
it('should return a random image url from lorempixel', () => {
const imageUrl = faker.image.lorempixel.imageUrl();
expect(imageUrl).toBe('https://lorempixel.com/640/480');
});
it('should return a random image url from lorempixel with width and height', () => {
const imageUrl = faker.image.lorempixel.imageUrl(100, 100);
expect(imageUrl).toBe('https://lorempixel.com/100/100');
});
it('should return a random image url for a specified category', () => {
const imageUrl = faker.image.lorempixel.imageUrl(100, 100, 'abstract');
expect(imageUrl).toBe('https://lorempixel.com/100/100/abstract');
});
});
describe('avatar()', () => {
it('should return a random avatar from cloudflare-ipfs', () => {
expect(
faker.image.lorempixel
.avatar()
.includes(
'cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar'
)
).toBeTruthy();
});
});
const categories = [
'abstract',
'animals',
'business',
'cats',
'city',
'food',
'nightlife',
'fashion',
'people',
'nature',
'sports',
'technics',
'transport',
];
for (const category of categories) {
describe(`${category}()`, () => {
it(`should return a random ${category} image url`, () => {
const actual = faker.image.lorempixel[category]();
expect(actual).toBe(`https://lorempixel.com/640/480/${category}`);
});
});
}
});
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',
];
for (const category of categories) {
describe(`${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('/');
console.log(imageUrl);
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('dataUri', () => {
it('should return a blank data', () => {
const dataUri = faker.image.dataUri(200, 300);
expect(dataUri).toBe(
'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22grey%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'
);
});
it('should return a custom background color data URI', () => {
const dataUri = faker.image.dataUri(200, 300, 'red');
expect(dataUri).toBe(
'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22red%22%2F%3E%3Ctext%20x%3D%22100%22%20y%3D%22150%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E200x300%3C%2Ftext%3E%3C%2Fsvg%3E'
);
});
});
});