forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmountInput.test.tsx
More file actions
436 lines (370 loc) · 11.5 KB
/
Copy pathAmountInput.test.tsx
File metadata and controls
436 lines (370 loc) · 11.5 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { AmountInput } from "./AmountInput";
describe("AmountInput", () => {
const creditLine = {
id: "cl-001",
name: "Business Line of Credit",
limit: 50000,
available: 35000,
utilization: 30,
riskBand: "Standard" as const,
termMonths: 24,
};
it("connects inline validation messaging to the input with aria-describedby including helper text", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/draw amount/i);
const describedBy = input.getAttribute("aria-describedby");
expect(describedBy).toContain("draw-amount-helper");
});
it("adds error message ID to aria-describedby when amount exceeds availability", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/draw amount/i) as HTMLInputElement;
fireEvent.change(input, {
target: { value: "36000" },
});
// Should have both helper and error in aria-describedby
const describedBy = input.getAttribute("aria-describedby");
expect(describedBy).toContain("draw-amount-helper");
expect(describedBy).toContain("draw-amount-error");
// aria-invalid should be true when there's an error
expect(input).toHaveAttribute("aria-invalid", "true");
});
it("shows an inline error and keeps continue disabled when the amount exceeds availability", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
fireEvent.change(screen.getByLabelText(/draw amount/i), {
target: { value: "36000" },
});
expect(screen.getByText("Exceeds available credit")).toBeInTheDocument();
expect(screen.getByRole("button", { name: /continue/i })).toBeDisabled();
});
it("displays validation error with danger type when amount is below minimum", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
fireEvent.change(screen.getByLabelText(/draw amount/i), {
target: { value: "0.50" },
});
expect(screen.getByText("Minimum amount required")).toBeInTheDocument();
expect(screen.getByRole("button", { name: /continue/i })).toBeDisabled();
});
it("renders Max button with accessible label", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const maxButton = screen.getByRole("button", {
name: /set amount to maximum/i,
});
expect(maxButton).toBeInTheDocument();
});
it("sets amount to maximum available when Max button is clicked", () => {
const onAmountChange = vi.fn();
render(
<AmountInput
creditLine={creditLine}
onAmountChange={onAmountChange}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const maxButton = screen.getByRole("button", {
name: /set amount to maximum/i,
});
fireEvent.click(maxButton);
const input = screen.getByLabelText(/draw amount/i) as HTMLInputElement;
expect(input.value).toBe(creditLine.available.toString());
});
it("shows success message when valid amount is entered", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
fireEvent.change(screen.getByLabelText(/draw amount/i), {
target: { value: "10000" },
});
expect(screen.getByText("Draw amount looks good")).toBeInTheDocument();
expect(
screen.getByRole("button", { name: /continue/i }),
).not.toBeDisabled();
});
it("enables continue button only when amount is valid", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const continueButton = screen.getByRole("button", { name: /continue/i });
expect(continueButton).toBeDisabled();
fireEvent.change(screen.getByLabelText(/draw amount/i), {
target: { value: "15000" },
});
expect(continueButton).not.toBeDisabled();
});
it("sanitizes pasted currency strings (strips $, commas, whitespace)", async () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/draw amount/i) as HTMLInputElement;
// Mock clipboard event
const pasteEvent = new ClipboardEvent("paste", {
clipboardData: {
getData: (type: string) => {
if (type === "text") return "$1,500.00";
return "";
},
},
});
fireEvent.paste(input, pasteEvent);
expect(input.value).toBe("1500.00");
expect(screen.getByText("Pasted value sanitized to $1,500.00")).toBeInTheDocument();
});
it("rejects non-numeric pasted text and announces the error", async () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/draw amount/i) as HTMLInputElement;
const pasteEvent = new ClipboardEvent("paste", {
clipboardData: {
getData: (type: string) => {
if (type === "text") return "invalid-amount";
return "";
},
},
});
fireEvent.paste(input, pasteEvent);
expect(input.value).not.toBe("invalid-amount");
expect(screen.getByText("Invalid amount pasted. Please enter a numeric value.")).toBeInTheDocument();
});
});
it("renders decrease stepper button with accessible label", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const decButton = screen.getByRole("button", {
name: /decrease amount/i,
});
expect(decButton).toBeInTheDocument();
expect(decButton).toBeDisabled(); // initially 0, can't go below 0
});
it("renders increase stepper button with accessible label", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const incButton = screen.getByRole("button", {
name: /increase amount/i,
});
expect(incButton).toBeInTheDocument();
expect(incButton).not.toBeDisabled();
});
it("increments amount by step when increase button is clicked", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const incButton = screen.getByRole("button", {
name: /increase amount/i,
});
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
fireEvent.click(incButton);
expect(input.value).toBe("100");
});
it("decrements amount by step when decrease button is clicked", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
fireEvent.change(input, { target: { value: "500" } });
const decButton = screen.getByRole("button", {
name: /decrease amount/i,
});
fireEvent.click(decButton);
expect(input.value).toBe("400");
});
it("disables decrease button when amount is 0", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const decButton = screen.getByRole("button", {
name: /decrease amount/i,
});
expect(decButton).toBeDisabled();
});
it("disables increase button when amount equals available credit", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
fireEvent.change(input, { target: { value: creditLine.available.toString() } });
const incButton = screen.getByRole("button", {
name: /increase amount/i,
});
expect(incButton).toBeDisabled();
});
it("does not increment amount beyond available credit", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
fireEvent.change(input, { target: { value: "34900" } });
const incButton = screen.getByRole("button", {
name: /increase amount/i,
});
fireEvent.click(incButton);
expect(input.value).toBe(creditLine.available.toString());
});
it("does not decrement amount below 0", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
// A small amount less than STEP_AMOUNT should floor to 0
fireEvent.change(input, { target: { value: "50" } });
const decButton = screen.getByRole("button", {
name: /decrease amount/i,
});
fireEvent.click(decButton);
expect(input.value).toBe("0");
});
it("responds to ArrowUp key to increment amount", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
fireEvent.change(input, { target: { value: "500" } });
fireEvent.keyDown(input, { key: "ArrowUp" });
expect(input.value).toBe("600");
});
it("responds to ArrowDown key to decrement amount", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
fireEvent.change(input, { target: { value: "500" } });
fireEvent.keyDown(input, { key: "ArrowDown" });
expect(input.value).toBe("400");
});
it("sets input step attribute", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/amount to draw/i) as HTMLInputElement;
expect(input).toHaveAttribute("step", "100");
});
it("sets input to invalid state with proper styling when error occurs", () => {
render(
<AmountInput
creditLine={creditLine}
onAmountChange={vi.fn()}
onNext={vi.fn()}
onBack={vi.fn()}
/>,
);
const input = screen.getByLabelText(/draw amount/i) as HTMLInputElement;
fireEvent.change(input, {
target: { value: "50000" },
});
expect(input).toHaveAttribute("aria-invalid", "true");
});
});