Skip to content

Commit df39a2a

Browse files
fix: Replace any types with proper window.location mocking in configUtils tests
1 parent c9a4238 commit df39a2a

File tree

1 file changed

+33
-46
lines changed

1 file changed

+33
-46
lines changed

client/src/utils/__tests__/configUtils.test.ts

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import {
88
import { DEFAULT_INSPECTOR_CONFIG } from "../../lib/constants";
99
import { InspectorConfig } from "../../lib/configurationTypes";
1010

11+
// Helper to mock window.location
12+
function mockWindowLocation(href: string) {
13+
Object.defineProperty(window, "location", {
14+
value: { ...window.location, href },
15+
writable: true,
16+
configurable: true,
17+
});
18+
}
19+
1120
describe("configUtils", () => {
1221
describe("getMCPProxyAuthToken", () => {
1322
test("returns token and default header name", () => {
@@ -83,26 +92,21 @@ describe("configUtils", () => {
8392
// Clear localStorage
8493
localStorage.clear();
8594
// Mock window.location
86-
delete (window as any).location;
87-
(window as any).location = {
88-
...originalLocation,
89-
href: "http://localhost:6274",
90-
};
95+
mockWindowLocation("http://localhost:6274");
9196
});
9297

9398
afterEach(() => {
94-
(window as any).location = originalLocation;
99+
mockWindowLocation(originalLocation.href);
95100
});
96101

97102
test("returns transport type from URL query parameter", () => {
98-
(window as any).location.href = "http://localhost:6274/?transport=sse";
103+
mockWindowLocation("http://localhost:6274/?transport=sse");
99104
expect(getInitialTransportType()).toBe("sse");
100105

101-
(window as any).location.href =
102-
"http://localhost:6274/?transport=streamable-http";
106+
mockWindowLocation("http://localhost:6274/?transport=streamable-http");
103107
expect(getInitialTransportType()).toBe("streamable-http");
104108

105-
(window as any).location.href = "http://localhost:6274/?transport=stdio";
109+
mockWindowLocation("http://localhost:6274/?transport=stdio");
106110
expect(getInitialTransportType()).toBe("stdio");
107111
});
108112

@@ -117,14 +121,12 @@ describe("configUtils", () => {
117121

118122
test("query parameter takes precedence over localStorage", () => {
119123
localStorage.setItem("lastTransportType", "sse");
120-
(window as any).location.href =
121-
"http://localhost:6274/?transport=streamable-http";
124+
mockWindowLocation("http://localhost:6274/?transport=streamable-http");
122125
expect(getInitialTransportType()).toBe("streamable-http");
123126
});
124127

125128
test("ignores invalid transport type from query param", () => {
126-
(window as any).location.href =
127-
"http://localhost:6274/?transport=invalid";
129+
mockWindowLocation("http://localhost:6274/?transport=invalid");
128130
expect(getInitialTransportType()).toBe("stdio");
129131
});
130132
});
@@ -134,20 +136,17 @@ describe("configUtils", () => {
134136

135137
beforeEach(() => {
136138
localStorage.clear();
137-
delete (window as any).location;
138-
(window as any).location = {
139-
...originalLocation,
140-
href: "http://localhost:6274",
141-
};
139+
mockWindowLocation("http://localhost:6274");
142140
});
143141

144142
afterEach(() => {
145-
(window as any).location = originalLocation;
143+
mockWindowLocation(originalLocation.href);
146144
});
147145

148146
test("returns serverUrl from query parameter", () => {
149-
(window as any).location.href =
150-
"http://localhost:6274/?serverUrl=http://example.com/sse";
147+
mockWindowLocation(
148+
"http://localhost:6274/?serverUrl=http://example.com/sse",
149+
);
151150
expect(getInitialSseUrl()).toBe("http://example.com/sse");
152151
});
153152

@@ -162,15 +161,15 @@ describe("configUtils", () => {
162161

163162
test("query parameter takes precedence over localStorage", () => {
164163
localStorage.setItem("lastSseUrl", "http://stored.com/sse");
165-
(window as any).location.href =
166-
"http://localhost:6274/?serverUrl=http://param.com/sse";
164+
mockWindowLocation(
165+
"http://localhost:6274/?serverUrl=http://param.com/sse",
166+
);
167167
expect(getInitialSseUrl()).toBe("http://param.com/sse");
168168
});
169169

170170
test("handles encoded URLs in query parameters", () => {
171171
const encodedUrl = encodeURIComponent("http://example.com/sse?token=123");
172-
(window as any).location.href =
173-
`http://localhost:6274/?serverUrl=${encodedUrl}`;
172+
mockWindowLocation(`http://localhost:6274/?serverUrl=${encodedUrl}`);
174173
expect(getInitialSseUrl()).toBe("http://example.com/sse?token=123");
175174
});
176175
});
@@ -180,20 +179,15 @@ describe("configUtils", () => {
180179

181180
beforeEach(() => {
182181
localStorage.clear();
183-
delete (window as any).location;
184-
(window as any).location = {
185-
...originalLocation,
186-
href: "http://localhost:6274",
187-
};
182+
mockWindowLocation("http://localhost:6274");
188183
});
189184

190185
afterEach(() => {
191-
(window as any).location = originalLocation;
186+
mockWindowLocation(originalLocation.href);
192187
});
193188

194189
test("returns serverCommand from query parameter", () => {
195-
(window as any).location.href =
196-
"http://localhost:6274/?serverCommand=node";
190+
mockWindowLocation("http://localhost:6274/?serverCommand=node");
197191
expect(getInitialCommand()).toBe("node");
198192
});
199193

@@ -208,8 +202,7 @@ describe("configUtils", () => {
208202

209203
test("handles encoded commands in query parameters", () => {
210204
const encodedCmd = encodeURIComponent("node server.js");
211-
(window as any).location.href =
212-
`http://localhost:6274/?serverCommand=${encodedCmd}`;
205+
mockWindowLocation(`http://localhost:6274/?serverCommand=${encodedCmd}`);
213206
expect(getInitialCommand()).toBe("node server.js");
214207
});
215208
});
@@ -219,20 +212,15 @@ describe("configUtils", () => {
219212

220213
beforeEach(() => {
221214
localStorage.clear();
222-
delete (window as any).location;
223-
(window as any).location = {
224-
...originalLocation,
225-
href: "http://localhost:6274",
226-
};
215+
mockWindowLocation("http://localhost:6274");
227216
});
228217

229218
afterEach(() => {
230-
(window as any).location = originalLocation;
219+
mockWindowLocation(originalLocation.href);
231220
});
232221

233222
test("returns serverArgs from query parameter", () => {
234-
(window as any).location.href =
235-
"http://localhost:6274/?serverArgs=--debug";
223+
mockWindowLocation("http://localhost:6274/?serverArgs=--debug");
236224
expect(getInitialArgs()).toBe("--debug");
237225
});
238226

@@ -247,8 +235,7 @@ describe("configUtils", () => {
247235

248236
test("handles encoded args in query parameters", () => {
249237
const encodedArgs = encodeURIComponent("--port 3000 --host 0.0.0.0");
250-
(window as any).location.href =
251-
`http://localhost:6274/?serverArgs=${encodedArgs}`;
238+
mockWindowLocation(`http://localhost:6274/?serverArgs=${encodedArgs}`);
252239
expect(getInitialArgs()).toBe("--port 3000 --host 0.0.0.0");
253240
});
254241
});

0 commit comments

Comments
 (0)