From 92bc6684d59db4c08491566cc667906df490f277 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 21 Nov 2024 14:08:19 +0100 Subject: [PATCH] Bug 38503: Allow to pass values --- t/cypress/integration/t/mockData.ts | 8 ++++++++ t/cypress/plugins/mockData.js | 16 ++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/t/cypress/integration/t/mockData.ts b/t/cypress/integration/t/mockData.ts index 6624909173e..cdc1c08ec53 100644 --- a/t/cypress/integration/t/mockData.ts +++ b/t/cypress/integration/t/mockData.ts @@ -6,6 +6,14 @@ describe("Generate Random Patron", () => { expect(mockPatron).to.have.property("patron_id"); }); }); + it("should generate a random patron with predefined values", () => { + cy.task("buildSampleObject", { + object: "patron", + values: { surname: "test_surname" }, + }).then(mockPatron => { + expect(mockPatron.surname).to.equal("test_surname"); + }); + }); }); describe("Generate Random Patrons", () => { diff --git a/t/cypress/plugins/mockData.js b/t/cypress/plugins/mockData.js index 5bb9bb75b25..02ec1a7aba6 100644 --- a/t/cypress/plugins/mockData.js +++ b/t/cypress/plugins/mockData.js @@ -26,27 +26,31 @@ const generateMockData = type => { } }; -const generateDataFromSchema = properties => { +const generateDataFromSchema = (properties, values = {}) => { const mockData = {}; Object.entries(properties).forEach(([key, value]) => { - mockData[key] = generateMockData(value.type); + if (values.hasOwnProperty(key)) { + mockData[key] = values[key]; + } else { + mockData[key] = generateMockData(value.type); + } }); return mockData; }; -const buildSampleObjects = ({ object, count = 1 }) => { +const buildSampleObjects = ({ object, values, count = 1 }) => { if (!objects.hasOwnProperty(object)) { throw new Error(`Object type not supported: ${object}`); } const yamlPath = `api/v1/swagger/definitions/${objects[object].spec}.yaml`; const schema = readYamlFile(yamlPath); return Array.from({ length: count }, () => - generateDataFromSchema(schema.properties) + generateDataFromSchema(schema.properties, values) ); }; -const buildSampleObject = object => { - return buildSampleObjects({ object })[0]; +const buildSampleObject = ({ object, values = {} }) => { + return buildSampleObjects({ object, values })[0]; }; module.exports = { -- 2.34.1