Skip to content

Commit

Permalink
Tests for Notification Types
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonPetkov96 committed Oct 18, 2023
1 parent b1fb6c3 commit a1cf0a8
Show file tree
Hide file tree
Showing 6 changed files with 760 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 150
"printWidth": 200
}
8 changes: 6 additions & 2 deletions lib/content-deployment.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const cds = require("@sap/cds");
const { validateNotificationTypes, readFile } = require("./utils");
const { createNotificationTypesMap, processNotificationTypes } = require("./notificationTypes");
const { processNotificationTypes } = require("./notificationTypes");
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");

async function deployNotificationTypes() {
setGlobalLogLevel("error");

// read notification types
const notificationTypes = readFile(cds.env.requires?.notifications?.types);
const notificationTypes = readFile(cds.env?.requires?.notifications?.types);

if (validateNotificationTypes(notificationTypes)) {
await processNotificationTypes(notificationTypes);
}
}

deployNotificationTypes();

module.exports = {
deployNotificationTypes
}
2 changes: 1 addition & 1 deletion lib/notificationTypes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
const { getNotificationDestination, doesKeyExist, getPrefix, getNotificationTypesKeyWithPrefix, executeRequest } = require("./utils");
const { getNotificationDestination, doesKeyExist, getPrefix, getNotificationTypesKeyWithPrefix } = require("./utils");
const _ = require("lodash");

const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc";
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
"lodash": "4.17.21"
},
"devDependencies": {
"jest": "^29.6.4"
"jest": "^29.6.4",
"chai": "^4.3.10"
},
"scripts": {
"lint": "npx eslint .",
"test": "npx jest --silent"
"test": "npx jest"
},
"cds": {
"requires": {
Expand Down
49 changes: 49 additions & 0 deletions test/lib/content-deployment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// import required modules and functions
const cds = require("@sap/cds");
const { validateNotificationTypes, readFile } = require("../../lib/utils");
const { processNotificationTypes } = require("../../lib/notificationTypes");
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");
const { expect } = require("chai");

jest.mock("@sap/cds");
jest.mock("../../lib/utils");
jest.mock("../../lib/notificationTypes");
jest.mock("@sap-cloud-sdk/util");

const contentDeployment = require("../../lib/content-deployment");

describe("contentDeployment", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("Given valid notification types | When Deploy is called | Then process is called", async () => {
setGlobalLogLevel.mockImplementation(() => undefined);
readFile.mockImplementation(() => []);
validateNotificationTypes.mockImplementation(() => true);
processNotificationTypes.mockImplementation(() => Promise.resolve());

await contentDeployment.deployNotificationTypes();

console.log(setGlobalLogLevel.mock.calls)
assert.expect(setGlobalLogLevel.mock.calls[0][0]).to.be.equal("error");
assert.expect(readFile.mock.calls[0][0]).to.be.equal(cds.env?.requires?.notifications?.types);
assert.expect(validateNotificationTypes.mock.calls[0][0]).to.be.deep.equal([]);
assert.expect(processNotificationTypes.mock.calls[0][0]).to.be.deep.equal([]);
});

test("Given invalid notification types | When Deploy is called | Then process is called", async () => {
setGlobalLogLevel.mockImplementation(() => undefined);
readFile.mockImplementation(() => []);
validateNotificationTypes.mockImplementation(() => false);
processNotificationTypes.mockImplementation(() => Promise.resolve());

await contentDeployment.deployNotificationTypes();

console.log(setGlobalLogLevel.mock.calls)
assert.expect(setGlobalLogLevel.mock.calls[0][0]).to.be.equal("error");
assert.expect(readFile.mock.calls[0][0]).to.be.equal(cds.env?.requires?.notifications?.types);
assert.expect(validateNotificationTypes.mock.calls[0][0]).to.be.deep.equal([]);
assert.expect(processNotificationTypes.mock.calls[0]).to.be.deep.equal(undefined);
});
});
Loading

0 comments on commit a1cf0a8

Please sign in to comment.