Skip to content

Commit ee4fc0f

Browse files
Add test to chat example (#160)
* Add test to chat example * Add test to chat example
1 parent a0bf78d commit ee4fc0f

File tree

5 files changed

+1983
-12
lines changed

5 files changed

+1983
-12
lines changed

ts/streaming-chat/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ You can also access Encore's [local developer dashboard](https://encore.dev/docs
3333

3434
In you change the frontend then run `npm run build` to build a new frontend in the `dist` folder.
3535

36+
## Testing
37+
38+
```bash
39+
encore test
40+
```
41+
3642
## Deployment
3743

3844
Deploy your application to a staging environment in Encore's free development cloud:

ts/streaming-chat/chat/chat.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, expect, test } from "vitest";
2+
import { chat } from "~encore/clients";
3+
4+
describe("chat", () => {
5+
test("should get back own message with correct parameters", async () => {
6+
// Connect to the chat server
7+
const stream = await chat.chat({ id: "user-id" });
8+
9+
// Send a message to the server
10+
await stream.send({ username: "foo", msg: "hello" });
11+
12+
// Receive a message from the server
13+
const { msg, userID, username } = await stream.recv();
14+
expect(userID).toBe("user-id");
15+
expect(msg).toBe("hello");
16+
expect(username).toBe("foo");
17+
});
18+
19+
test("should get other users message", async () => {
20+
// Connect clients to the chat server
21+
const stream1 = await chat.chat({ id: "user-1" });
22+
const stream2 = await chat.chat({ id: "user-2" });
23+
const stream3 = await chat.chat({ id: "user-3" });
24+
25+
// Send a message from one client
26+
await stream1.send({ username: "foo", msg: "hello" });
27+
28+
// Receive the message from the other clients
29+
const stream2Results = await stream2.recv();
30+
const stream3Results = await stream3.recv();
31+
32+
expect(stream2Results.userID).toBe("user-1");
33+
expect(stream3Results.userID).toBe("user-1");
34+
});
35+
});

0 commit comments

Comments
 (0)