forked from romgain/jest-websocket-mock
/
App.test.tsx
53 lines (40 loc) · 1.46 KB
/
App.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @copyright Romain Bertrand 2018
* @copyright Akiomi Kamakura 2023
*/
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { WS } from 'vitest-websocket-mock';
import App from './App';
let ws: WS;
beforeEach(async () => {
ws = new WS('ws://localhost:8080');
});
afterEach(() => {
WS.clean();
document.getElementsByTagName('body')[0].innerHTML = ''; // Clear jsdom rendering
});
describe('The App component', () => {
it('renders a dot indicating the connection status', async () => {
render(<App />);
expect(screen.getByTitle('disconnected')).toBeInTheDocument();
await ws.connected;
expect(await screen.findByTitle('connected')).toBeInTheDocument();
ws.close();
await ws.closed;
expect(await screen.findByTitle('disconnected')).toBeInTheDocument();
});
it('sends and receives messages', async () => {
const user = userEvent.setup();
render(<App />);
await ws.connected;
const input = screen.getByPlaceholderText('type your message here...');
await user.type(input, 'Hello there');
await user.keyboard('[Enter]');
await expect(ws).toReceiveMessage('Hello there');
expect(await screen.findByText('(sent) Hello there')).toBeInTheDocument();
ws.send('[echo] Hello there');
expect(await screen.findByText('(received) [echo] Hello there')).toBeInTheDocument();
});
});