Skip to content

Commit

Permalink
fixes redirectmap (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem authored Jul 13, 2024
1 parent 0ea6431 commit f16abfb
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 105 deletions.
212 changes: 108 additions & 104 deletions playwright-tests/tests/redirectmap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,150 +46,154 @@ describe("session-storage", () => {
);
});
});
});

describe("hot-reload", () => {
test.beforeEach(async ({ page }) => {
await page.goto("/");
});

test("should trigger api request to */socket.io/* if hot reload is enabled", async ({
page,
}) => {
let websocketCount = 0;

await page.route("**/socket.io/*", (route) => {
websocketCount++;
route.continue();
});
describe("hot-reload", () => {
test.beforeEach(async ({ page }) => {
await page.goto("/");
});

await page.evaluate(() => {
document.body.innerHTML = `<near-social-viewer src="neardevs.testnet/widget/default" config='{"dev": { "hotreload": { "enabled": true } } }'></near-social-viewer>`;
});
test("should trigger api request to */socket.io/* if hot reload is enabled", async ({
page,
}) => {
let websocketCount = 0;

await waitForSelectorToBeVisible(page, "near-social-viewer");
await page.route("**/socket.io/*", (route) => {
websocketCount++;
route.continue();
});

expect(websocketCount).toBeGreaterThan(0);
await page.evaluate(() => {
document.body.innerHTML = `<near-social-viewer src="neardevs.testnet/widget/default" config='{"dev": { "hotreload": { "enabled": true } } }'></near-social-viewer>`;
});

test("should not trigger api request to */socket.io/* if hot reload is not enabled", async ({
page,
}) => {
let websocketCount = 0;
await waitForSelectorToBeVisible(page, "near-social-viewer");

await page.route("**/socket.io/*", (route) => {
websocketCount++;
route.continue();
});
expect(websocketCount).toBeGreaterThan(0);
});

await page.evaluate(() => {
document.body.innerHTML = `<near-social-viewer src="neardevs.testnet/widget/default"></near-social-viewer>`;
});
test("should not trigger api request to */socket.io/* if hot reload is not enabled", async ({
page,
}) => {
let websocketCount = 0;

await waitForSelectorToBeVisible(page, "near-social-viewer");
await page.route("**/socket.io/*", (route) => {
websocketCount++;
route.continue();
});

expect(websocketCount).toEqual(0);
await page.evaluate(() => {
document.body.innerHTML = `<near-social-viewer src="neardevs.testnet/widget/default"></near-social-viewer>`;
});

describe("with running socket server", () => {
let io, httpServer;
const PORT = 3001;
let HOST = "localhost";
await waitForSelectorToBeVisible(page, "near-social-viewer");

test.beforeAll(async () => {
httpServer = http.createServer();
expect(websocketCount).toEqual(0);
});

io = new Server(httpServer, {
cors: {
origin: `http://${HOST}:3000`,
methods: ["GET", "POST"],
},
});
describe("with running socket server", () => {
let io, httpServer;
const PORT = 3001;
let HOST = "localhost";

io.on("connection", () => {
io.emit("fileChange", {
test.beforeAll(async () => {
httpServer = http.createServer();

io = new Server(httpServer, {
cors: {
origin: `http://${HOST}:3000`,
methods: ["GET", "POST"],
},
});

io.on("connection", () => {
io.emit("fileChange", {
components: {
"anybody.near/widget/test": {
code: "return <p>hello world</p>;",
},
});
},
});
});

// wait for socket start
await new Promise((resolve) => {
httpServer.listen(PORT, HOST, () => {
resolve();
});
// wait for socket start
await new Promise((resolve) => {
httpServer.listen(PORT, HOST, () => {
resolve();
});
});
});

test("should show local redirect map and react to changes", async ({
page,
}) => {
// Verify the viewer is visible
await waitForSelectorToBeVisible(page, "near-social-viewer");
test("should show local redirect map and react to changes", async ({
page,
}) => {
// Verify the viewer is visible
await waitForSelectorToBeVisible(page, "near-social-viewer");

await page.evaluate(() => {
const viewer = document.querySelector("near-social-viewer");
viewer.setAttribute("src", "anybody.near/widget/test"); // this code does not exist
});
await page.evaluate(() => {
const viewer = document.querySelector("near-social-viewer");
viewer.setAttribute("src", "anybody.near/widget/test"); // this code does not exist
});

await page.waitForSelector(
'div.alert.alert-danger:has-text("is not found")'
);
await page.waitForSelector(
'div.alert.alert-danger:has-text("is not found")'
);

// Verify error
const errMsg = await page.locator(
'div.alert.alert-danger:has-text("is not found")'
);
// Verify error
const errMsg = await page.locator(
'div.alert.alert-danger:has-text("is not found")'
);

expect(await errMsg.isVisible()).toBe(true);
expect(await errMsg.isVisible()).toBe(true);

let websocketCount = 0;
let websocketCount = 0;

await page.route("**/socket.io/*", (route) => {
websocketCount++;
route.continue();
});
await page.route("**/socket.io/*", (route) => {
websocketCount++;
route.continue();
});

const config = {
dev: { hotreload: { enabled: true, wss: `ws://${HOST}:${PORT}` } },
};
const config = {
dev: { hotreload: { enabled: true, wss: `ws://${HOST}:${PORT}` } },
};

// Enable hot reload
await page.evaluate(
({ config }) => {
const viewer = document.querySelector("near-social-viewer");
viewer.setAttribute("config", JSON.stringify(config));
},
{ config }
);
// Enable hot reload
await page.evaluate(
({ config }) => {
const viewer = document.querySelector("near-social-viewer");
viewer.setAttribute("config", JSON.stringify(config));
},
{ config }
);

await page.waitForSelector("near-social-viewer");
await page.waitForSelector("near-social-viewer");

// Get the value of the config attribute
const actualConfig = await page.evaluate(() => {
const viewer = document.querySelector("near-social-viewer");
return viewer.getAttribute("config");
});
// Get the value of the config attribute
const actualConfig = await page.evaluate(() => {
const viewer = document.querySelector("near-social-viewer");
return viewer.getAttribute("config");
});

// Assert it is set and equals custom value
expect(actualConfig).toBe(JSON.stringify(config));
// Assert it is set and equals custom value
expect(actualConfig).toBe(JSON.stringify(config));

// Assert web socket was hit
expect(websocketCount).toBeGreaterThan(0);
// Assert web socket was hit
expect(websocketCount).toBeGreaterThan(0);

await expect(await page.getByText("hello world")).toBeVisible();
await expect(await page.getByText("hello world")).toBeVisible();

io.emit("fileChange", {
io.emit("fileChange", {
components: {
"anybody.near/widget/test": { code: "return <p>goodbye world</p>;" },
});

await expect(await page.getByText("goodbye world")).toBeVisible();
},
});

test.afterAll(() => {
io.close();
httpServer.close();
});
await expect(await page.getByText("goodbye world")).toBeVisible();
});

test.afterAll(() => {
io.close();
httpServer.close();
});
});
});
2 changes: 1 addition & 1 deletion src/utils/bos-workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function useRedirectMap() {

socket.on("fileChange", (d) => {
console.log("File change detected via WebSocket", d);
setDevJson(d);
setDevJson(d.components);
});

socket.on("connect_error", (error) => {
Expand Down

0 comments on commit f16abfb

Please sign in to comment.