Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(image): dataUri is not random #2316

Merged
merged 23 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/modules/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export class ImageModule {
* @param options Options for generating a data uri.
* @param options.width The width of the image. Defaults to `640`.
* @param options.height The height of the image. Defaults to `480`.
* @param options.color The color of the image. Defaults to `grey`.
* @param options.color The color of the image. Defaults to a random color.
OmkarPednekar marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...'
Expand All @@ -375,14 +375,18 @@ export class ImageModule {
*/
height?: number;
/**
* The color of the image.
* The color of the image. Must be a color supported by svg. Defaults to a random color.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
OmkarPednekar marked this conversation as resolved.
Show resolved Hide resolved
*
* @default 'grey'
* @default faker.color.rgb()
*/
color?: string;
} = {}
): string {
const { width = 640, height = 480, color = 'grey' } = options;
const {
width = 640,
height = 480,
color = this.faker.color.rgb(),
} = options;

const svgString = `<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="${width}" height="${height}"><rect width="100%" height="100%" fill="${color}"/><text x="${
width / 2
Expand Down
2 changes: 0 additions & 2 deletions test/modules/__snapshots__/image.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,3 @@ exports[`image > 1337 > urlPlaceholder > with width 1`] = `"https://via.placehol
exports[`image > 1337 > urlPlaceholder > with width and height 1`] = `"https://via.placeholder.com/128x128/5c346b/a075bd.jpeg?text=conculco%20voluptatibus%20cerno"`;

exports[`image > dataUri > should return a background color data URI 1`] = `"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"`;

exports[`image > dataUri > should return a blank data 1`] = `"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"`;
3 changes: 2 additions & 1 deletion test/modules/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,9 @@ describe('image', () => {

describe('dataUri', () => {
it('should return a blank data', () => {
const expectedPattern = /^data:image\/svg\+xml;charset=UTF-8,/;
const dataUri = faker.image.dataUri({ width: 200, height: 300 });
expect(dataUri).toMatchSnapshot();
expect(dataUri).toMatch(expectedPattern);
OmkarPednekar marked this conversation as resolved.
Show resolved Hide resolved
});

it('should return a background color data URI', () => {
OmkarPednekar marked this conversation as resolved.
Show resolved Hide resolved
Expand Down