forked from raspiblitz/raspiblitz-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendModal.test.tsx
166 lines (144 loc) · 5.31 KB
/
SendModal.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import userEvent from "@testing-library/user-event";
import { UserEvent } from "@testing-library/user-event/dist/types/setup";
import { render, screen } from "test-utils";
import { rest, server } from "../../../../testServer";
import SendModal, { Props } from "../SendModal";
const handleClose = jest.fn();
const basicProps: Props = {
lnBalance: 0,
onchainBalance: 0,
onClose: handleClose,
};
const setup = () => {
render(<SendModal {...basicProps} />);
};
describe("SendModal", () => {
it("should render with lightning as default", () => {
setup();
const addressInput = screen.getByLabelText("wallet.invoice");
const lnTypeBtn = screen.getByRole("button", {
name: "home.lightning",
});
const onChainBtn = screen.getByRole("button", {
name: "wallet.on_chain",
});
expect(addressInput).toBeInTheDocument();
expect(lnTypeBtn).toBeDisabled();
expect(onChainBtn).not.toBeDisabled();
});
it("should close on click of X button", async () => {
const user = userEvent.setup();
setup();
const closeBtn = screen.getByRole("button", { name: "" });
await user.click(closeBtn);
expect(handleClose).toHaveBeenCalled();
});
describe("SendLN", () => {
it("enables the send button on valid input", async () => {
const user = userEvent.setup();
setup();
const sendBtn = screen.getByRole("button", {
name: "wallet.send",
});
const addressInput = screen.getByLabelText("wallet.invoice");
expect(sendBtn).toBeDisabled();
await user.type(addressInput, "lnbc123ewewewewew");
expect(sendBtn).toBeEnabled();
});
it("disables the send button on invalid input", async () => {
const user = userEvent.setup();
setup();
const sendBtn = screen.getByRole("button", {
name: "wallet.send",
});
const addressInput = screen.getByLabelText("wallet.invoice");
await user.type(addressInput, "bla");
expect(addressInput).toHaveClass("input-error");
expect(sendBtn).toBeDisabled();
});
it("should send the decode request correctly", async () => {
server.use(
rest.get("/api/v1/lightning/decode-pay-req", (req, res, ctx) => {
if (req.url.searchParams.get("pay_req")) {
return res(
ctx.status(200),
ctx.json({
destination:
"0323dbd695d801553837f9907100f304abd153932bb000a3a7ea9132ff3e7437a1",
payment_hash:
"dc171b0d9a6c33d40ba2d9ed95819b29af40d83132b15072ab4e8b60feb08b90",
num_satoshis: 20,
timestamp: 1893456000000,
expiry: 36000,
description: "TEST",
description_hash: "",
fallback_addr: "",
cltv_expiry: 40,
route_hints: [],
payment_addr:
"24efc95be534b44b801ea5603b9aa1ad5424196972c7a3357b478e773b55f22e",
num_msat: 20000,
features: [],
})
);
} else {
return res(ctx.status(500));
}
})
);
const user = userEvent.setup();
setup();
const sendBtn = screen.getByRole("button", {
name: "wallet.send",
});
const addressInput = screen.getByLabelText("wallet.invoice");
await user.type(addressInput, "lnbc111");
await user.click(sendBtn);
// Should display confirm modal with amount
expect(await screen.findByText("20 Sat")).toBeInTheDocument();
});
});
describe("SendOnChain", () => {
let user: UserEvent;
// switch to onchain modal
beforeEach(async () => {
user = userEvent.setup();
setup();
const onChainBtn = screen.getByRole("button", {
name: "wallet.on_chain",
});
await user.click(onChainBtn);
});
it("should switch to onChain on SwitchTxType click", async () => {
const addressInput = await screen.findByLabelText("wallet.address");
expect(addressInput).toBeInTheDocument();
});
it("should show error on wrong addrees input", async () => {
const addressInput = await screen.findByLabelText("wallet.address");
await user.type(addressInput, "bccccccc");
expect(addressInput).toHaveClass("input-error");
});
it("should format amount correctly", async () => {
const addressInput = await screen.findByLabelText("wallet.address");
const amountInput = await screen.findByLabelText("wallet.amount");
await user.type(addressInput, "bc1q12345");
await user.type(amountInput, "20123");
expect(amountInput).toHaveValue("20,123");
});
it("test a valid form pass", async () => {
const addressInput = await screen.findByLabelText("wallet.address");
const amountInput = await screen.findByLabelText("wallet.amount");
const feeInput = await screen.findByLabelText("tx.fee");
await user.type(addressInput, "bc1q12345");
await user.type(amountInput, "20123");
await user.type(feeInput, "200");
const confirmBtn = screen.getByRole("button", {
name: "wallet.confirm",
});
expect(confirmBtn).toBeEnabled();
await user.click(confirmBtn);
// Should display confirm modal with amount
expect(await screen.findByText("20123 Sat")).toBeInTheDocument();
});
});
});