|
| 1 | +/** |
| 2 | + * Unit tests for the buy-side quantity input enforcing a minimum of 1 and a |
| 3 | + * maximum equal to the creator's available supply (#753). |
| 4 | + */ |
| 5 | +import { describe, expect, it, vi } from 'vitest'; |
| 6 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 7 | +import TradeDialog from '../TradeDialog'; |
| 8 | + |
| 9 | +describe('TradeDialog available-supply quantity validation (#753)', () => { |
| 10 | + function renderBuyDialog( |
| 11 | + overrides: Partial<React.ComponentProps<typeof TradeDialog>> = {} |
| 12 | + ) { |
| 13 | + return render( |
| 14 | + <TradeDialog |
| 15 | + open={true} |
| 16 | + side="buy" |
| 17 | + creatorName="Alex Rivers" |
| 18 | + availableHoldings={0} |
| 19 | + availableSupply={100} |
| 20 | + onOpenChange={vi.fn()} |
| 21 | + onConfirm={vi.fn()} |
| 22 | + {...overrides} |
| 23 | + /> |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + it("shows 'Minimum quantity is 1' when the quantity is 0", () => { |
| 28 | + renderBuyDialog(); |
| 29 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 30 | + fireEvent.change(input, { target: { value: '0' } }); |
| 31 | + |
| 32 | + expect(screen.getByTestId('trade-dialog-amount-error')).toHaveTextContent( |
| 33 | + 'Minimum quantity is 1' |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("shows 'Minimum quantity is 1' when the quantity is negative", () => { |
| 38 | + renderBuyDialog(); |
| 39 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 40 | + fireEvent.change(input, { target: { value: '-4' } }); |
| 41 | + |
| 42 | + expect(screen.getByTestId('trade-dialog-amount-error')).toHaveTextContent( |
| 43 | + 'Minimum quantity is 1' |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it("shows 'Exceeds available supply' when the quantity is above the available supply", () => { |
| 48 | + renderBuyDialog({ availableSupply: 50 }); |
| 49 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 50 | + fireEvent.change(input, { target: { value: '51' } }); |
| 51 | + |
| 52 | + expect(screen.getByTestId('trade-dialog-amount-error')).toHaveTextContent( |
| 53 | + 'Exceeds available supply' |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('passes validation when the quantity exactly equals the available supply', () => { |
| 58 | + renderBuyDialog({ availableSupply: 50 }); |
| 59 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 60 | + fireEvent.change(input, { target: { value: '50' } }); |
| 61 | + |
| 62 | + expect( |
| 63 | + screen.queryByTestId('trade-dialog-amount-error') |
| 64 | + ).not.toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('shows no error for a valid quantity within range', () => { |
| 68 | + renderBuyDialog({ availableSupply: 50 }); |
| 69 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 70 | + fireEvent.change(input, { target: { value: '10' } }); |
| 71 | + |
| 72 | + expect( |
| 73 | + screen.queryByTestId('trade-dialog-amount-error') |
| 74 | + ).not.toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('disables the confirm button while the available-supply error is active', () => { |
| 78 | + renderBuyDialog({ availableSupply: 50 }); |
| 79 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 80 | + fireEvent.change(input, { target: { value: '51' } }); |
| 81 | + |
| 82 | + expect(screen.getByTestId('trade-dialog-confirm')).toBeDisabled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('disables the confirm button while the minimum-quantity error is active', () => { |
| 86 | + renderBuyDialog(); |
| 87 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 88 | + fireEvent.change(input, { target: { value: '0' } }); |
| 89 | + |
| 90 | + expect(screen.getByTestId('trade-dialog-confirm')).toBeDisabled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('does not apply the available-supply ceiling when availableSupply is null (unlimited)', () => { |
| 94 | + renderBuyDialog({ availableSupply: null }); |
| 95 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 96 | + fireEvent.change(input, { target: { value: '100000' } }); |
| 97 | + |
| 98 | + expect( |
| 99 | + screen.queryByTestId('trade-dialog-amount-error') |
| 100 | + ).not.toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('does not apply the minimum-quantity or available-supply rules on the sell side', () => { |
| 104 | + render( |
| 105 | + <TradeDialog |
| 106 | + open={true} |
| 107 | + side="sell" |
| 108 | + creatorName="Alex Rivers" |
| 109 | + availableHoldings={5} |
| 110 | + availableSupply={50} |
| 111 | + onOpenChange={vi.fn()} |
| 112 | + onConfirm={vi.fn()} |
| 113 | + /> |
| 114 | + ); |
| 115 | + const input = screen.getByTestId('trade-dialog-amount'); |
| 116 | + fireEvent.change(input, { target: { value: '0' } }); |
| 117 | + |
| 118 | + // Sell side keeps its own zero-quantity message, not the buy-side one. |
| 119 | + const error = screen.getByTestId('trade-dialog-amount-error'); |
| 120 | + expect(error).toHaveTextContent('Amount must be greater than zero.'); |
| 121 | + expect(error).not.toHaveTextContent('Minimum quantity is 1'); |
| 122 | + }); |
| 123 | +}); |
0 commit comments