|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import Statistic from '../index'; |
| 4 | + |
| 5 | +describe('<Statistic />', () => { |
| 6 | + it('should match the snapshot', () => { |
| 7 | + const { asFragment } = render(<Statistic title="Active Users" value={112893} />); |
| 8 | + expect(asFragment()).toMatchSnapshot(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should render correctly', () => { |
| 12 | + const { container } = render(<Statistic title="Score" value={95} />); |
| 13 | + expect(container.firstChild).toHaveClass('ty-statistic'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should render title', () => { |
| 17 | + const { getByText } = render(<Statistic title="Total Sales" value={1000} />); |
| 18 | + expect(getByText('Total Sales')).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should format value with group separator', () => { |
| 22 | + const { getByText } = render(<Statistic value={112893} />); |
| 23 | + expect(getByText('112,893')).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should format value with precision', () => { |
| 27 | + const { getByText } = render(<Statistic value={11.28} precision={2} />); |
| 28 | + expect(getByText('11.28')).toBeInTheDocument(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should render prefix and suffix', () => { |
| 32 | + const { getByText } = render( |
| 33 | + <Statistic value={100} prefix="$" suffix="USD" /> |
| 34 | + ); |
| 35 | + expect(getByText('$')).toBeInTheDocument(); |
| 36 | + expect(getByText('USD')).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should use custom formatter', () => { |
| 40 | + const formatter = (val: number | string) => `${val}%`; |
| 41 | + const { getByText } = render(<Statistic value={95} formatter={formatter} />); |
| 42 | + expect(getByText('95%')).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should render string value', () => { |
| 46 | + const { getByText } = render(<Statistic value="N/A" />); |
| 47 | + expect(getByText('N/A')).toBeInTheDocument(); |
| 48 | + }); |
| 49 | +}); |
0 commit comments