Skip to content

Commit 023e5ed

Browse files
authored
fix: add test for redirect error (#273)
1 parent 0b76d37 commit 023e5ed

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Telegram, Utils } from "@rx-lab/testing";
2+
3+
const {
4+
PORT,
5+
Api,
6+
initialize,
7+
TestingEnvironment,
8+
MessageType,
9+
DEFAULT_RENDERING_WAIT_TIME,
10+
} = Telegram;
11+
const { sleep } = Utils;
12+
13+
const chatroomId = 2104;
14+
15+
describe("error page with redirect", () => {
16+
let api: Telegram.Api<any>;
17+
let coreApi: any;
18+
19+
beforeAll(async () => {
20+
api = new Api({
21+
baseUrl: `http://0.0.0.0:${PORT}`,
22+
});
23+
});
24+
25+
it("should render the error page using the default one", async () => {
26+
const { core } = await initialize({
27+
filename: import.meta.url,
28+
environment: TestingEnvironment.LongPolling,
29+
api,
30+
chatroomId,
31+
});
32+
coreApi = core;
33+
34+
await api.chatroom.sendMessageToChatroom(chatroomId, {
35+
content: "Hello",
36+
type: MessageType.Text,
37+
});
38+
await sleep(DEFAULT_RENDERING_WAIT_TIME);
39+
let messages = await api.chatroom.getMessagesByChatroom(chatroomId);
40+
expect(messages.data.count).toBe(2);
41+
let currentMessage = messages.data.messages[1];
42+
expect(currentMessage!.text).toContain("Home");
43+
44+
// click on the error button
45+
await api.chatroom.clickOnMessageInChatroom(
46+
chatroomId,
47+
currentMessage!.message_id,
48+
{
49+
text: "Go to error",
50+
},
51+
);
52+
await sleep(DEFAULT_RENDERING_WAIT_TIME);
53+
messages = await api.chatroom.getMessagesByChatroom(chatroomId);
54+
currentMessage = messages.data.messages[1];
55+
expect(currentMessage!.text).toContain("Enter something");
56+
57+
// enter a text
58+
await api.chatroom.sendMessageToChatroom(chatroomId, {
59+
content: "error",
60+
type: MessageType.Text,
61+
});
62+
await sleep(DEFAULT_RENDERING_WAIT_TIME);
63+
messages = await api.chatroom.getMessagesByChatroom(chatroomId);
64+
currentMessage = messages.data.messages[3];
65+
expect(currentMessage!.text).toContain("500");
66+
67+
// enter a text again
68+
await api.chatroom.sendMessageToChatroom(chatroomId, {
69+
content: "hi",
70+
type: MessageType.Text,
71+
});
72+
73+
await sleep(DEFAULT_RENDERING_WAIT_TIME);
74+
messages = await api.chatroom.getMessagesByChatroom(chatroomId);
75+
currentMessage = messages.data.messages[5];
76+
expect(currentMessage!.text).toContain("Home");
77+
});
78+
79+
afterEach(async () => {
80+
await coreApi?.onDestroy();
81+
});
82+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use client";
2+
3+
import { ErrorPageProps } from "@rx-lab/common";
4+
import { useRouter } from "@rx-lab/router/hooks";
5+
import { useEffect } from "react";
6+
7+
export default function ErrorPage({ error, code }: ErrorPageProps) {
8+
const router = useRouter();
9+
useEffect(() => {
10+
router.redirectTo("/", {
11+
renderNewMessage: false,
12+
shouldRender: false,
13+
shouldAddToHistory: true,
14+
});
15+
}, []);
16+
17+
return (
18+
<div>
19+
<h1>{code}</h1>
20+
<p>Custom: {error.message}</p>
21+
</div>
22+
);
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PageProps } from "@rx-lab/common";
2+
3+
export default function Page({ text }: PageProps) {
4+
if (!text) {
5+
return <p>Enter something</p>;
6+
}
7+
8+
if (text === "error") {
9+
throw new Error("🔴 Custom error message");
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { CommandButton } from "@rx-lab/core";
2+
3+
export default function Page() {
4+
return (
5+
<div>
6+
<span>Home</span>
7+
<div>
8+
<CommandButton command={"/error"}>Go to error</CommandButton>
9+
</div>
10+
</div>
11+
);
12+
}

0 commit comments

Comments
 (0)