Skip to content

Commit 8df38c7

Browse files
author
Aiden
committed
readme
1 parent 49c0560 commit 8df38c7

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

README.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# data-mocks
22

33
# modified by aiden for websocket support.
4+
45
# will hopefully be deleted soon when merged in to upstream
56

67
[![GitHub license](https://img.shields.io/github/license/ovotech/data-mocks.svg)](https://github.com/grug/data-mocks)
78
![npm](https://img.shields.io/npm/dm/data-mocks.svg)
89

910
<img src="https://i.imgur.com/gEG3io2.jpg" height="250">
1011

11-
Library (written in TypeScript) to mock REST and GraphQL requests
12+
Library (written in TypeScript) to mock REST, GraphQ, and Websocket requests
1213

1314
<!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} -->
1415

@@ -96,13 +97,13 @@ import { AppModule } from './app/app.module';
9697
import { environment } from './environments/environment';
9798

9899
async function setupMocks() {
99-
const { injectMocks, extractScenarioFromLocation } = await import(
100-
'data-mocks'
101-
);
102-
// You could just define your mocks inline if you didn't want to import them.
103-
const { getMocks } = await import('./path/to/your/mock/definitions');
100+
const { injectMocks, extractScenarioFromLocation } = await import(
101+
'data-mocks'
102+
);
103+
// You could just define your mocks inline if you didn't want to import them.
104+
const { getMocks } = await import('./path/to/your/mock/definitions');
104105

105-
injectMocks(getMocks(), extractScenarioFromLocation(window.location));
106+
injectMocks(getMocks(), extractScenarioFromLocation(window.location));
106107
}
107108

108109
async function main() {
@@ -306,6 +307,38 @@ const Component = () => {
306307
};
307308
```
308309

310+
### Basic Websocket server mock injection
311+
312+
To mock a WebSocket server you should provide a function which takes a single `Server` (provided by `mock-socket`) as a paremeter. On this mock server parameter you can set your callbacks as normal. Please not that due to limitations in the underlying websocket mocking library, the url _must_ be supplied as a string, not a regular expression
313+
314+
```ts
315+
const wsMock: WebSocketServerMock = s => {
316+
return s.on('connection', socket => {
317+
socket.on('message', _ => {
318+
socket.send('hello world')
319+
}
320+
});
321+
});
322+
};
323+
324+
const mocks = {
325+
default: [
326+
{
327+
url: 'ws://localhost' //notice this is NOT a regular expression
328+
method: 'WEBSOCKET',
329+
server: wsMock,
330+
}
331+
]
332+
};
333+
334+
injectMocks(mocks, extractScenarioFromLocation(window.location));
335+
336+
337+
const socket = new WebSocket('ws://localhost')
338+
socket.send('hello')
339+
340+
```
341+
309342
## Exported types
310343

311344
### Scenarios
@@ -334,6 +367,14 @@ const Component = () => {
334367
| method | string || Must be 'GRAPHQL' to specify that this is a GraphQL mock. |
335368
| operations | Array\<Operation\> || Array of GraphQL operations for this request. |
336369

370+
### WebSocketMock
371+
372+
| Property | Type | Required | Description |
373+
| -------- | -------- | -------- | ------------------------------------------------------------------------------------------------------------- |
374+
| url | string || Regular expression that matches part of the URL. |
375+
| method | string || Must be 'WEBSOCKET' to specify that this is a Websocket mock. |
376+
| server | function || a function which takes a server as a parameter. Here you will set up the functionality of the server to mock. |
377+
337378
### Mock
338379

339380
Union type of [`HttpMock`](#HttpMock) and [`GraphQLMock`](#GraphQLMock).

0 commit comments

Comments
 (0)