|
1 |
| -import { render, screen } from '@testing-library/react'; |
| 1 | +import { act, fireEvent, waitFor } from '@testing-library/react'; |
2 | 2 | import BalanceSheetGraph from './BalanceSheetGraph';
|
| 3 | +import { |
| 4 | + getMockStoreData, |
| 5 | + mockBalanceSheetData, |
| 6 | + renderWithMockContext, |
| 7 | +} from '../../../../utilities/test-utilities'; |
3 | 8 |
|
4 |
| -jest.mock('react-router-dom', () => ({ |
5 |
| - ...jest.requireActual('react-router-dom'), |
6 |
| - useNavigate: jest.fn(), |
7 |
| -})); |
| 9 | +describe('Balance Sheet Graph component ', () => { |
| 10 | + let providerProps = JSON.parse(JSON.stringify(getMockStoreData())); |
| 11 | + let container; |
8 | 12 |
|
9 |
| -describe.skip('Balance Sheet Graph component ', () => { |
10 |
| - beforeEach(() => render(<BalanceSheetGraph balanceSheetData={undefined} width={900} />)); |
| 13 | + beforeEach(() => { |
| 14 | + ({ container } = renderWithMockContext( |
| 15 | + <BalanceSheetGraph balanceSheetData={mockBalanceSheetData} width={900} />, |
| 16 | + { providerProps }, |
| 17 | + )); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should render the graph', () => { |
| 21 | + const svg = container.querySelector('svg'); |
| 22 | + expect(svg).toBeInTheDocument(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('renders correctly with empty data', () => { |
| 26 | + renderWithMockContext(<BalanceSheetGraph balanceSheetData={{ periods: [] }} width={900} />, { |
| 27 | + providerProps, |
| 28 | + }); |
| 29 | + const svg = container.querySelector('svg'); |
| 30 | + expect(svg).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('renders correct number of bars for the periods', () => { |
| 34 | + const bars = container.querySelectorAll('g.bar-group'); |
| 35 | + expect(bars.length).toBe(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('displays tooltip with correct content on hover', async () => { |
| 39 | + const bar = container.querySelector('rect.bar'); |
| 40 | + let tooltip = container.querySelector('.sats-flow-tooltip'); |
| 41 | + |
| 42 | + expect(tooltip).toBeNull(); |
| 43 | + expect(bar).not.toBeNull(); |
| 44 | + |
| 45 | + await act(async () => fireEvent.mouseOver(bar)); |
| 46 | + |
| 47 | + tooltip = await waitFor(() => { |
| 48 | + const tooltipElement = document.querySelector('.balance-sheet-tooltip'); |
| 49 | + if (!tooltipElement) throw new Error('Tooltip not found'); |
| 50 | + return tooltipElement; |
| 51 | + }); |
| 52 | + |
| 53 | + expect(tooltip).toBeVisible(); |
| 54 | + |
| 55 | + const tooltipText = tooltip.textContent?.replace(/\s+/g, ' ').trim(); |
| 56 | + const expectedText = ` |
| 57 | + Short Channel ID: 12345x12345x1 |
| 58 | + Remote Alias: Wallet 1 |
| 59 | + Balance: 500,000.000 |
| 60 | + Percentage: 50% |
| 61 | + Account: onchain_wallet |
| 62 | + Total Period Balance: 1,000,000.000 |
| 63 | + ` |
| 64 | + .replace(/\s+/g, ' ') |
| 65 | + .trim(); |
| 66 | + expect(tooltipText).toContain(expectedText); |
| 67 | + }); |
| 68 | + |
| 69 | + it('hides tooltip on mouseout', async () => { |
| 70 | + const bar = container.querySelector('rect.bar'); |
| 71 | + let tooltip = container.querySelector('.balance-sheet-tooltip'); |
| 72 | + |
| 73 | + await act(async () => fireEvent.mouseOver(bar)); |
| 74 | + |
| 75 | + tooltip = await waitFor(() => { |
| 76 | + const tooltipElement = document.querySelector('.balance-sheet-tooltip'); |
| 77 | + if (!tooltipElement) throw new Error('Tooltip not found'); |
| 78 | + return tooltipElement; |
| 79 | + }); |
| 80 | + |
| 81 | + expect(tooltip).toBeVisible(); |
| 82 | + |
| 83 | + await act(async () => fireEvent.mouseOut(bar)); |
| 84 | + |
| 85 | + tooltip = await waitFor(() => { |
| 86 | + const tooltipElement = document.querySelector('.balance-sheet-tooltip'); |
| 87 | + if (!tooltipElement) throw new Error('Tooltip not found'); |
| 88 | + return tooltipElement; |
| 89 | + }); |
| 90 | + |
| 91 | + expect(tooltip).not.toBeVisible(); |
| 92 | + }); |
| 93 | + it('renders the x-axis and y-axis correctly', () => { |
| 94 | + const xAxisLabels = container.querySelectorAll('.x-axis text'); |
| 95 | + const yAxisLabels = container.querySelectorAll('.y-axis text'); |
11 | 96 |
|
12 |
| - it('should be in the document', () => { |
13 |
| - expect(screen.getByTestId('balancesheetgraph-container')).not.toBeEmptyDOMElement() |
| 97 | + expect(xAxisLabels.length).toBe(mockBalanceSheetData.periods.length); |
| 98 | + expect(yAxisLabels.length).toBeGreaterThan(0); |
| 99 | + expect(xAxisLabels[0].textContent).toBe(mockBalanceSheetData.periods[0].periodKey); |
14 | 100 | });
|
15 | 101 | });
|
0 commit comments