-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathpayment-adapter.spec.ts
More file actions
185 lines (148 loc) · 5.58 KB
/
Copy pathpayment-adapter.spec.ts
File metadata and controls
185 lines (148 loc) · 5.58 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { PaymentSendResult, WalletCurrency } from "@app/graphql/generated"
import {
ConvertDirection,
FeeQuoteType,
PaymentResultStatus,
} from "@app/types/payment.types"
import {
UnsupportedOperationError,
createCustodialClaimDeposit,
createCustodialConvert,
createCustodialGetFee,
createCustodialListPendingDeposits,
createCustodialReceiveLightning,
createCustodialReceiveOnchain,
createCustodialSendPayment,
} from "@app/custodial/adapters/payment-adapter"
describe("createCustodialSendPayment", () => {
it("maps SUCCESS to success", async () => {
const mutation = jest.fn().mockResolvedValue({
status: PaymentSendResult.Success,
errors: [],
})
const adapter = createCustodialSendPayment(mutation)
const result = await adapter({ destination: "lnbc1..." })
expect(result.status).toBe(PaymentResultStatus.Success)
expect(mutation).toHaveBeenCalledWith({
destination: "lnbc1...",
amount: undefined,
memo: undefined,
})
})
it("maps PENDING to pending", async () => {
const mutation = jest.fn().mockResolvedValue({
status: PaymentSendResult.Pending,
errors: [],
})
const adapter = createCustodialSendPayment(mutation)
const result = await adapter({ destination: "lnbc1..." })
expect(result.status).toBe(PaymentResultStatus.Pending)
})
it("maps FAILURE to failed", async () => {
const mutation = jest.fn().mockResolvedValue({
status: PaymentSendResult.Failure,
errors: [{ message: "Insufficient balance" }],
})
const adapter = createCustodialSendPayment(mutation)
const result = await adapter({ destination: "lnbc1..." })
expect(result.status).toBe(PaymentResultStatus.Failed)
expect(result.errors).toHaveLength(1)
expect(result.errors?.[0].message).toBe("Insufficient balance")
})
it("passes amount and memo to mutation", async () => {
const mutation = jest.fn().mockResolvedValue({
status: PaymentSendResult.Success,
errors: [],
})
const adapter = createCustodialSendPayment(mutation)
await adapter({
destination: "lnbc1...",
amount: { amount: 1000, currency: WalletCurrency.Btc, currencyCode: "BTC" },
memo: "test memo",
})
expect(mutation).toHaveBeenCalledWith({
destination: "lnbc1...",
amount: 1000,
memo: "test memo",
})
})
})
describe("createCustodialGetFee", () => {
it("returns fee quote for lightning", async () => {
const mutation = jest.fn().mockResolvedValue({ amount: 5 })
const adapter = createCustodialGetFee(mutation)
const result = await adapter({ destination: "lnbc1..." })
expect(result).not.toBeNull()
expect(result?.paymentType).toBe(FeeQuoteType.Lightning)
expect(result?.feeAmount.amount).toBe(5)
})
it("returns null when mutation returns null", async () => {
const mutation = jest.fn().mockResolvedValue(null)
const adapter = createCustodialGetFee(mutation)
const result = await adapter({ destination: "lnbc1..." })
expect(result).toBeNull()
})
})
describe("createCustodialReceiveLightning", () => {
it("returns invoice on success", async () => {
const mutation = jest.fn().mockResolvedValue({ invoice: "lnbc1invoice..." })
const adapter = createCustodialReceiveLightning(mutation)
const result = await adapter({})
expect(result.invoice).toBe("lnbc1invoice...")
expect(result.errors).toBeUndefined()
})
it("returns error when mutation returns null", async () => {
const mutation = jest.fn().mockResolvedValue(null)
const adapter = createCustodialReceiveLightning(mutation)
const result = await adapter({})
expect(result.invoice).toBeUndefined()
expect(result.errors).toHaveLength(1)
expect(result.errors?.[0].message).toBe("Failed to create invoice")
})
})
describe("createCustodialReceiveOnchain", () => {
it("returns address on success", async () => {
const mutation = jest.fn().mockResolvedValue({ address: "bc1q..." })
const adapter = createCustodialReceiveOnchain(mutation)
const result = await adapter()
expect(result.address).toBe("bc1q...")
expect(result.errors).toBeUndefined()
})
it("returns error when mutation returns null", async () => {
const mutation = jest.fn().mockResolvedValue(null)
const adapter = createCustodialReceiveOnchain(mutation)
const result = await adapter()
expect(result.address).toBeUndefined()
expect(result.errors).toHaveLength(1)
})
})
describe("createCustodialListPendingDeposits", () => {
it("returns empty deposits", async () => {
const result = await createCustodialListPendingDeposits()
expect(result.deposits).toHaveLength(0)
expect(result.errors).toBeUndefined()
})
})
describe("createCustodialClaimDeposit", () => {
it("getClaimFee throws UnsupportedOperationError", async () => {
await expect(
createCustodialClaimDeposit.getClaimFee({ depositId: "d1" }),
).rejects.toThrow(UnsupportedOperationError)
})
it("claimDeposit throws UnsupportedOperationError", async () => {
await expect(
createCustodialClaimDeposit.claimDeposit({ depositId: "d1" }),
).rejects.toThrow(UnsupportedOperationError)
})
})
describe("createCustodialConvert", () => {
it("returns failed status", async () => {
const result = await createCustodialConvert({
fromAmount: { amount: 1000, currency: WalletCurrency.Btc, currencyCode: "BTC" },
toAmount: { amount: 0, currency: WalletCurrency.Usd, currencyCode: "USD" },
direction: ConvertDirection.BtcToUsd,
})
expect(result.status).toBe(PaymentResultStatus.Failed)
expect(result.errors).toHaveLength(1)
})
})