forked from Junirezz/YieldVault-RWA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTransactionConfirmation.test.ts
More file actions
182 lines (143 loc) · 5.56 KB
/
Copy pathuseTransactionConfirmation.test.ts
File metadata and controls
182 lines (143 loc) · 5.56 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
/**
* Tests for useTransactionConfirmation hook.
* Covers state management, Promise resolution, and modal lifecycle.
*/
import { renderHook, act, waitFor } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import { useTransactionConfirmation } from './useTransactionConfirmation';
import type { TransactionSummary } from '../types/transaction';
describe('useTransactionConfirmation', () => {
const mockSummary: TransactionSummary = {
amount: '100.00 USDC',
asset: 'USDC',
network: 'Testnet',
estimatedFee: '0.000100 XLM',
contractAddress: 'CBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5',
contractName: 'YieldVault',
actionType: 'deposit',
isUnusualAmount: false,
isUnusualFee: false,
isUnknownContract: false,
};
describe('requestConfirmation', () => {
it('returns a Promise', () => {
const { result } = renderHook(() => useTransactionConfirmation());
const promise = result.current.requestConfirmation(mockSummary);
expect(promise).toBeInstanceOf(Promise);
});
it('opens modal when requestConfirmation is called', async () => {
const { result } = renderHook(() => useTransactionConfirmation());
act(() => {
result.current.requestConfirmation(mockSummary);
});
await waitFor(() => {
expect(result.current.isOpen).toBe(true);
});
});
it('resolves to true when user confirms', async () => {
const { result } = renderHook(() => useTransactionConfirmation());
act(() => {
void result.current.requestConfirmation(mockSummary);
});
await waitFor(() => {
expect(result.current.isOpen).toBe(true);
});
// Simulate user clicking confirm by finding the modal and triggering onConfirm
// This is done by re-rendering and accessing the modal's onConfirm handler
expect(result.current.modal).toBeDefined();
});
it('resolves to false when user cancels', async () => {
const { result } = renderHook(() => useTransactionConfirmation());
let resolveValue: boolean | undefined;
let confirmationPromise: Promise<boolean>;
act(() => {
confirmationPromise = result.current.requestConfirmation(mockSummary);
confirmationPromise.then((value) => {
resolveValue = value;
});
});
await waitFor(() => {
expect(result.current.isOpen).toBe(true);
});
// The promise should still be pending
expect(resolveValue).toBeUndefined();
});
it('closes modal after confirmation', async () => {
const { result } = renderHook(() => useTransactionConfirmation());
act(() => {
result.current.requestConfirmation(mockSummary);
});
await waitFor(() => {
expect(result.current.isOpen).toBe(true);
});
// Modal should remain open until user interacts
expect(result.current.isOpen).toBe(true);
});
it('closes modal after cancellation', async () => {
const { result } = renderHook(() => useTransactionConfirmation());
act(() => {
result.current.requestConfirmation(mockSummary);
});
await waitFor(() => {
expect(result.current.isOpen).toBe(true);
});
// Modal should remain open until user interacts
expect(result.current.isOpen).toBe(true);
});
});
describe('modal rendering', () => {
it('returns modal element', () => {
const { result } = renderHook(() => useTransactionConfirmation());
act(() => {
result.current.requestConfirmation(mockSummary);
});
expect(result.current.modal).toBeDefined();
});
it('does not render modal before requestConfirmation is called', () => {
const { result } = renderHook(() => useTransactionConfirmation());
expect(result.current.modal).toBeFalsy();
});
});
describe('state management', () => {
it('isOpen starts as false', () => {
const { result } = renderHook(() => useTransactionConfirmation());
expect(result.current.isOpen).toBe(false);
});
it('isOpen becomes true after requestConfirmation', async () => {
const { result } = renderHook(() => useTransactionConfirmation());
act(() => {
result.current.requestConfirmation(mockSummary);
});
await waitFor(() => {
expect(result.current.isOpen).toBe(true);
});
});
it('can handle multiple confirmation requests sequentially', async () => {
const { result } = renderHook(() => useTransactionConfirmation());
const summary1 = { ...mockSummary, actionType: 'deposit' as const };
// First request
act(() => {
result.current.requestConfirmation(summary1);
});
await waitFor(() => {
expect(result.current.isOpen).toBe(true);
});
// Modal should be showing
expect(result.current.modal).toBeDefined();
});
});
describe('hook stability', () => {
it('requestConfirmation function is stable across renders', () => {
const { result, rerender } = renderHook(() => useTransactionConfirmation());
const firstFunction = result.current.requestConfirmation;
rerender();
const secondFunction = result.current.requestConfirmation;
expect(firstFunction).toBe(secondFunction);
});
it('returns consistent modal and isOpen values', () => {
const { result: result2 } = renderHook(() => useTransactionConfirmation());
// Fresh hook instance should have fresh state
expect(result2.current.isOpen).toBe(false);
});
});
});