Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add mock websockets client #321

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ yarn-error.log*

# cache
.eslintcache
.jest-cache

# testing and coverage
cypress/screenshots
Expand Down
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
spaenleh marked this conversation as resolved.
Show resolved Hide resolved

// Automatically clear mock calls and instances between every test
// clearMocks: false,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { DATA_KEYS } from './config/keys';
export { API_ROUTES } from './api/routes';

export * from './types';
export { MockWebSocket } from './ws';
spaenleh marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/
export * from './hooks';
export { configureWebsocketClient } from './ws-client';
export { default as MockWebSocket } from './mock-ws-client';
spaenleh marked this conversation as resolved.
Show resolved Hide resolved
59 changes: 59 additions & 0 deletions src/ws/mock-ws-client.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
}