@@ -46,4 +46,72 @@ describe('yuanFormat', () => {
4646 expect ( yuanFormat ( - 1000000 , { unit : 'yuan' } ) ) . toMatchInlineSnapshot ( `"-1,000,000.00"` )
4747 expect ( yuanFormat ( - 9432432.34 , { unit : 'yuan' } ) ) . toMatchInlineSnapshot ( `"-9,432,432.34"` )
4848 } )
49+
50+ describe ( 'prefix option' , ( ) => {
51+ it ( 'should work with prefix: true (¥)' , ( ) => {
52+ expect ( yuanFormat ( 100 , { prefix : true } ) ) . toBe ( '¥1.00' )
53+ expect ( yuanFormat ( 100000 , { prefix : true } ) ) . toBe ( '¥1,000.00' )
54+ expect ( yuanFormat ( 100 , { prefix : true , unit : 'yuan' } ) ) . toBe ( '¥100.00' )
55+ expect ( yuanFormat ( - 100 , { prefix : true } ) ) . toBe ( '¥-1.00' )
56+ } )
57+
58+ it ( 'should work with custom prefix string' , ( ) => {
59+ expect ( yuanFormat ( 100 , { prefix : '$' } ) ) . toBe ( '$1.00' )
60+ expect ( yuanFormat ( 100000 , { prefix : 'USD' } ) ) . toBe ( 'USD1,000.00' )
61+ expect ( yuanFormat ( 100 , { prefix : '€' , unit : 'yuan' } ) ) . toBe ( '€100.00' )
62+ expect ( yuanFormat ( - 100 , { prefix : 'RMB' } ) ) . toBe ( 'RMB-1.00' )
63+ } )
64+
65+ it ( 'should work without prefix (undefined)' , ( ) => {
66+ expect ( yuanFormat ( 100 ) ) . toBe ( '1.00' )
67+ expect ( yuanFormat ( 100 , { prefix : undefined } ) ) . toBe ( '1.00' )
68+ expect ( yuanFormat ( 100000 ) ) . toBe ( '1,000.00' )
69+ } )
70+ } )
71+
72+ describe ( 'space option' , ( ) => {
73+ it ( 'should add space between prefix and amount when space: true' , ( ) => {
74+ expect ( yuanFormat ( 100 , { prefix : true , space : true } ) ) . toBe ( '¥ 1.00' )
75+ expect ( yuanFormat ( 100000 , { prefix : true , space : true } ) ) . toBe ( '¥ 1,000.00' )
76+ expect ( yuanFormat ( 100 , { prefix : '$' , space : true } ) ) . toBe ( '$ 1.00' )
77+ expect ( yuanFormat ( 100 , { prefix : 'USD' , space : true } ) ) . toBe ( 'USD 1.00' )
78+ expect ( yuanFormat ( - 100 , { prefix : true , space : true } ) ) . toBe ( '¥ -1.00' )
79+ } )
80+
81+ it ( 'should not add space when space: false or undefined' , ( ) => {
82+ expect ( yuanFormat ( 100 , { prefix : true , space : false } ) ) . toBe ( '¥1.00' )
83+ expect ( yuanFormat ( 100 , { prefix : true } ) ) . toBe ( '¥1.00' )
84+ expect ( yuanFormat ( 100 , { prefix : '$' , space : false } ) ) . toBe ( '$1.00' )
85+ expect ( yuanFormat ( 100 , { prefix : '$' } ) ) . toBe ( '$1.00' )
86+ } )
87+
88+ it ( 'should ignore space option when prefix is undefined' , ( ) => {
89+ expect ( yuanFormat ( 100 , { space : true } ) ) . toBe ( '1.00' )
90+ expect ( yuanFormat ( 100 , { prefix : undefined , space : true } ) ) . toBe ( '1.00' )
91+ } )
92+ } )
93+
94+ describe ( 'combined prefix and space options' , ( ) => {
95+ it ( 'should work with all combinations' , ( ) => {
96+ // prefix: true, space: true
97+ expect ( yuanFormat ( 12345 , { prefix : true , space : true } ) ) . toBe ( '¥ 123.45' )
98+ // prefix: string, space: true
99+ expect ( yuanFormat ( 12345 , { prefix : 'CNY' , space : true } ) ) . toBe ( 'CNY 123.45' )
100+ // prefix: true, space: false
101+ expect ( yuanFormat ( 12345 , { prefix : true , space : false } ) ) . toBe ( '¥123.45' )
102+ // prefix: string, space: false
103+ expect ( yuanFormat ( 12345 , { prefix : 'CNY' , space : false } ) ) . toBe ( 'CNY123.45' )
104+ // with unit: yuan
105+ expect ( yuanFormat ( 123.45 , { prefix : true , space : true , unit : 'yuan' } ) ) . toBe ( '¥ 123.45' )
106+ expect ( yuanFormat ( 123456.78 , { prefix : 'USD' , space : true , unit : 'yuan' } ) ) . toBe ( 'USD 123,456.78' )
107+ } )
108+ } )
109+
110+ describe ( 'error cases with prefix and space' , ( ) => {
111+ it ( 'should return "-" when input is invalid even with prefix/space options' , ( ) => {
112+ expect ( yuanFormat ( null as any , { prefix : true , space : true } ) ) . toBe ( '-' )
113+ expect ( yuanFormat ( 'abc' as any , { prefix : '$' , space : true } ) ) . toBe ( '-' )
114+ expect ( yuanFormat ( Number . NaN , { prefix : true } ) ) . toBe ( '-' )
115+ } )
116+ } )
49117} )
0 commit comments