-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathuse-backup-nudge-state.spec.ts
More file actions
213 lines (157 loc) · 6.86 KB
/
Copy pathuse-backup-nudge-state.spec.ts
File metadata and controls
213 lines (157 loc) · 6.86 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { renderHook, act } from "@testing-library/react-native"
import { useBackupNudgeState } from "@app/hooks/use-backup-nudge-state"
const mockBackupState = jest.fn()
const mockActiveWallet = jest.fn()
const mockRemoteConfig = jest.fn()
const mockGetItem = jest.fn()
const mockSetItem = jest.fn()
jest.mock("@app/self-custodial/providers/backup-state-provider", () => ({
BackupStatus: { None: "none", Completed: "completed" },
useBackupState: () => mockBackupState(),
}))
jest.mock("@app/hooks/use-active-wallet", () => ({
useActiveWallet: () => mockActiveWallet(),
}))
jest.mock("@app/config/feature-flags-context", () => ({
useRemoteConfig: () => mockRemoteConfig(),
}))
const SATS_PER_USD_CENT = 10
jest.mock("@app/components/balance-header/use-total-balance", () => ({
useTotalBalance: (wallets: Array<{ walletCurrency: string; balance: number }>) => ({
satsBalance: wallets.reduce((sum, w) => {
if (w.walletCurrency === "BTC") return sum + w.balance
if (w.walletCurrency === "USD") return sum + w.balance * 10
return sum
}, 0),
}),
}))
jest.mock("@react-native-async-storage/async-storage", () => ({
getItem: (...args: string[]) => mockGetItem(...args),
setItem: (...args: string[]) => mockSetItem(...args),
}))
const defaultBackupState = { backupState: { status: "none", method: null } }
const completedBackupState = { backupState: { status: "completed", method: "manual" } }
const selfCustodialWallet = {
accountType: "self-custodial",
wallets: [{ id: "btc-1", walletCurrency: "BTC", balance: { amount: 3000 } }],
}
const custodialWallet = {
accountType: "custodial",
wallets: [{ id: "btc-1", walletCurrency: "BTC", balance: { amount: 50000 } }],
}
const defaultConfig = {
backupNudgeBannerThreshold: 2100,
backupNudgeModalThreshold: 21000,
}
describe("useBackupNudgeState", () => {
beforeEach(() => {
jest.clearAllMocks()
mockBackupState.mockReturnValue(defaultBackupState)
mockActiveWallet.mockReturnValue(selfCustodialWallet)
mockRemoteConfig.mockReturnValue(defaultConfig)
mockGetItem.mockResolvedValue(null)
mockSetItem.mockResolvedValue(undefined)
})
it("hides banner and modal while dismissal-load is pending", () => {
mockGetItem.mockReturnValue(new Promise(() => {}))
const { result } = renderHook(() => useBackupNudgeState())
expect(result.current.shouldShowBanner).toBe(false)
expect(result.current.shouldShowModal).toBe(false)
})
it("shows settings banner without waiting for dismissal-load", () => {
mockGetItem.mockReturnValue(new Promise(() => {}))
const { result } = renderHook(() => useBackupNudgeState())
expect(result.current.shouldShowSettingsBanner).toBe(true)
})
it("shows banner when balance >= banner threshold and not backed up", async () => {
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowBanner).toBe(true)
expect(result.current.shouldShowModal).toBe(false)
})
it("shows modal when balance >= modal threshold", async () => {
mockActiveWallet.mockReturnValue({
accountType: "self-custodial",
wallets: [{ id: "btc-1", walletCurrency: "BTC", balance: { amount: 22000 } }],
})
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowModal).toBe(true)
expect(result.current.shouldShowBanner).toBe(false)
})
it("shows nothing when backed up", async () => {
mockBackupState.mockReturnValue(completedBackupState)
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowBanner).toBe(false)
expect(result.current.shouldShowModal).toBe(false)
expect(result.current.shouldShowSettingsBanner).toBe(false)
})
it("shows nothing for custodial users", async () => {
mockActiveWallet.mockReturnValue(custodialWallet)
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowBanner).toBe(false)
expect(result.current.shouldShowModal).toBe(false)
})
it("shows settings banner for unbacked self-custodial", async () => {
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowSettingsBanner).toBe(true)
})
it("dismisses banner and persists to AsyncStorage", async () => {
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
act(() => {
result.current.dismissBanner()
})
expect(result.current.shouldShowBanner).toBe(false)
expect(mockSetItem).toHaveBeenCalledWith("backupNudgeDismissedAt", expect.any(String))
})
it("loads dismissed state from AsyncStorage", async () => {
mockGetItem.mockResolvedValue(String(Date.now()))
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowBanner).toBe(false)
})
it("shows banner again after 24h cooldown", async () => {
mockGetItem.mockResolvedValue(String(Date.now() - 25 * 60 * 60 * 1000))
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowBanner).toBe(true)
})
it("triggers banner when USD weight pushes combined balance over the threshold", async () => {
const btcAmount = 1_000
const usdCentsAmount = 200
const combinedSats = btcAmount + usdCentsAmount * SATS_PER_USD_CENT
expect(btcAmount).toBeLessThan(defaultConfig.backupNudgeBannerThreshold)
expect(combinedSats).toBeGreaterThanOrEqual(defaultConfig.backupNudgeBannerThreshold)
mockActiveWallet.mockReturnValue({
accountType: "self-custodial",
wallets: [
{ id: "btc-1", walletCurrency: "BTC", balance: { amount: btcAmount } },
{ id: "usd-1", walletCurrency: "USD", balance: { amount: usdCentsAmount } },
],
})
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowBanner).toBe(true)
})
it("triggers modal when USD weight pushes combined balance over the modal threshold", async () => {
const btcAmount = 1_000
const usdCentsAmount = 2_200
const combinedSats = btcAmount + usdCentsAmount * SATS_PER_USD_CENT
expect(btcAmount).toBeLessThan(defaultConfig.backupNudgeModalThreshold)
expect(combinedSats).toBeGreaterThanOrEqual(defaultConfig.backupNudgeModalThreshold)
mockActiveWallet.mockReturnValue({
accountType: "self-custodial",
wallets: [
{ id: "btc-1", walletCurrency: "BTC", balance: { amount: btcAmount } },
{ id: "usd-1", walletCurrency: "USD", balance: { amount: usdCentsAmount } },
],
})
const { result } = renderHook(() => useBackupNudgeState())
await act(async () => {})
expect(result.current.shouldShowModal).toBe(true)
})
})