-
Notifications
You must be signed in to change notification settings - Fork 29
/
App.test.tsx
43 lines (35 loc) · 1.18 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
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import WS from "jest-websocket-mock";
import App from "./App";
let ws: WS;
beforeEach(() => {
ws = new WS("ws://localhost:8080");
});
afterEach(() => {
WS.clean();
});
describe("The App component", () => {
it("renders a dot indicating the connection status", async () => {
render(<App />);
expect(screen.getByTitle("disconnected")).toBeInTheDocument();
await ws.connected;
expect(screen.getByTitle("connected")).toBeInTheDocument();
ws.close();
expect(screen.getByTitle("disconnected")).toBeInTheDocument();
});
it("sends and receives messages", async () => {
render(<App />);
await ws.connected;
const input = screen.getByPlaceholderText("type your message here...");
userEvent.type(input, "Hello there");
fireEvent.submit(input);
await expect(ws).toReceiveMessage("Hello there");
expect(screen.getByText("(sent) Hello there")).toBeInTheDocument();
ws.send("[echo] Hello there");
expect(
screen.getByText("(received) [echo] Hello there")
).toBeInTheDocument();
});
});