Skip to content

Commit

Permalink
add a check in isNodeDevStageEnv for undefined (#122)
Browse files Browse the repository at this point in the history
* add a check in isNodeDevStageEnv for undefined

* fix tests

* add more coverage
  • Loading branch information
rami-monday authored Aug 30, 2023
1 parent 5e6aaaf commit d2f05e4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 40 deletions.
114 changes: 75 additions & 39 deletions src/constants-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe("constants", () => {
//setup
beforeEach(() => {
process.env.NODE_ENV = "development";
process.env.MONDAY_DOMAIN = undefined;
process.env.MONDAY_COM_PROTOCOL = undefined;
process.env.MONDAY_COM_DOMAIN = undefined;
process.env.MONDAY_SUBDOMAIN_API = undefined;
Expand Down Expand Up @@ -35,44 +36,79 @@ describe("constants", () => {
expect(constants.MONDAY_OAUTH_TOKEN_URL).to.eq(MONDAY_OAUTH_TOKEN_URL);
});

it("constants should be correct", () => {
const keys = Object.keys(constants);
let key;
let value;
for (let i = 0; i < keys.length; i++) {
key = keys[i];
value = constants[key];

switch (key) {
case "MONDAY_DOMAIN": {
expect(value).to.eq("monday.com");
break;
}

case "MONDAY_PROTOCOL": {
expect(value).to.eq("https");
break;
}

case "MONDAY_API_URL": {
expect(value).to.eq("https://api.monday.com/v2");
break;
}

case "MONDAY_OAUTH_URL": {
expect(value).to.eq("https://auth.monday.com/oauth2/authorize");
break;
}

case "MONDAY_OAUTH_TOKEN_URL": {
expect(value).to.eq("https://auth.monday.com/oauth2/token");
break;
}

default: {
throw new Error(`missing test for this constant: ${key}`);
}
}
}
describe("check that constants are correct when NODE_ENV is development", () => {
it("MONDAY_DOMAIN should be correct", () => {
expect(constants.MONDAY_DOMAIN).to.eq("monday.com");
});

it("MONDAY_PROTOCOL should be correct", () => {
expect(constants.MONDAY_PROTOCOL).to.eq("https");
});

it("MONDAY_API_URL should be correct", () => {
expect(constants.MONDAY_API_URL).to.eq("https://api.monday.com/v2");
});

it("MONDAY_OAUTH_URL should be correct", () => {
expect(constants.MONDAY_OAUTH_URL).to.eq("https://auth.monday.com/oauth2/authorize");
});

it("MONDAY_OAUTH_TOKEN_URL should be correct", () => {
expect(constants.MONDAY_OAUTH_TOKEN_URL).to.eq("https://auth.monday.com/oauth2/token");
});
});

describe("check that constants are correct when NODE_ENV is undefined", () => {
beforeEach(() => {
process.env.NODE_ENV = undefined;
process.env.MONDAY_COM_DOMAIN = "should not be used";
});

it("MONDAY_DOMAIN should be correct", () => {
expect(constants.MONDAY_DOMAIN).to.eq("monday.com");
});

it("MONDAY_PROTOCOL should be correct", () => {
expect(constants.MONDAY_PROTOCOL).to.eq("https");
});

it("MONDAY_API_URL should be correct", () => {
expect(constants.MONDAY_API_URL).to.eq("https://api.monday.com/v2");
});

it("MONDAY_OAUTH_URL should be correct", () => {
expect(constants.MONDAY_OAUTH_URL).to.eq("https://auth.monday.com/oauth2/authorize");
});

it("MONDAY_OAUTH_TOKEN_URL should be correct", () => {
expect(constants.MONDAY_OAUTH_TOKEN_URL).to.eq("https://auth.monday.com/oauth2/token");
});
});

describe("check that constants are correct when NODE_ENV is undefined", () => {
beforeEach(() => {
process.env.NODE_ENV = "production";
process.env.MONDAY_COM_DOMAIN = "should not be used";
});

it("MONDAY_DOMAIN should be correct", () => {
expect(constants.MONDAY_DOMAIN).to.eq("monday.com");
});

it("MONDAY_PROTOCOL should be correct", () => {
expect(constants.MONDAY_PROTOCOL).to.eq("https");
});

it("MONDAY_API_URL should be correct", () => {
expect(constants.MONDAY_API_URL).to.eq("https://api.monday.com/v2");
});

it("MONDAY_OAUTH_URL should be correct", () => {
expect(constants.MONDAY_OAUTH_URL).to.eq("https://auth.monday.com/oauth2/authorize");
});

it("MONDAY_OAUTH_TOKEN_URL should be correct", () => {
expect(constants.MONDAY_OAUTH_TOKEN_URL).to.eq("https://auth.monday.com/oauth2/token");
});
});
});
4 changes: 3 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ function isNodeDevStageEnv() {
}

const getEnvOrDefault = (key, defaultVal) => {
return isNodeDevStageEnv() && process.env[key] !== "undefined" ? process.env[key] : defaultVal;
return isNodeDevStageEnv() && process.env[key] !== "undefined" && process.env[key] !== undefined
? process.env[key]
: defaultVal;
};

const MONDAY_PROTOCOL = () => getEnvOrDefault("MONDAY_COM_PROTOCOL", "https");
Expand Down

0 comments on commit d2f05e4

Please sign in to comment.