From 540ce747213d8e2f8d62238d33e767e13ffa23bc Mon Sep 17 00:00:00 2001 From: spaenleh Date: Thu, 29 Jun 2023 10:56:34 +0200 Subject: [PATCH 1/2] fix: add mock websockets client --- src/index.ts | 1 + src/ws/index.ts | 1 + src/ws/mock-ws-client.ts | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/ws/mock-ws-client.ts diff --git a/src/index.ts b/src/index.ts index 4d9b4969..dd4cc7ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,3 +5,4 @@ export { DATA_KEYS } from './config/keys'; export { API_ROUTES } from './api/routes'; export * from './types'; +export { MockWebSocket } from './ws'; diff --git a/src/ws/index.ts b/src/ws/index.ts index 180ca5c2..e5b241fa 100644 --- a/src/ws/index.ts +++ b/src/ws/index.ts @@ -4,3 +4,4 @@ */ export * from './hooks'; export { configureWebsocketClient } from './ws-client'; +export { default as MockWebSocket } from './mock-ws-client'; diff --git a/src/ws/mock-ws-client.ts b/src/ws/mock-ws-client.ts new file mode 100644 index 00000000..895b00af --- /dev/null +++ b/src/ws/mock-ws-client.ts @@ -0,0 +1,59 @@ +/** + * Mock WebSocket class for Graasp WS protocol + */ + +export default class MockWebSocket { + CLOSED: number; + + OPEN: number; + + readyState: number; + + onopen?: () => void; + + onmessage?: (msg: unknown) => void; + + constructor() { + this.CLOSED = 0; + this.OPEN = 1; + + this.readyState = this.OPEN; + + this.send = this.send.bind(this); + this.receive = this.receive.bind(this); + this.addEventListener = this.addEventListener.bind(this); + } + + send(msg: string) { + const req = JSON.parse(msg); + // acknowledge request + if (req.action.includes('subscribe')) { + const res = { + data: JSON.stringify({ + realm: 'notif', + type: 'response', + status: 'success', + request: req, + }), + }; + this.onmessage?.(res); + } + } + + receive(msg: object) { + const event = { + data: JSON.stringify(msg), + }; + this.onmessage?.(event); + } + + addEventListener( + event: 'message' | 'open', + handler: (message?: unknown) => void, + ) { + this[`on${event}`] = handler; + if (event === 'open') { + handler(); + } + } +} From dfb9687afb4f9297077830ff17b00e14ec8ef539 Mon Sep 17 00:00:00 2001 From: spaenleh Date: Thu, 29 Jun 2023 15:10:56 +0200 Subject: [PATCH 2/2] fix: add jest cache for speed --- .gitignore | 1 + jest.config.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index bd2eb276..dfd259af 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ yarn-error.log* # cache .eslintcache +.jest-cache # testing and coverage cypress/screenshots diff --git a/jest.config.ts b/jest.config.ts index e52fd4d1..30c1d5a1 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -11,7 +11,7 @@ export default { // bail: 0, // The directory where Jest should store its cached dependency information - // cacheDirectory: "/private/var/folders/8n/0bjjpkw94_97_lywdtll_d0r0000gn/T/jest_dx", + cacheDirectory: '.jest-cache', // Automatically clear mock calls and instances between every test // clearMocks: false,