@@ -4,84 +4,94 @@ import Statistic from '../index';
44
55describe ( '<Statistic />' , ( ) => {
66 it ( 'should match the snapshot' , ( ) => {
7- const { asFragment } = render ( < Statistic title = "Active Users" value = { 112893 } /> ) ;
7+ const { asFragment } = render (
8+ < Statistic
9+ title = "Monthly Revenue"
10+ description = "Booked revenue across all active subscriptions."
11+ value = { 128430.5 }
12+ format = { { type : 'currency' , currency : 'USD' , maximumFractionDigits : 2 } }
13+ trend = { { direction : 'up' , value : '+12.4%' , label : 'vs last month' , sentiment : 'positive' } }
14+ status = { { type : 'success' , text : 'Healthy growth' } }
15+ />
16+ ) ;
817 expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
918 } ) ;
1019
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 support custom decimal separator' , ( ) => {
20+ it ( 'should render currency formatting' , ( ) => {
3221 const { getByText } = render (
33- < Statistic value = { 112893 .5} precision = { 1 } groupSeparator = "." decimalSeparator = "," />
22+ < Statistic value = { 128430 .5} format = { { type : 'currency' , currency : 'USD' , maximumFractionDigits : 2 } } />
3423 ) ;
35- expect ( getByText ( '112.893,5 ' ) ) . toBeInTheDocument ( ) ;
24+ expect ( getByText ( '$128,430.50 ' ) ) . toBeInTheDocument ( ) ;
3625 } ) ;
3726
38- it ( 'should render prefix and suffix ' , ( ) => {
27+ it ( 'should render percent formatting ' , ( ) => {
3928 const { getByText } = render (
40- < Statistic value = { 100 } prefix = "$" suffix = "USD" />
29+ < Statistic value = { 0.2386 } format = { { type : 'percent' , maximumFractionDigits : 2 } } />
4130 ) ;
42- expect ( getByText ( '$' ) ) . toBeInTheDocument ( ) ;
43- expect ( getByText ( 'USD' ) ) . toBeInTheDocument ( ) ;
44- } ) ;
45-
46- it ( 'should use custom formatter' , ( ) => {
47- const formatter = ( val : number | string ) => `${ val } %` ;
48- const { getByText } = render ( < Statistic value = { 95 } formatter = { formatter } /> ) ;
49- expect ( getByText ( '95%' ) ) . toBeInTheDocument ( ) ;
31+ expect ( getByText ( '23.86%' ) ) . toBeInTheDocument ( ) ;
5032 } ) ;
5133
52- it ( 'should pass formatting info to formatter' , ( ) => {
53- const formatter = jest . fn ( ( _ : number | string , info ) => info . formattedValue ) ;
54- const { getByText } = render ( < Statistic value = { 2386.4 } precision = { 1 } formatter = { formatter } /> ) ;
55- expect ( getByText ( '2,386.4' ) ) . toBeInTheDocument ( ) ;
34+ it ( 'should support custom formatter' , ( ) => {
35+ const formatter = jest . fn ( ( _ , info ) => `${ info . formattedValue } live` ) ;
36+ const { getByText } = render (
37+ < Statistic value = { 112893 } format = { { type : 'compact' , maximumFractionDigits : 1 } } formatter = { formatter } />
38+ ) ;
39+ expect ( getByText ( '112.9K live' ) ) . toBeInTheDocument ( ) ;
5640 expect ( formatter ) . toHaveBeenCalledWith (
57- 2386.4 ,
41+ 112893 ,
5842 expect . objectContaining ( {
59- formattedValue : '2,386.4' ,
60- groupSeparator : ',' ,
61- decimalSeparator : '.' ,
62- precision : 1 ,
43+ formattedValue : '112.9K' ,
6344 isNumeric : true ,
6445 } )
6546 ) ;
6647 } ) ;
6748
68- it ( 'should render string value' , ( ) => {
69- const { getByText } = render ( < Statistic value = "N/A" /> ) ;
70- expect ( getByText ( 'N/A' ) ) . toBeInTheDocument ( ) ;
49+ it ( 'should render loading skeleton' , ( ) => {
50+ const { container } = render ( < Statistic title = "Net Retention" loading /> ) ;
51+ expect ( container . querySelector ( '.ty-skeleton' ) ) . toBeInTheDocument ( ) ;
52+ } ) ;
53+
54+ it ( 'should render empty placeholder for null value' , ( ) => {
55+ const { getByText } = render ( < Statistic value = { null } empty = "No data" /> ) ;
56+ expect ( getByText ( 'No data' ) ) . toBeInTheDocument ( ) ;
7157 } ) ;
7258
73- it ( 'should render empty placeholder for invalid number' , ( ) => {
74- const { getByText } = render ( < Statistic value = { Number . NaN } /> ) ;
75- expect ( getByText ( '--' ) ) . toBeInTheDocument ( ) ;
59+ it ( 'should render error state before value' , ( ) => {
60+ const { getByText, queryByText } = render (
61+ < Statistic value = { 95 } suffix = "%" error = "Feed unavailable" />
62+ ) ;
63+ expect ( getByText ( 'Feed unavailable' ) ) . toBeInTheDocument ( ) ;
64+ expect ( queryByText ( '95' ) ) . not . toBeInTheDocument ( ) ;
7665 } ) ;
7766
78- it ( 'should support custom empty placeholder' , ( ) => {
79- const { getByText } = render ( < Statistic empty = "Pending" /> ) ;
80- expect ( getByText ( 'Pending' ) ) . toBeInTheDocument ( ) ;
67+ it ( 'should render trend and status' , ( ) => {
68+ const { container, getByText } = render (
69+ < Statistic
70+ value = { 95 }
71+ trend = { { direction : 'down' , value : '-2%' , label : 'vs yesterday' , sentiment : 'negative' } }
72+ status = { { type : 'warning' , text : 'Below target' } }
73+ />
74+ ) ;
75+ expect ( getByText ( '-2%' ) ) . toBeInTheDocument ( ) ;
76+ expect ( getByText ( 'Below target' ) ) . toBeInTheDocument ( ) ;
77+ expect ( container . querySelector ( '.ty-statistic__trend_negative' ) ) . toBeInTheDocument ( ) ;
78+ expect ( container . querySelector ( '.ty-statistic__status_warning' ) ) . toBeInTheDocument ( ) ;
8179 } ) ;
8280
83- it ( 'should apply value class name' , ( ) => {
84- const { container } = render ( < Statistic value = { 95 } valueClassName = "custom-value" /> ) ;
85- expect ( container . querySelector ( '.ty-statistic__content' ) ) . toHaveClass ( 'custom-value' ) ;
81+ it ( 'should build aria label from the rendered metric content' , ( ) => {
82+ const { container } = render (
83+ < Statistic
84+ title = "Revenue"
85+ value = { 128430.5 }
86+ format = { { type : 'currency' , currency : 'USD' , maximumFractionDigits : 2 } }
87+ trend = { { direction : 'up' , value : '+12.4%' , label : 'vs last month' } }
88+ status = { { type : 'success' , text : 'Healthy growth' } }
89+ />
90+ ) ;
91+
92+ expect ( container . querySelector ( '.ty-statistic__content' ) ) . toHaveAttribute (
93+ 'aria-label' ,
94+ 'Revenue, $128,430.50, up +12.4% vs last month, success Healthy growth'
95+ ) ;
8696 } ) ;
8797} ) ;
0 commit comments