Skip to content

Commit

Permalink
fix(urlUtils.test): replace direct manipulation of window.location wi…
Browse files Browse the repository at this point in the history
…th vi.stubGlobal
  • Loading branch information
amalv committed Jan 15, 2024
1 parent 231586b commit 8200aa6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/utils/urlUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { vi } from "vitest";

import { getEnvironmentDependentUrl } from "./urlUtils";

describe("getEnvironmentDependentUrl", () => {
const originalWindowLocation = window.location;

beforeEach(() => {
delete window.location;
window.location = { ...originalWindowLocation };
vi.stubGlobal("window", { location: { ...window.location } });
});

afterEach(() => {
vi.unstubAllEnvs();
vi.unstubAllGlobals();
});

it("returns the correct URL for production environment", () => {
vi.stubEnv("VITE_ENV", "production");
window.location.origin = "https://amalv.github.io";
vi.stubGlobal("window", {
location: { origin: "https://amalv.github.io" },
});

const url = getEnvironmentDependentUrl();

Expand All @@ -23,7 +24,7 @@ describe("getEnvironmentDependentUrl", () => {

it("returns the correct URL for non-production environment", () => {
vi.stubEnv("VITE_ENV", "development");
window.location.origin = "http://localhost:3000";
vi.stubGlobal("window", { location: { origin: "http://localhost:3000" } });

const url = getEnvironmentDependentUrl();

Expand Down

1 comment on commit 8200aa6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.