From 38effe11f1d01aee22d939834c2ea68420dcdbda Mon Sep 17 00:00:00 2001 From: Albin Ramovic Date: Tue, 12 Nov 2024 17:28:18 +0100 Subject: [PATCH] tmp change --- __tests__/noOrdInCdsrc.test.js | 88 ++++------------------------- __tests__/ordCdsrc.test.js | 9 +-- __tests__/ordPackageJson.test.js | 13 +---- __tests__/protectedServices.test.js | 30 +++------- 4 files changed, 26 insertions(+), 114 deletions(-) diff --git a/__tests__/noOrdInCdsrc.test.js b/__tests__/noOrdInCdsrc.test.js index 4e9b3c6..dee3006 100644 --- a/__tests__/noOrdInCdsrc.test.js +++ b/__tests__/noOrdInCdsrc.test.js @@ -1,90 +1,22 @@ const cds = require("@sap/cds"); const ord = require("../lib/ord"); const path = require("path"); +const csn = require("./__mocks__/publicResourcesCsn.json"); -// Mock the @sap/cds module -jest.mock("@sap/cds", () => { - const path = require("path"); - let cds = jest.requireActual("@sap/cds"); - cds.root = path.join(__dirname, "bookshop"); - cds.env = {}; +describe("Tests for default ORD document when .cdsrc.json is present", () => { + beforeAll(async () => { }); - return cds; -}); - - -describe.skip("Tests for default ORD document when .cdsrc.json is present", () => { - let csn; + beforeEach(() => { + cds.root = path.join(__dirname, "bookshop"); + cds.env = {}; + }); - beforeAll(async () => { - csn = await cds.load(path.join(__dirname, "bookshop", "srv")); + afterEach(() => { + jest.clearAllMocks(); }); test("Successfully create ORD Documents with defaults", () => { const document = ord(csn); - expect(document).toMatchSnapshot(); - }); - - describe("apiResources", () => { - // eslint-disable-next-line no-useless-escape - const PACKAGE_ID_REGEX = - /^([a-z0-9]+(?:[.][a-z0-9]+)*):(package):([a-zA-Z0-9._\-]+):(v0|v[1-9][0-9]*)$/; - - let document; - - beforeAll(() => { - document = ord(csn); - }); - - test("PartOfPackage values are valid ORD IDs ", () => { - for (const apiResource of document.apiResources) { - expect(apiResource.partOfPackage).toMatch(PACKAGE_ID_REGEX); - } - }); - - test("The partOfPackage references an existing package", () => { - for (const apiResource of document.apiResources) { - expect( - document.packages.find( - (pck) => pck.ordId === apiResource.partOfPackage - ) - ).toBeDefined(); - } - }); - }); - - describe("eventResources", () => { - // eslint-disable-next-line no-useless-escape - const GROUP_ID_REGEX = - /^([a-z0-9-]+(?:[.][a-z0-9-]+)*):([a-zA-Z0-9._\-/]+):([a-z0-9-]+(?:[.][a-z0-9-]+)*):(?[a-zA-Z0-9._\-/]+)$/; - - let document; - - beforeAll(() => { - document = ord(csn); - }); - - test("Assigned to exactly one CDS Service group", () => { - for (const eventResource of document.eventResources) { - expect(eventResource.partOfGroups.length).toEqual(1); - } - }); - - test("The CDS Service Group ID includes the CDS Service identifier", () => { - for (const eventResource of document.eventResources) { - const [groupId] = eventResource.partOfGroups; - expect(groupId).toMatch(GROUP_ID_REGEX); - - const match = GROUP_ID_REGEX.exec(groupId); - if (match && match.groups?.service) { - let service = match.groups?.service; - if (service.startsWith("undefined")) - service = service.replace("undefined.", ""); - const definition = csn.definitions[service]; - expect(definition).toBeDefined(); - expect(definition.kind).toEqual("service"); - } - } - }); + expect(document).not.toBeUndefined(); }); }); diff --git a/__tests__/ordCdsrc.test.js b/__tests__/ordCdsrc.test.js index dbf2e0e..3722e25 100644 --- a/__tests__/ordCdsrc.test.js +++ b/__tests__/ordCdsrc.test.js @@ -5,9 +5,10 @@ const path = require("path"); // Mock the @sap/cds module jest.mock("@sap/cds", () => { const path = require("path"); - let originalCds = jest.requireActual("@sap/cds"); - originalCds.root = path.join(__dirname, "bookshop"); - return originalCds; + let cds = jest.requireActual("@sap/cds"); + cds.root = path.join(__dirname, "bookshop"); + + return cds; }); jest.mock("../lib/date", () => ({ @@ -19,7 +20,7 @@ describe("Tests for default ORD document when .cdsrc.json is present", () => { let csn; beforeAll(async () => { - csn = await cds.load(path.join(__dirname, "bookshop", "srv")); + csn = await cds.load(path.join(cds.root, "srv")); }); test("Successfully create ORD Documents with defaults", () => { diff --git a/__tests__/ordPackageJson.test.js b/__tests__/ordPackageJson.test.js index 0b0558d..867cfee 100644 --- a/__tests__/ordPackageJson.test.js +++ b/__tests__/ordPackageJson.test.js @@ -2,15 +2,6 @@ const cds = require("@sap/cds"); const ord = require("../lib/ord"); const path = require("path"); - -// Mock the @sap/cds module -jest.mock("@sap/cds", () => { - const path = require("path"); - let originalCds = jest.requireActual("@sap/cds"); - originalCds.root = path.join(__dirname, "bookshop"); - return originalCds; -}); - jest.mock("../lib/date", () => ({ getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00") })); @@ -19,9 +10,11 @@ describe("Tests for default ORD document when .cdsrc.json is not present", () => let csn; beforeAll(async () => { - cds.env["ord"] = ""; csn = await cds.load(path.join(__dirname, "bookshop", "srv")); + }); + beforeEach(() => { + cds.root = path.join(__dirname, "bookshop"); }); test("Successfully create ORD Documents with defaults", () => { diff --git a/__tests__/protectedServices.test.js b/__tests__/protectedServices.test.js index 9f59f95..61a3ad3 100644 --- a/__tests__/protectedServices.test.js +++ b/__tests__/protectedServices.test.js @@ -1,29 +1,15 @@ +const cds = require("@sap/cds"); +const internal_csn = require("./__mocks__/internalResourcesCsn.json"); const ord = require("../lib/ord"); +const path = require("path"); const private_csn = require("./__mocks__/privateResourcesCsn.json"); -const internal_csn = require("./__mocks__/internalResourcesCsn.json"); - - -// Mock the @sap/cds module -jest.mock("@sap/cds", () => { - const path = require("path"); - let cds = jest.requireActual("@sap/cds"); - cds.root = path.join(__dirname, "bookshop"); - - return cds; -}); - -jest.mock("../lib/extendOrdWithCustom", () => ({ - extendCustomORDContentIfExists: jest.fn( - (_appConfig, ordContent, _lazyLogger) => { - console.log( - "The custom.ord.json file is not considered for the purpose of this test." - ); - return ordContent; - } - ), -})); describe("Tests for ORD document when there is no public service", () => { + beforeAll(() => { + cds.root = path.join(__dirname, "bookshop"); + cds.env = {}; + }); + afterAll(() => { jest.clearAllMocks(); })