From b915db099d24fe264a94846c39dfd76bad16f940 Mon Sep 17 00:00:00 2001 From: Vedanta Krishna Date: Fri, 26 Jul 2024 14:47:19 +0530 Subject: [PATCH] fix the avatar url in faker js --- lib/superstring/dynamic-variables.js | 34 +++++++++++++++++++++++++++- test/unit/dynamic-variables.test.js | 7 ++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/superstring/dynamic-variables.js b/lib/superstring/dynamic-variables.js index aeff852b4..2bfcb6434 100644 --- a/lib/superstring/dynamic-variables.js +++ b/lib/superstring/dynamic-variables.js @@ -75,6 +75,28 @@ var faker = require('@faker-js/faker/locale/en'), '/var/yp' ], + /** + * Copied over from: https://github.com/faker-js/faker/blob/next/src/modules/image/index.ts#L27 + * Generates a random avatar from GitHub. + * + * @example 'https://avatars.githubusercontent.com/u/97165289' + */ + avatarGitHub = () => { + return `https://avatars.githubusercontent.com/u/${faker.datatype.number(100000000)}`; + }, + + /** + * Copied over from: https://github.com/faker-js/faker/blob/next/src/modules/image/index.ts#L27 + * Generates a random avatar from + * `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar`. + * + * @example 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg' + */ + avatarLegacy = () => { + // eslint-disable-next-line max-len + return `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/${faker.datatype.number(1249)}.jpg`; + }, + // generators for $random* variables dynamicGenerators = { $guid: { @@ -371,7 +393,17 @@ var faker = require('@faker-js/faker/locale/en'), $randomAvatarImage: { description: 'A random avatar image', - generator: faker.image.avatar + generator: () => { + // We have overridden the avatar generator to be similar to current + // latest version (v9) of faker.js. We don't want to upgrade to the latest + // version of faker.js as it is significantly big in size. + const avatarMethod = faker.random.arrayElement([ + avatarLegacy, + avatarGitHub + ]); + + return avatarMethod(); + } }, $randomImageUrl: { description: 'A URL for a random image', diff --git a/test/unit/dynamic-variables.test.js b/test/unit/dynamic-variables.test.js index 9d0ac370e..0e700ee1c 100644 --- a/test/unit/dynamic-variables.test.js +++ b/test/unit/dynamic-variables.test.js @@ -97,5 +97,12 @@ describe('Dynamic variable', function () { expect(directoryPath).to.not.be.undefined; expect(directoryPath).to.not.be.null; }); + + it('$randomAvatarImage returns a random avatar image', function () { + var avatarImage = dynamicVariables.$randomAvatarImage.generator(); + + expect(avatarImage).to.not.be.undefined; + expect(avatarImage).to.not.be.null; + }); }); });