| 
1 | 1 | # data-mocks  | 
2 | 2 | 
 
  | 
3 | 3 | # modified by aiden for websocket support.  | 
 | 4 | + | 
4 | 5 | # will hopefully be deleted soon when merged in to upstream  | 
5 | 6 | 
 
  | 
6 | 7 | [](https://github.com/grug/data-mocks)  | 
7 | 8 |   | 
8 | 9 | 
 
  | 
9 | 10 | <img src="https://i.imgur.com/gEG3io2.jpg" height="250">  | 
10 | 11 | 
 
  | 
11 |  | -Library (written in TypeScript) to mock REST and GraphQL requests  | 
 | 12 | +Library (written in TypeScript) to mock REST, GraphQ, and Websocket requests  | 
12 | 13 | 
 
  | 
13 | 14 | <!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} -->  | 
14 | 15 | 
 
  | 
@@ -96,13 +97,13 @@ import { AppModule } from './app/app.module';  | 
96 | 97 | import { environment } from './environments/environment';  | 
97 | 98 | 
 
  | 
98 | 99 | 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');  | 
104 | 105 | 
 
  | 
105 |  | -    injectMocks(getMocks(), extractScenarioFromLocation(window.location));  | 
 | 106 | +  injectMocks(getMocks(), extractScenarioFromLocation(window.location));  | 
106 | 107 | }  | 
107 | 108 | 
 
  | 
108 | 109 | async function main() {  | 
@@ -306,6 +307,38 @@ const Component = () => {  | 
306 | 307 | };  | 
307 | 308 | ```  | 
308 | 309 | 
 
  | 
 | 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 | + | 
309 | 342 | ## Exported types  | 
310 | 343 | 
 
  | 
311 | 344 | ### Scenarios  | 
@@ -334,6 +367,14 @@ const Component = () => {  | 
334 | 367 | | method     | string             | ✅       | Must be 'GRAPHQL' to specify that this is a GraphQL mock. |  | 
335 | 368 | | operations | Array\<Operation\> | ✅       | Array of GraphQL operations for this request.             |  | 
336 | 369 | 
 
  | 
 | 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 | + | 
337 | 378 | ### Mock  | 
338 | 379 | 
 
  | 
339 | 380 | Union type of [`HttpMock`](#HttpMock) and [`GraphQLMock`](#GraphQLMock).  | 
 | 
0 commit comments