Skip to content

Commit

Permalink
fix(docs): Add mock server examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Oct 14, 2020
1 parent a1e145c commit 061c8f1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ npm install @relaycorp/ws-mock

## Using the mock client

You should initialise `MockClient` by passing the WebSocket server to be tested and then call `client.connect()` to initiate the connection. From that point you can interact with the server. For example:
You should initialise `MockClient` by passing the `ws` server to be tested and then call `client.connect()` to initiate the connection. From that point you can interact with the server. For example:

```javascript
test('Challenge should be sent as soon as client connects', async () => {
Expand All @@ -23,3 +23,23 @@ You should initialise `MockClient` by passing the WebSocket server to be tested
```

You'll find [real-world examples in relaycorp/relaynet-internet-gateway](https://github.com/relaycorp/relaynet-internet-gateway/search?l=TypeScript&q=%22%40relaycorp%2Fws-mock%22).

## Using the mock server

Simply initialise `MockServer` with the `ws` client connection you wish to test. For example:

```javascript
test('Server message should be played back', async () => {
const mockConnection = new MockConnection();
const mockServer = new MockServer(mockConnection);
const clientUnderTest = new ClientUnderTest(mockConnection);

clientUnderTest.connectToServer();
mockServer.send('foo');

const clientResponse = await mockServer.receive();
expect(clientResponse).toEqual('foo');
});
```

You'll find [real-world examples in relaycorp/relaynet-poweb-js](https://github.com/relaycorp/relaynet-poweb-js/search?l=TypeScript&q=%22%40relaycorp%2Fws-mock%22).

0 comments on commit 061c8f1

Please sign in to comment.