From 991a09b878e832591ee9b8a14e9830fb34356623 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 21 Nov 2024 14:00:05 +0100 Subject: [PATCH] Bug 38503: Read patron swagger spec file and implement buildSamplePatron --- package.json | 4 ++- t/cypress/integration/t/mockData.ts | 9 +++++++ t/cypress/plugins/index.js | 35 ++++++++++++++++---------- t/cypress/plugins/mockData.js | 39 +++++++++++++++++++++++++++++ t/cypress/plugins/readYamlFile.js | 14 +++++++++++ 5 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 t/cypress/integration/t/mockData.ts create mode 100644 t/cypress/plugins/mockData.js create mode 100644 t/cypress/plugins/readYamlFile.js diff --git a/package.json b/package.json index 1040420d1c5..2e31e6f6de4 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "dependencies": { "@cypress/vue": "^3.1.1", "@cypress/webpack-dev-server": "^1.8.3", + "@faker-js/faker": "^9.2.0", "@fortawesome/fontawesome-svg-core": "^6.1.0", "@fortawesome/free-solid-svg-icons": "^6.0.0", "@fortawesome/vue-fontawesome": "^3.0.0-5", @@ -42,7 +43,8 @@ "vue": "^3.2.31", "vue-flatpickr-component": "^9", "vue-router": "^4.0.14", - "vue-select": "4.0.0-beta.3" + "vue-select": "4.0.0-beta.3", + "yaml": "^2.6.1" }, "scripts": { "css:build": "gulp css && gulp css --view opac", diff --git a/t/cypress/integration/t/mockData.ts b/t/cypress/integration/t/mockData.ts new file mode 100644 index 00000000000..4afd0c33208 --- /dev/null +++ b/t/cypress/integration/t/mockData.ts @@ -0,0 +1,9 @@ +import { mount } from "@cypress/vue"; + +describe("Generate Random Patron", () => { + it("should generate a random patron from the schema", () => { + cy.task("buildSamplePatron").then(mockPatron => { + expect(mockPatron).to.have.property("patron_id"); + }); + }); +}); diff --git a/t/cypress/plugins/index.js b/t/cypress/plugins/index.js index e35c437a8bc..2936a8f0074 100644 --- a/t/cypress/plugins/index.js +++ b/t/cypress/plugins/index.js @@ -1,19 +1,28 @@ -const { startDevServer } = require('@cypress/webpack-dev-server') -const webpackConfig = require('@vue/cli-service/webpack.config.js') +const { startDevServer } = require("@cypress/webpack-dev-server"); +const webpackConfig = require("@vue/cli-service/webpack.config.js"); module.exports = (on, config) => { - on('dev-server:start', options => - startDevServer({ - options, - webpackConfig - }) - ) + on("dev-server:start", options => + startDevServer({ + options, + webpackConfig, + }) + ); - return config -} + return config; +}; -const mysql = require('cypress-mysql'); +const mysql = require("cypress-mysql"); module.exports = (on, config) => { - mysql.configurePlugin(on); -} + mysql.configurePlugin(on); +}; + +const { buildSamplePatron } = require("./mockData.js"); + +module.exports = (on, config) => { + on("task", { + buildSamplePatron, + }); + return config; +}; diff --git a/t/cypress/plugins/mockData.js b/t/cypress/plugins/mockData.js new file mode 100644 index 00000000000..0c84043b0af --- /dev/null +++ b/t/cypress/plugins/mockData.js @@ -0,0 +1,39 @@ +const { faker } = require("@faker-js/faker"); +const { readYamlFile } = require("./../plugins/readYamlFile.js"); + +const generateMockData = type => { + switch (type) { + case "string": + return faker.lorem.words(3); + case "integer": + return faker.number.int(); + case "boolean": + return faker.datatype.boolean(); + case "array": + return [faker.lorem.word(), faker.lorem.word()]; + case "number": + return faker.number.float(); + default: + return faker.lorem.word(); + } +}; + +const generateDataFromSchema = properties => { + const mockData = {}; + Object.entries(properties).forEach(([key, value]) => { + mockData[key] = generateMockData(value.type); + }); + return mockData; +}; + +const buildSamplePatron = () => { + const yamlPath = "api/v1/swagger/definitions/patron.yaml"; + const schema = readYamlFile(yamlPath); + return generateDataFromSchema(schema.properties); +}; + +module.exports = { + generateMockData, + generateDataFromSchema, + buildSamplePatron, +}; diff --git a/t/cypress/plugins/readYamlFile.js b/t/cypress/plugins/readYamlFile.js new file mode 100644 index 00000000000..369650c44d5 --- /dev/null +++ b/t/cypress/plugins/readYamlFile.js @@ -0,0 +1,14 @@ +const path = require("path"); +const fs = require("fs"); +const yaml = require("yaml"); + +const readYamlFile = filePath => { + const absolutePath = path.resolve(filePath); + if (!fs.existsSync(absolutePath)) { + throw new Error(`File not found: ${absolutePath}`); + } + const fileContent = fs.readFileSync(absolutePath, "utf8"); + return yaml.parse(fileContent); +}; + +module.exports = { readYamlFile }; -- 2.34.1