-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(measure): UI buttons unresponsive/streamline socket URL handling (#…
…191) * fix(measure): streamline socket URL handling * test(measure): write tests for dynamic socket connection * chore: prevent jest for failing for ts errors Seemingly Jest can fail even though tsc is passing properly Since we catch errors in "yarn tsc" already, we don't need jest to bother us about it anyway I think ts-jest doesn't recognize properly the .d.ts definition basically --------- Co-authored-by: almouro <contact@almouro.com>
- Loading branch information
Showing
11 changed files
with
240 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/commands/measure/src/__tests__/server/ServerApp.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import supertest from "supertest"; | ||
import express from "express"; | ||
import fs from "fs"; | ||
|
||
import { createExpressApp } from "../../server/ServerApp"; | ||
import type { FlashlightData } from "../../common/types"; | ||
|
||
jest.mock("fs", () => ({ | ||
promises: { | ||
readFile: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe("ServerApp", () => { | ||
let app: express.Express; | ||
const injected: FlashlightData = { socketServerUrl: "http://localhost:9999" }; | ||
|
||
beforeAll(() => { | ||
jest.spyOn(express, "static").mockImplementation(() => (req, res, next) => next()); | ||
}); | ||
|
||
beforeEach(() => { | ||
(fs.promises.readFile as jest.Mock).mockResolvedValue("<html>/* %FLASHLIGHT_DATA% */</html>"); | ||
|
||
app = createExpressApp(injected); | ||
}); | ||
|
||
describe("GET /", () => { | ||
it("injects FlashlightData into index.html", async () => { | ||
const response = await supertest(app).get("/"); | ||
|
||
expect(response.statusCode).toBe(200); | ||
expect(response.text).toContain(`window.__FLASHLIGHT_DATA__ = ${JSON.stringify(injected)};`); | ||
}); | ||
}); | ||
|
||
test("index.html contains the FlashlightData placeholder", async () => { | ||
const fsPromises = jest.requireActual("fs").promises; | ||
const fileContent = await fsPromises.readFile(`${__dirname}/../../webapp/index.html`, "utf8"); | ||
expect(fileContent).toContain("/* %FLASHLIGHT_DATA% */"); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
packages/commands/measure/src/__tests__/webapp/socket.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { io } from "socket.io-client"; | ||
|
||
jest.mock("socket.io-client", () => { | ||
return { | ||
...jest.requireActual("socket.io-client"), | ||
io: jest.fn().mockImplementation(() => { | ||
return { | ||
on: jest.fn(), | ||
close: jest.fn(), | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
let originalWindow: Window & typeof globalThis; | ||
|
||
describe("socket", () => { | ||
beforeAll(async () => { | ||
originalWindow = global.window; | ||
|
||
global.window = Object.create(window); | ||
Object.defineProperty(window, "__FLASHLIGHT_DATA__", { | ||
value: { socketServerUrl: "http://localhost:9999" }, | ||
writable: true, | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
// Restore the original window object | ||
global.window = originalWindow; | ||
}); | ||
|
||
it("sets the expected socket server URL", async () => { | ||
await import("../../webapp/socket"); | ||
expect(io).toHaveBeenCalledWith("http://localhost:9999"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface FlashlightData { | ||
socketServerUrl: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { FlashlightData } from "../common/types"; | ||
|
||
declare global { | ||
interface Window { | ||
__FLASHLIGHT_DATA__: FlashlightData; | ||
} | ||
} | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { io, Socket } from "socket.io-client"; | ||
import { ServerToClientEvents, ClientToServerEvents } from "../server/socket/socketInterface"; | ||
|
||
export const socket: Socket<ServerToClientEvents, ClientToServerEvents> = | ||
io("http://localhost:3000/"); | ||
export const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io( | ||
window.__FLASHLIGHT_DATA__.socketServerUrl | ||
); | ||
|
||
socket.on("disconnect", () => socket.close()); |
Oops, something went wrong.