Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explorer: Add tests for makeNodeUrl #2741

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions explorer/src/lib/url/__tests__/makeNodeUrl.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { describe, expect, it } from "vitest";
import { makeNodeUrl } from "..";
// import { JSDOM } from "jsdom";

describe("makeNodeUrl", () => {
const localhostString = window.location.hostname;

it("should return a local URL when VITE_NODE_URL is not set", () => {
delete import.meta.env.VITE_NODE_URL;
expect(makeNodeUrl().hostname).toBe(localhostString);
});

it("should return a local URL when VITE_NODE_URL is an empty string", () => {
import.meta.env.VITE_NODE_URL = "";
expect(makeNodeUrl().hostname).toBe(localhostString);
});

it("should return a local URL with no base path when VITE_NODE_URL is not set and VITE_RUSK_PATH is not set", () => {
delete import.meta.env.VITE_NODE_URL;
delete import.meta.env.VITE_RUSK_PATH;
expect(makeNodeUrl().hostname).toBe(localhostString);
expect(makeNodeUrl().pathname).toBe("/");
});

it("should return a local URL with no base path when VITE_NODE_URL is set to an empty string and VITE_RUSK_PATH is not set", () => {
import.meta.env.VITE_NODE_URL = "";
delete import.meta.env.VITE_RUSK_PATH;
expect(makeNodeUrl().hostname).toBe(localhostString);
expect(makeNodeUrl().pathname).toBe("/");
});

it("should return a local URL with no base path when VITE_NODE_URL is not set and VITE_RUSK_PATH is set to an empty string", () => {
delete import.meta.env.VITE_NODE_URL;
import.meta.env.VITE_RUSK_PATH = "";
expect(makeNodeUrl().hostname).toBe(localhostString);
expect(makeNodeUrl().pathname).toBe("/");
});

it("should return a local URL with no base path when VITE_NODE_URL is set to an empty string and VITE_RUSK_PATH is set to an empty string", () => {
import.meta.env.VITE_NODE_URL = "";
import.meta.env.VITE_RUSK_PATH = "";
expect(makeNodeUrl().hostname).toBe(localhostString);
expect(makeNodeUrl().pathname).toBe("/");
});

it("should return a local URL with a base path when `VITE_NODE_URL` is not set and `VITE_RUSK_PATH` is set to a valid string", () => {
delete import.meta.env.VITE_NODE_URL;
import.meta.env.VITE_RUSK_PATH = "/testing";
expect(makeNodeUrl().hostname).toBe(localhostString);
expect(makeNodeUrl().pathname).toBe("/testing");
});

it("should return a local URL with a base path when `VITE_NODE_URL` is set to an empty string and `VITE_RUSK_PATH` is set to a valid string", () => {
import.meta.env.VITE_NODE_URL = "";
import.meta.env.VITE_RUSK_PATH = "/testing";
expect(makeNodeUrl().hostname).toBe(localhostString);
expect(makeNodeUrl().pathname).toBe("/testing");
});

it("should return the devnet URL when the hostname starts with 'devnet.staging' on the staging URL", () => {
global.window = Object.create(window);

const url = new URL("https://devnet.staging.dusk.network");

Object.defineProperty(window, "location", {
value: {
hostname: url.hostname,
href: url.href,
protocol: url.protocol,
},
});

expect(makeNodeUrl().hostname).toBe("devnet.nodes.dusk.network");
});

it("should return the devnet URL when the hostname starts with 'devnet'", () => {
global.window = Object.create(window);

const url = new URL("https://devnet.dusk.network");

Object.defineProperty(window, "location", {
value: {
hostname: url.hostname,
href: url.href,
protocol: url.protocol,
},
});

expect(makeNodeUrl().hostname).toBe("devnet.nodes.dusk.network");
});

it("should return the testnet URL when the hostname starts with 'testnet.staging' on the staging URL", () => {
global.window = Object.create(window);

const url = new URL("https://testnet.staging.dusk.network");

Object.defineProperty(window, "location", {
value: {
hostname: url.hostname,
href: url.href,
protocol: url.protocol,
},
});

expect(makeNodeUrl().hostname).toBe("testnet.nodes.dusk.network");
});

it("should return the testnet URL when the hostname starts with 'testnet'", () => {
global.window = Object.create(window);

const url = new URL("https://testnet.dusk.network");

Object.defineProperty(window, "location", {
value: {
hostname: url.hostname,
href: url.href,
protocol: url.protocol,
},
});

expect(makeNodeUrl().hostname).toBe("testnet.nodes.dusk.network");
});

it("should return the mainnet URL when the hostname starts with 'apps.staging' on the staging URL", () => {
global.window = Object.create(window);

const url = new URL("https://apps.staging.dusk.network");

Object.defineProperty(window, "location", {
value: {
hostname: url.hostname,
href: url.href,
protocol: url.protocol,
},
});

expect(makeNodeUrl().hostname).toBe("nodes.dusk.network");
});

it("should return the mainnet URL when the hostname starts with 'apps'", () => {
global.window = Object.create(window);

const url = new URL("https://apps.dusk.network");

Object.defineProperty(window, "location", {
value: {
hostname: url.hostname,
href: url.href,
protocol: url.protocol,
},
});

expect(makeNodeUrl().hostname).toBe("nodes.dusk.network");
});
});
19 changes: 11 additions & 8 deletions explorer/src/lib/url/makeNodeUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* @returns {URL} nodeUrl
*/
function makeNodeUrl(path = "") {
if (path && !path.startsWith("/")) {
if (path !== "" && !path.startsWith("/")) {
throw new Error("A path must start with a '/'.");
}

const subDomains = window.location.hostname.split(".");
const hostedNodeDomain = subDomains.slice(1).join(".");
const hostedNodeDomain = subDomains.slice(-2).join(".");
const nodeBaseUrl = import.meta.env.VITE_NODE_URL || "";
const nodeBasePath = import.meta.env.VITE_RUSK_PATH || "";

Expand All @@ -23,24 +23,27 @@ function makeNodeUrl(path = "") {
`${window.location.protocol}${base}${hostedNodeDomain}${nodeBasePath}${path}`
);

let node;
let nodeUrl;

switch (subDomains[0]) {
case "apps": // mainnet
node = buildHostedNodeUrl("nodes.");
nodeUrl = buildHostedNodeUrl("nodes.");
break;
case "devnet":
node = buildHostedNodeUrl("devnet.nodes.");
nodeUrl = buildHostedNodeUrl("devnet.nodes.");
break;
case "testnet":
node = buildHostedNodeUrl("testnet.nodes.");
nodeUrl = buildHostedNodeUrl("testnet.nodes.");
break;
default:
node = new URL(`${nodeBaseUrl}${nodeBasePath}${path}`, import.meta.url);
nodeUrl = new URL(
`${nodeBaseUrl}${nodeBasePath}${path}`,
import.meta.url
);
break;
}

return node;
return nodeUrl;
}

export default makeNodeUrl;
Loading