From 74925a0bc22d884a426983e038114da30fa57f77 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Fri, 15 Sep 2023 22:40:50 +0200 Subject: [PATCH] test: simplify random seeded tests --- test/modules/__snapshots__/image.spec.ts.snap | 22 +++-- test/modules/image.spec.ts | 80 ++++++++----------- 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/test/modules/__snapshots__/image.spec.ts.snap b/test/modules/__snapshots__/image.spec.ts.snap index 5874fc13ef4..38eab14eb1e 100644 --- a/test/modules/__snapshots__/image.spec.ts.snap +++ b/test/modules/__snapshots__/image.spec.ts.snap @@ -8,7 +8,9 @@ exports[`image > 42 > avatarLegacy 1`] = `"https://cloudflare-ipfs.com/ipfs/Qmd3 exports[`image > 42 > dataUri > noArgs 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%22640%22%20height%3D%22480%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22%238be4ab%22%2F%3E%3Ctext%20x%3D%22320%22%20y%3D%22240%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E640x480%3C%2Ftext%3E%3C%2Fsvg%3E"`; -exports[`image > 42 > dataUri > with all options 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4Ij48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSJibHVlIi8+PHRleHQgeD0iNjQiIHk9IjY0IiBmb250LXNpemU9IjIwIiBhbGlnbm1lbnQtYmFzZWxpbmU9Im1pZGRsZSIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0id2hpdGUiPjEyOHgxMjg8L3RleHQ+PC9zdmc+"`; +exports[`image > 42 > dataUri > with all options+base64 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIyIiBoZWlnaHQ9IjEzMzciPjxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9IiM2NDMyMTgiLz48dGV4dCB4PSIxIiB5PSI2NjguNSIgZm9udC1zaXplPSIyMCIgYWxpZ25tZW50LWJhc2VsaW5lPSJtaWRkbGUiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj4yeDEzMzc8L3RleHQ+PC9zdmc+"`; + +exports[`image > 42 > dataUri > with all options+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%2242%22%20height%3D%22314%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22red%22%2F%3E%3Ctext%20x%3D%2221%22%20y%3D%22157%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E42x314%3C%2Ftext%3E%3C%2Fsvg%3E"`; exports[`image > 42 > dataUri > with color 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%22640%22%20height%3D%22480%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22blue%22%2F%3E%3Ctext%20x%3D%22320%22%20y%3D%22240%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E640x480%3C%2Ftext%3E%3C%2Fsvg%3E"`; @@ -82,7 +84,9 @@ exports[`image > 1211 > avatarLegacy 1`] = `"https://cloudflare-ipfs.com/ipfs/Qm exports[`image > 1211 > dataUri > noArgs 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%22640%22%20height%3D%22480%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22%23eadb42%22%2F%3E%3Ctext%20x%3D%22320%22%20y%3D%22240%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E640x480%3C%2Ftext%3E%3C%2Fsvg%3E"`; -exports[`image > 1211 > dataUri > with all options 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4Ij48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSJibHVlIi8+PHRleHQgeD0iNjQiIHk9IjY0IiBmb250LXNpemU9IjIwIiBhbGlnbm1lbnQtYmFzZWxpbmU9Im1pZGRsZSIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0id2hpdGUiPjEyOHgxMjg8L3RleHQ+PC9zdmc+"`; +exports[`image > 1211 > dataUri > with all options+base64 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIyIiBoZWlnaHQ9IjEzMzciPjxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9IiM2NDMyMTgiLz48dGV4dCB4PSIxIiB5PSI2NjguNSIgZm9udC1zaXplPSIyMCIgYWxpZ25tZW50LWJhc2VsaW5lPSJtaWRkbGUiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj4yeDEzMzc8L3RleHQ+PC9zdmc+"`; + +exports[`image > 1211 > dataUri > with all options+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%2242%22%20height%3D%22314%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22red%22%2F%3E%3Ctext%20x%3D%2221%22%20y%3D%22157%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E42x314%3C%2Ftext%3E%3C%2Fsvg%3E"`; exports[`image > 1211 > dataUri > with color 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%22640%22%20height%3D%22480%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22blue%22%2F%3E%3Ctext%20x%3D%22320%22%20y%3D%22240%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E640x480%3C%2Ftext%3E%3C%2Fsvg%3E"`; @@ -156,7 +160,9 @@ exports[`image > 1337 > avatarLegacy 1`] = `"https://cloudflare-ipfs.com/ipfs/Qm exports[`image > 1337 > dataUri > noArgs 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%22640%22%20height%3D%22480%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22%235c346b%22%2F%3E%3Ctext%20x%3D%22320%22%20y%3D%22240%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E640x480%3C%2Ftext%3E%3C%2Fsvg%3E"`; -exports[`image > 1337 > dataUri > with all options 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4Ij48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSJibHVlIi8+PHRleHQgeD0iNjQiIHk9IjY0IiBmb250LXNpemU9IjIwIiBhbGlnbm1lbnQtYmFzZWxpbmU9Im1pZGRsZSIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0id2hpdGUiPjEyOHgxMjg8L3RleHQ+PC9zdmc+"`; +exports[`image > 1337 > dataUri > with all options+base64 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIyIiBoZWlnaHQ9IjEzMzciPjxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9IiM2NDMyMTgiLz48dGV4dCB4PSIxIiB5PSI2NjguNSIgZm9udC1zaXplPSIyMCIgYWxpZ25tZW50LWJhc2VsaW5lPSJtaWRkbGUiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj4yeDEzMzc8L3RleHQ+PC9zdmc+"`; + +exports[`image > 1337 > dataUri > with all options+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%2242%22%20height%3D%22314%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22red%22%2F%3E%3Ctext%20x%3D%2221%22%20y%3D%22157%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E42x314%3C%2Ftext%3E%3C%2Fsvg%3E"`; exports[`image > 1337 > dataUri > with color 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%22640%22%20height%3D%22480%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22blue%22%2F%3E%3Ctext%20x%3D%22320%22%20y%3D%22240%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E640x480%3C%2Ftext%3E%3C%2Fsvg%3E"`; @@ -221,13 +227,3 @@ exports[`image > 1337 > urlPlaceholder > with textColor 1`] = `"https://via.plac exports[`image > 1337 > urlPlaceholder > with width 1`] = `"https://via.placeholder.com/128x1048/c346ba/075bd5.jpeg?text=voluptatibus%20cerno%20tabella"`; 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 background color svg-base64 data URI 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSI0NDQiIGhlaWdodD0iMzAiPjxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9ImdyZWVuIi8+PHRleHQgeD0iMjIyIiB5PSIxNSIgZm9udC1zaXplPSIyMCIgYWxpZ25tZW50LWJhc2VsaW5lPSJtaWRkbGUiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IndoaXRlIj40NDR4MzA8L3RleHQ+PC9zdmc+"`; - -exports[`image > dataUri > should return a background color svg-uri 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%221%22%20height%3D%221234%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22yellow%22%2F%3E%3Ctext%20x%3D%220.5%22%20y%3D%22617%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E1x1234%3C%2Ftext%3E%3C%2Fsvg%3E"`; - -exports[`image > dataUri > should return a blank svg-base64 data 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIxMjMiIGhlaWdodD0iNDU2Ij48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSIjNzViZDU3Ii8+PHRleHQgeD0iNjEuNSIgeT0iMjI4IiBmb250LXNpemU9IjIwIiBhbGlnbm1lbnQtYmFzZWxpbmU9Im1pZGRsZSIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0id2hpdGUiPjEyM3g0NTY8L3RleHQ+PC9zdmc+"`; - -exports[`image > dataUri > should return a blank svg-uri 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%22456%22%20height%3D%22789%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22%23f5a62b%22%2F%3E%3Ctext%20x%3D%22228%22%20y%3D%22394.5%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E456x789%3C%2Ftext%3E%3C%2Fsvg%3E"`; diff --git a/test/modules/image.spec.ts b/test/modules/image.spec.ts index 9951f874376..d77b854d16d 100644 --- a/test/modules/image.spec.ts +++ b/test/modules/image.spec.ts @@ -1,3 +1,4 @@ +import isDataURI from 'validator/lib/isDataURI'; import { describe, expect, it } from 'vitest'; import { faker } from '../../src'; import { seededTests } from './../support/seededRuns'; @@ -75,11 +76,17 @@ describe('image', () => { .it('with width and height', { width: 128, height: 128 }) .it('with color', { color: 'blue' }) .it('with type', { type: 'svg-base64' }) - .it('with all options', { - width: 128, - height: 128, - color: 'blue', + .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', }); }); @@ -484,60 +491,43 @@ describe('image', () => { }); describe('dataUri', () => { - it('should return a blank data', () => { - const dataUri = faker.image.dataUri({ width: 200, height: 300 }); - expect(dataUri).toMatch(/^data:image\/svg\+xml;charset=UTF-8,/); - expect(dataUri).toMatch(/width%3D%22200%22%20height%3D%22300/); + it('should return an image data uri', () => { + const dataUri = faker.image.dataUri(); + expect(dataUri).toMatch(/^data:image\/svg\+xml;/); + expect(dataUri).toSatisfy(isDataURI); }); - it('should return a background color data URI', () => { - const dataUri = faker.image.dataUri({ - width: 200, - height: 300, - color: 'red', - }); + it('should return an uri-encoded image data uri', () => { + const dataUri = faker.image.dataUri({ type: 'svg-uri' }); expect(dataUri).toMatch(/^data:image\/svg\+xml;charset=UTF-8,/); - expect(dataUri).toMatch(/width%3D%22200%22%20height%3D%22300/); - expect(dataUri).toMatch(/fill%3D%22red/); - expect(dataUri).toMatchSnapshot(); + expect(dataUri).toSatisfy(isDataURI); }); - it('should return a blank svg-uri data', () => { - const dataUri = faker.image.dataUri({ - width: 456, - height: 789, - type: 'svg-uri', - }); - expect(dataUri).toMatchSnapshot(); + it('should return a base64 image data uri', () => { + const dataUri = faker.image.dataUri({ type: 'svg-base64' }); + expect(dataUri).toMatch(/^data:image\/svg\+xml;base64,/); + expect(dataUri).toSatisfy(isDataURI); }); - it('should return a background color svg-uri data URI', () => { + it('should return an image data uri with fixed size', () => { const dataUri = faker.image.dataUri({ - width: 1, - height: 1234, - color: 'yellow', - type: 'svg-uri', - }); - expect(dataUri).toMatchSnapshot(); - }); - - it('should return a blank svg-base64 data', () => { - const dataUri = faker.image.dataUri({ - width: 123, - height: 456, - type: 'svg-base64', + width: 200, + height: 300, + type: 'svg-uri', // required for the regex check }); - expect(dataUri).toMatchSnapshot(); + expect(dataUri).toMatch(/^data:image\/svg\+xml;charset=UTF-8,/); + expect(dataUri).toMatch(/width%3D%22200%22%20height%3D%22300/); + expect(dataUri).toSatisfy(isDataURI); }); - it('should return a background color svg-base64 data URI', () => { + it('should return an image data uri with a fixed background color', () => { const dataUri = faker.image.dataUri({ - width: 444, - height: 30, - color: 'green', - type: 'svg-base64', + color: 'red', + type: 'svg-uri', // required for the regex check }); - expect(dataUri).toMatchSnapshot(); + expect(dataUri).toMatch(/^data:image\/svg\+xml;charset=UTF-8,/); + expect(dataUri).toMatch(/fill%3D%22red/); + expect(dataUri).toSatisfy(isDataURI); }); }); });