-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathCollateralSubstitutionModal.test.tsx
More file actions
228 lines (180 loc) · 8.83 KB
/
Copy pathCollateralSubstitutionModal.test.tsx
File metadata and controls
228 lines (180 loc) · 8.83 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* CollateralSubstitutionModal — button order tests
*
* Verifies that every step of the 3-step collateral substitution flow
* renders action buttons in the order mandated by docs/BUTTON_ORDER.md:
*
* Cancel (exit flow) — Back (previous step) — Primary (forward / confirm)
*
* The DOM position of a button determines its visual position in the footer,
* so these tests query all footer buttons by role and assert relative indices.
*/
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { CollateralSubstitutionModal } from './CollateralSubstitutionModal';
// Suppress hooks that interact with the DOM environment
vi.mock('../hooks/useBodyScrollLock', () => ({ useBodyScrollLock: vi.fn() }));
vi.mock('../hooks/useInertBackdrop', () => ({ useInertBackdrop: vi.fn() }));
/** Returns all visible action buttons in the modal footer, excluding the ✕ close button. */
function getFooterButtons() {
return screen
.getAllByRole('button')
.filter(
(btn) =>
!btn.getAttribute('aria-label')?.toLowerCase().includes('close'),
);
}
/** Returns the index of a button whose visible text matches `label`. */
function indexOfButton(buttons: HTMLElement[], label: string | RegExp): number {
return buttons.findIndex((b) => {
const text = b.textContent?.trim() ?? '';
return typeof label === 'string' ? text === label : label.test(text);
});
}
const defaultProps = {
isOpen: true,
onClose: vi.fn(),
onSuccess: vi.fn(),
creditLineName: 'Test Credit Line',
loanBalance: 10_000,
currentAsset: undefined,
_delayMs: 0, // disable artificial network delay in tests
};
describe('CollateralSubstitutionModal — button order (docs/BUTTON_ORDER.md)', () => {
beforeEach(() => {
vi.clearAllMocks();
});
// ── Step 1: Select ─────────────────────────────────────────────────────────
describe('Step 1 (Select)', () => {
it('renders Cancel before the primary Review button', () => {
render(<CollateralSubstitutionModal {...defaultProps} />);
const buttons = getFooterButtons();
const cancelIdx = indexOfButton(buttons, 'Cancel');
const reviewIdx = indexOfButton(buttons, 'Review');
expect(cancelIdx).toBeGreaterThanOrEqual(0);
expect(reviewIdx).toBeGreaterThanOrEqual(0);
// Cancel must precede the primary action in the DOM
expect(cancelIdx).toBeLessThan(reviewIdx);
});
it('does NOT render a Back button on the first step', () => {
render(<CollateralSubstitutionModal {...defaultProps} />);
const buttons = getFooterButtons();
expect(indexOfButton(buttons, 'Back')).toBe(-1);
});
});
// ── Step 2: Review ─────────────────────────────────────────────────────────
describe('Step 2 (Review)', () => {
async function renderAtReviewStep() {
const user = userEvent.setup();
render(<CollateralSubstitutionModal {...defaultProps} />);
// Select the first available asset option
const assetOption = screen.getAllByRole('option')[0];
await user.click(assetOption);
// Advance to Review step
await user.click(screen.getByRole('button', { name: 'Review' }));
}
it('renders Cancel → Back → Continue in that DOM order', async () => {
await renderAtReviewStep();
const buttons = getFooterButtons();
const cancelIdx = indexOfButton(buttons, 'Cancel');
const backIdx = indexOfButton(buttons, 'Back');
const continueIdx = indexOfButton(buttons, 'Continue');
expect(cancelIdx).toBeGreaterThanOrEqual(0);
expect(backIdx).toBeGreaterThanOrEqual(0);
expect(continueIdx).toBeGreaterThanOrEqual(0);
// Rule: Cancel < Back < Primary
expect(cancelIdx).toBeLessThan(backIdx);
expect(backIdx).toBeLessThan(continueIdx);
});
it('Back returns to the Select step', async () => {
const user = userEvent.setup();
await renderAtReviewStep();
await user.click(screen.getByRole('button', { name: 'Back' }));
// After going back the "Review" button (step 1 primary) is visible again
expect(screen.getByRole('button', { name: 'Review' })).toBeInTheDocument();
});
it('Cancel calls onClose', async () => {
const onClose = vi.fn();
const user = userEvent.setup();
render(<CollateralSubstitutionModal {...defaultProps} onClose={onClose} />);
const assetOption = screen.getAllByRole('option')[0];
await user.click(assetOption);
await user.click(screen.getByRole('button', { name: 'Review' }));
await user.click(screen.getByRole('button', { name: 'Cancel' }));
expect(onClose).toHaveBeenCalledTimes(1);
});
});
// ── Step 3: Confirm ────────────────────────────────────────────────────────
describe('Step 3 (Confirm)', () => {
async function renderAtConfirmStep() {
const user = userEvent.setup();
render(<CollateralSubstitutionModal {...defaultProps} />);
const assetOption = screen.getAllByRole('option')[0];
await user.click(assetOption);
await user.click(screen.getByRole('button', { name: 'Review' }));
await user.click(screen.getByRole('button', { name: 'Continue' }));
}
it('renders Cancel → Back → Confirm substitution in that DOM order', async () => {
await renderAtConfirmStep();
const buttons = getFooterButtons();
const cancelIdx = indexOfButton(buttons, 'Cancel');
const backIdx = indexOfButton(buttons, 'Back');
const confirmIdx = indexOfButton(buttons, /confirm substitution/i);
expect(cancelIdx).toBeGreaterThanOrEqual(0);
expect(backIdx).toBeGreaterThanOrEqual(0);
expect(confirmIdx).toBeGreaterThanOrEqual(0);
// Rule: Cancel < Back < Primary
expect(cancelIdx).toBeLessThan(backIdx);
expect(backIdx).toBeLessThan(confirmIdx);
});
it('"Confirm substitution" is disabled until the asset name is typed', async () => {
await renderAtConfirmStep();
expect(
screen.getByRole('button', { name: /confirm substitution/i }),
).toBeDisabled();
});
it('enables "Confirm substitution" once the asset name is typed correctly', async () => {
const user = userEvent.setup();
await renderAtConfirmStep();
// The input placeholder carries the expected asset name
const input = screen.getByRole('textbox');
const assetName = input.getAttribute('placeholder') ?? '';
await user.type(input, assetName);
expect(
screen.getByRole('button', { name: /confirm substitution/i }),
).not.toBeDisabled();
});
it('Back returns to the Review step from the Confirm step', async () => {
const user = userEvent.setup();
await renderAtConfirmStep();
await user.click(screen.getByRole('button', { name: 'Back' }));
// After going back the "Continue" button (step 2 primary) is visible again
expect(screen.getByRole('button', { name: 'Continue' })).toBeInTheDocument();
});
});
// ── Edge cases ─────────────────────────────────────────────────────────────
describe('edge cases', () => {
it('does not render footer buttons when modal is closed', () => {
render(<CollateralSubstitutionModal {...defaultProps} isOpen={false} />);
expect(screen.queryByRole('button', { name: 'Cancel' })).not.toBeInTheDocument();
});
it('disables Cancel and Back while submission is pending', async () => {
const user = userEvent.setup();
// Use a long delay so the modal stays in pending state during assertion
render(<CollateralSubstitutionModal {...defaultProps} _delayMs={60_000} />);
const assetOption = screen.getAllByRole('option')[0];
await user.click(assetOption);
await user.click(screen.getByRole('button', { name: 'Review' }));
await user.click(screen.getByRole('button', { name: 'Continue' }));
// The input placeholder carries the expected asset name
const input = screen.getByRole('textbox');
const assetName = input.getAttribute('placeholder') ?? '';
await user.type(input, assetName);
await user.click(screen.getByRole('button', { name: /confirm substitution/i }));
// While pending, both secondary buttons must be disabled
expect(screen.getByRole('button', { name: 'Cancel' })).toBeDisabled();
expect(screen.getByRole('button', { name: 'Back' })).toBeDisabled();
});
});
});