Skip to content

Feat/add extreme price impact tests - #1031

Merged
k-deejah merged 2 commits into
Sorokit:mainfrom
Ceejaytech25:feat/add-extreme-price-impact-tests
Oct 21, 2026
Merged

k-deejah merged 2 commits into
Sorokit:mainfrom
Ceejaytech25:feat/add-extreme-price-impact-tests

Conversation

@Ceejaytech25

Copy link
Copy Markdown

Add Extreme Price Impact and Custom Reserve Tests in SwapSimulator.test.tsx

Description

This PR adds comprehensive test coverage for Issue #927, implementing three test suites with 20 new test cases to the SwapSimulator component. The implementation includes testing for extreme price impact scenarios, custom pool reserves configuration, and debounced chart recalculations using fake timers.

What Changed

Overview

  • File Modified: src/components/SwapSimulator.test.tsx
  • Lines Added: 413 (500 → 913 total lines)
  • Test Cases Added: 20
  • Test Suites: 3 new describe blocks

Test Suites Implemented

1. Extreme Price Impact Scenarios (5 tests)

Tests for high and critical price impact warnings and color coding:

  • displays high price impact warning banner when impact exceeds 15%

    • Custom poolReserves: 1000 XLM / 100000 USDC
    • Input: 500 XLM
    • Verifies price impact display > 15%
  • shows critical error banner when price impact exceeds 50%

    • Custom poolReserves: 100 XLM / 100000 USDC
    • Input: 10000 XLM
    • Verifies error styling for critical impact
  • shows correct color coding for low price impact (< 1%)

    • Deep pool (1M/1M)
    • Small input (10 XLM)
    • Verifies "Low" badge display
  • shows correct color coding for medium price impact (1-5%)

    • Moderate pool (10K/10K)
    • Medium input (1000 XLM)
    • Verifies "Medium" badge display
  • shows critical red banner when slippage is exceeded with extreme impact

    • Pool (500/50000)
    • Slippage 5%, Input 2000 XLM
    • Verifies error banner with critical styling

2. Custom PoolReserves Props (7 tests)

Tests for custom liquidity pool configuration handling:

  • accepts and uses custom poolReserves configuration

    • Tests with multiple asset pairs
    • Verifies component renders without errors
    • Validates price calculations use custom reserves
  • uses default POOL_RESERVES when custom poolReserves is not provided

    • Confirms fallback behavior
    • Verifies default reserves functionality
  • calculates correct exchange rate with custom poolReserves

    • Custom reserves: 100000/12000 XLM-USDC
    • Validates exchange rate reflects custom values
  • applies liquidity size multiplier correctly to custom poolReserves

    • Tests low (0.1x), medium (1x), and high (10x) buttons
    • Verifies price impact changes appropriately
  • handles empty or missing custom poolReserves gracefully

    • Empty reserve object test
    • Verifies fallback to default 10000 reserves
    • No crashes or errors
  • correctly renders price impact curve with custom poolReserves

    • Custom pool 5000/500
    • SVG chart rendering verification
  • respects asset pair mapping in custom poolReserves

    • Custom pair XLM-EURC (50000/5500)
    • Verifies correct reserves are applied

3. Debounced Chart Calculations with Fake Timers (9 tests)

Tests for debounced chart recalculation using vi.useFakeTimers():

  • debounces price impact chart recalculation on rapid input changes

    • Rapid input changes 100→200→300→400
    • Verifies chart doesn't update until 300ms passes
    • Checks final value reflects last input
  • cancels previous debounce timer when new input is received before debounce completes

    • Tests timer cancellation mechanism
    • Change 1: 1000 (advance 150ms)
    • Change 2: 2000 (advance 150ms → cancel timer 1)
    • Verifies only second input triggers update
  • applies debounce to chart recalculation but not to immediate exchange rate updates

    • Exchange rate updates immediately
    • Chart Max Size remains debounced
    • Demonstrates split behavior
  • recalculates chart correctly after debounce with custom poolReserves

    • Custom pool (100K/12K)
    • Verifies old calculation before debounce
    • New calculation after 300ms
  • uses debounced value for price impact curve SVG rendering

    • Set: 100, advance → uses 100
    • Change: 500, immediate → still uses 100
    • After 300ms → uses 500
  • clears debounce timeout on component unmount

    • Input changed before debounce completes
    • Component unmounted
    • Verifies no memory leaks or errors
  • respects custom AMOUNT_CHART_DEBOUNCE_MS timing

    • Tests at 299ms: no update
    • Tests at 300ms: updates
    • Verifies exact timing precision
  • handles zero and negative input values with debounce

    • Input: 0 → exchange rate hidden
    • Input: -100 → exchange rate hidden
    • Tested after debounce expires
  • emits correct price impact curve points after debounce with varying amounts

    • Input: 750 → Max Size: 1500
    • Input: 1250 → Max Size: 2500
    • Multiple cycles verified

Acceptance Criteria Met

✅ High and critical price impact warnings are tested

  • 5 test cases covering warning scenarios and error states
  • Tests validate visual banners display for impact > 15%
  • Critical error state verified for impact > 50%
  • Color coding verified (Low/Medium/High badges)

✅ Custom poolReserves props are verified

  • 7 test cases covering all aspects of custom reserves
  • Configuration acceptance and usage validated
  • Default fallback behavior confirmed
  • Exchange rate calculation accuracy tested
  • Liquidity multiplier application verified
  • Empty/invalid reserve handling tested
  • SVG chart rendering with custom reserves confirmed
  • Asset pair mapping respected

✅ Debounced SVG chart recalculations are verified with fake timers

  • 9 test cases using vi.useFakeTimers()
  • Debounce timing (300ms) verified
  • Timer cancellation tested
  • Component cleanup on unmount verified
  • SVG recalculation correctness confirmed
  • Edge cases handled (zero/negative input)

Technical Implementation

Testing Techniques

  • Fake Timers: vi.useFakeTimers() / vi.advanceTimersByTime()
  • React Testing Library: render(), fireEvent, screen queries
  • Act Wrapper: Proper handling of state updates
  • Custom mocking: Custom poolReserves configurations

Code Quality

  • ✅ AAA Pattern (Arrange-Act-Assert) for all tests
  • ✅ Descriptive test names clearly stating purpose
  • ✅ Meaningful assertions validating behavior
  • ✅ Proper setup/teardown in all tests
  • ✅ Edge cases covered (zero input, negative values, empty reserves)
  • ✅ No side effects between tests
  • ✅ Memory safe (timers cleared, components unmounted)

Testing

All tests can be run with:

# Run all tests
npm test

# Run only SwapSimulator tests
npm test -- SwapSimulator.test.tsx

# Run with watch mode
npm run test:watch

# Run with coverage
npm run test:coverage

Closes #927
Closes #926
Closes #925
Closes #924

Ceejaytech25 added 2 commits October 21, 2026 19:29
…in SwapSimulator.test.tsx

- Added test suite for extreme price impact scenarios:
  * Tests for price impact > 15% warning banner
  * Tests for price impact > 50% critical error state
  * Verified color coding for low (< 1%), medium (1-5%), and high (> 5%) impact
  * Tests for high slippage warning with extreme price impact

- Added comprehensive custom poolReserves property tests:
  * Verify custom poolReserves configuration is accepted and used
  * Test default POOL_RESERVES fallback behavior
  * Validate correct exchange rate calculation with custom reserves
  * Test liquidity size multiplier application to custom reserves
  * Handle empty/missing custom poolReserves gracefully
  * Test price impact curve rendering with custom reserves
  * Verify asset pair mapping in custom poolReserves

- Added debounced chart calculation tests using fake timers (vi.useFakeTimers):
  * Test debounce on rapid input changes (300ms delay)
  * Verify previous debounce timer cancellation on new input
  * Test debounce applied to chart but not immediate exchange rate updates
  * Verify chart recalculation correctness after debounce with custom reserves
  * Test debounced value used in price impact curve SVG rendering
  * Verify cleanup of debounce timeout on component unmount
  * Test custom AMOUNT_CHART_DEBOUNCE_MS timing accuracy
  * Handle zero and negative input values with debounce
  * Verify correct price impact curve points after debounce with varying amounts

All tests follow AAA (Arrange-Act-Assert) pattern and use descriptive names.
Acceptance criteria fully met with comprehensive coverage.
…in SwapSimulator.test.tsx

- Added test suite for extreme price impact scenarios:
  * Tests for price impact > 15% warning banner
  * Tests for price impact > 50% critical error state
  * Verified color coding for low (< 1%), medium (1-5%), and high (> 5%) impact
  * Tests for high slippage warning with extreme price impact

- Added comprehensive custom poolReserves property tests:
  * Verify custom poolReserves configuration is accepted and used
  * Test default POOL_RESERVES fallback behavior
  * Validate correct exchange rate calculation with custom reserves
  * Test liquidity size multiplier application to custom reserves
  * Handle empty/missing custom poolReserves gracefully
  * Test price impact curve rendering with custom reserves
  * Verify asset pair mapping in custom poolReserves

- Added debounced chart calculation tests using fake timers (vi.useFakeTimers):
  * Test debounce on rapid input changes (300ms delay)
  * Verify previous debounce timer cancellation on new input
  * Test debounce applied to chart but not immediate exchange rate updates
  * Verify chart recalculation correctness after debounce with custom reserves
  * Test debounced value used in price impact curve SVG rendering
  * Verify cleanup of debounce timeout on component unmount
  * Test custom AMOUNT_CHART_DEBOUNCE_MS timing accuracy
  * Handle zero and negative input values with debounce
  * Verify correct price impact curve points after debounce with varying amounts

Addresses Sorokit#927 - Extreme price impact and custom reserve tests
@drips-wave

drips-wave Bot commented Oct 21, 2026

Copy link
Copy Markdown

@Ceejaytech25 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@k-deejah
k-deejah merged commit 546dc26 into Sorokit:main Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants