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 code example #393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,35 @@ import { WebSocket, Server } from 'mock-socket';
```js
import test from 'ava';
import { Server } from 'mock-socket';
import {AsyncQueue} from '@borewit/async-queue';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep the readme simple and avoid using 3rd party libs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fully understand that. I tried to embed an inline solution, yet that started to a bit lengthy and distracting, hence I moved this portion to borewit/async-queue.

I fixed the code example in 3 different area's. I leave it as is.


class ChatApp {

constructor(url) {
this.messages = [];
this.connection = new WebSocket(url);
this.messageQueue = new AsyncQueue();

this.connection.onmessage = event => {
this.messages.push(event.data);
};
this.connection = new WebSocket(url);
this.connection.onmessage = event => this.messageQueue.push(event.data);
}

sendMessage(message) {
this.connection.send(message);
}

static connect(url) {
const chatApp = new ChatApp(url);
return new Promise((resolve, reject) => {
chatApp.connection.addEventListener('open', () => {
resolve(chatApp);
});
chatApp.connection.addEventListener('error', err => {
reject(err);
});
});
}
}

test.cb('that chat app can be mocked', t => {
test('that chat app can be mocked', async t => {
const fakeURL = 'ws://localhost:8080';
const mockServer = new Server(fakeURL);

Expand All @@ -64,15 +76,11 @@ test.cb('that chat app can be mocked', t => {
});
});

const app = new ChatApp(fakeURL);
const app = await ChatApp.connect(fakeURL);
app.sendMessage('test message from app'); // NOTE: this line creates a micro task

// NOTE: this timeout is for creating another micro task that will happen after the above one
setTimeout(() => {
t.is(app.messages.length, 1);
t.is(app.messages[0], 'test message from mock server', 'we have stubbed our websocket backend');
mockServer.stop(t.done);
}, 100);
const message = await app.messageQueue.next();
t.is(message, 'test message from mock server', 'we have stubbed our websocket backend');
});
```

Expand Down