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
|
import isDataURI from 'validator/lib/isDataURI';
import { describe, expect, it } from 'vitest';
import { faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
/**
* Checks that the given address is a valid https address.
*
* An address is considered valid, if it:
*
* - is a string
* - starts with https
* - is a proper url
*
* There is a separate integretation test file for checking if the address is reachable.
*
* @param address The address to check.
*/
function assertValidUrl(address: string): void {
expect(address).toBeTypeOf('string');
expect(address).toMatch(/^https:\/\//);
expect(() => new URL(address)).not.toThrow();
}
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 type', { type: 'svg-base64' })
.it('with all options+base64', {
width: 2,
height: 1337,
color: '#643218',
type: 'svg-base64',
})
.it('with all options+uri', {
width: 42,
height: 314,
color: 'red',
type: 'svg-uri',
});
});
t.describe('personPortrait', (t) => {
t.it('noArgs')
.it('with sex', { sex: 'female' })
.it('with size', { size: 128 })
.it('with sex and size', { sex: 'male', size: 256 });
});
});
describe('avatar', () => {
it('should return a random avatar url', () => {
const actual = faker.image.avatar();
assertValidUrl(actual);
});
});
describe('avatarGitHub', () => {
it('should return a random avatar url from GitHub', () => {
const actual = faker.image.avatarGitHub();
expect(actual).toBeTypeOf('string');
expect(actual).toMatch(
/^https:\/\/avatars\.githubusercontent\.com\/u\/\d+$/
);
assertValidUrl(actual);
});
});
describe('avatarLegacy', () => {
it('should return a random avatar url from cloudflare-ipfs', () => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
const actual = faker.image.avatarLegacy();
expect(actual).toBeTypeOf('string');
expect(actual).toMatch(
/^https:\/\/cloudflare-ipfs\.com\/ipfs\/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye\/avatar\/\d{1,4}\.jpg$/
);
// The links aren't working anymore - there is nothing we can do about it
// assertWebAddress(avatarUrl);
});
});
describe('personPortrait', () => {
it('should return a random avatar url from AI', () => {
const imageUrl = faker.image.personPortrait();
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https:\/\/cdn\.jsdelivr\.net\/gh\/faker-js\/assets-person-portrait\/(female|male)\/512\/\d{1,2}\.jpg$/
);
expect(() => new URL(imageUrl)).not.toThrow();
});
it('should return a random avatar url from AI with fixed size and sex', () => {
const imageUrl = faker.image.personPortrait({ sex: 'male', size: 128 });
expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(
/^https:\/\/cdn\.jsdelivr\.net\/gh\/faker-js\/assets-person-portrait\/male\/128\/\d{1,2}\.jpg$/
);
expect(() => new URL(imageUrl)).not.toThrow();
});
});
describe('url', () => {
it('should return a random image url', () => {
const actual = faker.image.url();
assertValidUrl(actual);
});
it('should return a random image url with a width', () => {
const width = 100;
const actual = faker.image.url({ width });
assertValidUrl(actual);
expect(actual).include(`${width}`);
});
it('should return a random image url with a height', () => {
const height = 100;
const actual = faker.image.url({ height });
assertValidUrl(actual);
expect(actual).include(`${height}`);
});
it('should return a random image url with a width and height', () => {
const width = 128;
const height = 64;
const actual = faker.image.url({ width, height });
assertValidUrl(actual);
expect(actual).include(`${width}`);
expect(actual).include(`${height}`);
});
});
describe('urlLoremFlickr', () => {
it('should return a random image url from LoremFlickr', () => {
const actual = faker.image.urlLoremFlickr();
assertValidUrl(actual);
expect(actual).toMatch(
/^https:\/\/loremflickr\.com\/\d+\/\d+\?lock=\d+$/
);
});
});
describe('urlPicsumPhotos', () => {
it('should return a random image url from PicsumPhotos', () => {
const actual = faker.image.urlPicsumPhotos();
assertValidUrl(actual);
expect(actual).toMatch(
/^https:\/\/picsum\.photos\/seed\/[0-9a-zA-Z]+\/\d+\/\d+(\?(grayscale&?)?(blur=\d+)?)?$/
);
});
});
describe('urlPlaceholder', () => {
it('should return a random image url from Placeholder', () => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
const actual = faker.image.urlPlaceholder();
assertValidUrl(actual);
expect(actual).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 an image data uri', () => {
const actual = faker.image.dataUri();
expect(actual).toMatch(/^data:image\/svg\+xml;/);
expect(actual).toSatisfy(isDataURI);
});
it('should return an uri-encoded image data uri', () => {
const actual = faker.image.dataUri({ type: 'svg-uri' });
expect(actual).toMatch(/^data:image\/svg\+xml;charset=UTF-8,/);
expect(actual).toSatisfy(isDataURI);
});
it('should return a base64 image data uri', () => {
const actual = faker.image.dataUri({ type: 'svg-base64' });
expect(actual).toMatch(/^data:image\/svg\+xml;base64,/);
expect(actual).toSatisfy(isDataURI);
});
it('should return an image data uri with fixed size', () => {
const actual = faker.image.dataUri({
width: 200,
height: 300,
type: 'svg-uri', // required for the regex check
});
expect(actual).toMatch(/^data:image\/svg\+xml;charset=UTF-8,/);
expect(actual).toMatch(/width%3D%22200%22%20height%3D%22300/);
expect(actual).toSatisfy(isDataURI);
});
it('should return an image data uri with a fixed background color', () => {
const actual = faker.image.dataUri({
color: 'red',
type: 'svg-uri', // required for the regex check
});
expect(actual).toMatch(/^data:image\/svg\+xml;charset=UTF-8,/);
expect(actual).toMatch(/fill%3D%22red/);
expect(actual).toSatisfy(isDataURI);
});
});
});
|