Skip to content

Commit fdbbef8

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

File tree

1 file changed

+29
-46
lines changed

1 file changed

+29
-46
lines changed

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

Lines changed: 29 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,15 @@ 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("http://localhost:6274/?serverUrl=http://example.com/sse");
151148
expect(getInitialSseUrl()).toBe("http://example.com/sse");
152149
});
153150

@@ -162,15 +159,13 @@ describe("configUtils", () => {
162159

163160
test("query parameter takes precedence over localStorage", () => {
164161
localStorage.setItem("lastSseUrl", "http://stored.com/sse");
165-
(window as any).location.href =
166-
"http://localhost:6274/?serverUrl=http://param.com/sse";
162+
mockWindowLocation("http://localhost:6274/?serverUrl=http://param.com/sse");
167163
expect(getInitialSseUrl()).toBe("http://param.com/sse");
168164
});
169165

170166
test("handles encoded URLs in query parameters", () => {
171167
const encodedUrl = encodeURIComponent("http://example.com/sse?token=123");
172-
(window as any).location.href =
173-
`http://localhost:6274/?serverUrl=${encodedUrl}`;
168+
mockWindowLocation(`http://localhost:6274/?serverUrl=${encodedUrl}`);
174169
expect(getInitialSseUrl()).toBe("http://example.com/sse?token=123");
175170
});
176171
});
@@ -180,20 +175,15 @@ describe("configUtils", () => {
180175

181176
beforeEach(() => {
182177
localStorage.clear();
183-
delete (window as any).location;
184-
(window as any).location = {
185-
...originalLocation,
186-
href: "http://localhost:6274",
187-
};
178+
mockWindowLocation("http://localhost:6274");
188179
});
189180

190181
afterEach(() => {
191-
(window as any).location = originalLocation;
182+
mockWindowLocation(originalLocation.href);
192183
});
193184

194185
test("returns serverCommand from query parameter", () => {
195-
(window as any).location.href =
196-
"http://localhost:6274/?serverCommand=node";
186+
mockWindowLocation("http://localhost:6274/?serverCommand=node");
197187
expect(getInitialCommand()).toBe("node");
198188
});
199189

@@ -208,8 +198,7 @@ describe("configUtils", () => {
208198

209199
test("handles encoded commands in query parameters", () => {
210200
const encodedCmd = encodeURIComponent("node server.js");
211-
(window as any).location.href =
212-
`http://localhost:6274/?serverCommand=${encodedCmd}`;
201+
mockWindowLocation(`http://localhost:6274/?serverCommand=${encodedCmd}`);
213202
expect(getInitialCommand()).toBe("node server.js");
214203
});
215204
});
@@ -219,20 +208,15 @@ describe("configUtils", () => {
219208

220209
beforeEach(() => {
221210
localStorage.clear();
222-
delete (window as any).location;
223-
(window as any).location = {
224-
...originalLocation,
225-
href: "http://localhost:6274",
226-
};
211+
mockWindowLocation("http://localhost:6274");
227212
});
228213

229214
afterEach(() => {
230-
(window as any).location = originalLocation;
215+
mockWindowLocation(originalLocation.href);
231216
});
232217

233218
test("returns serverArgs from query parameter", () => {
234-
(window as any).location.href =
235-
"http://localhost:6274/?serverArgs=--debug";
219+
mockWindowLocation("http://localhost:6274/?serverArgs=--debug");
236220
expect(getInitialArgs()).toBe("--debug");
237221
});
238222

@@ -247,8 +231,7 @@ describe("configUtils", () => {
247231

248232
test("handles encoded args in query parameters", () => {
249233
const encodedArgs = encodeURIComponent("--port 3000 --host 0.0.0.0");
250-
(window as any).location.href =
251-
`http://localhost:6274/?serverArgs=${encodedArgs}`;
234+
mockWindowLocation(`http://localhost:6274/?serverArgs=${encodedArgs}`);
252235
expect(getInitialArgs()).toBe("--port 3000 --host 0.0.0.0");
253236
});
254237
});

0 commit comments

Comments
 (0)