🐛 Fix mocking websockets when running multiple tests

This commit is contained in:
Belén Albeza 2024-05-14 15:13:11 +02:00
parent 575873eba7
commit d43458ee89
5 changed files with 51 additions and 36 deletions

View file

@ -2,17 +2,19 @@ export class MockWebSocketHelper extends EventTarget {
static #mocks = new Map();
static async init(page) {
await page.exposeFunction("MockWebSocket$$constructor", (url, protocols) => {
const webSocket = new MockWebSocketHelper(page, url, protocols);
this.#mocks = new Map();
await page.exposeFunction("onMockWebSocketConstructor", (url) => {
const webSocket = new MockWebSocketHelper(page, url);
this.#mocks.set(url, webSocket);
});
await page.exposeFunction("MockWebSocket$$spyMessage", (url, data) => {
await page.exposeFunction("onMockWebSocketSpyMessage", (url, data) => {
if (!this.#mocks.has(url)) {
throw new Error(`WebSocket with URL ${url} not found`);
}
this.#mocks.get(url).dispatchEvent(new MessageEvent("message", { data }));
});
await page.exposeFunction("MockWebSocket$$spyClose", (url, code, reason) => {
await page.exposeFunction("onMockWebSocketSpyClose", (url, code, reason) => {
if (!this.#mocks.has(url)) {
throw new Error(`WebSocket with URL ${url} not found`);
}
@ -36,39 +38,52 @@ export class MockWebSocketHelper extends EventTarget {
#page = null;
#url;
#protocols;
constructor(page, url, protocols) {
super();
this.#page = page;
this.#url = url;
this.#protocols = protocols;
}
mockOpen(options) {
return this.#page.evaluate(({ url, options }) => {
if (typeof WebSocket.getByURL !== 'function') {
throw new Error('WebSocket.getByURL is not a function. Did you forget to call MockWebSocket.init(page)?')
}
WebSocket.getByURL(url).mockOpen(options);
}, { url: this.#url, options });
return this.#page.evaluate(
({ url, options }) => {
if (typeof WebSocket.getByURL !== "function") {
throw new Error(
"WebSocket.getByURL is not a function. Did you forget to call MockWebSocket.init(page)?",
);
}
WebSocket.getByURL(url).mockOpen(options);
},
{ url: this.#url, options },
);
}
mockMessage(data) {
return this.#page.evaluate(({ url, data }) => {
if (typeof WebSocket.getByURL !== 'function') {
throw new Error('WebSocket.getByURL is not a function. Did you forget to call MockWebSocket.init(page)?')
}
WebSocket.getByURL(url).mockMessage(data);
}, { url: this.#url, data });
return this.#page.evaluate(
({ url, data }) => {
if (typeof WebSocket.getByURL !== "function") {
throw new Error(
"WebSocket.getByURL is not a function. Did you forget to call MockWebSocket.init(page)?",
);
}
WebSocket.getByURL(url).mockMessage(data);
},
{ url: this.#url, data },
);
}
mockClose() {
return this.#page.evaluate(({ url }) => {
if (typeof WebSocket.getByURL !== 'function') {
throw new Error('WebSocket.getByURL is not a function. Did you forget to call MockWebSocket.init(page)?')
}
WebSocket.getByURL(url).mockClose();
}, { url: this.#url });
return this.#page.evaluate(
({ url }) => {
if (typeof WebSocket.getByURL !== "function") {
throw new Error(
"WebSocket.getByURL is not a function. Did you forget to call MockWebSocket.init(page)?",
);
}
WebSocket.getByURL(url).mockClose();
},
{ url: this.#url },
);
}
}