1 | import {bahtText} from './';
|
2 |
|
3 | const defaultResult = false
|
4 | jest.autoMockOff()
|
5 |
|
6 | describe('digit', () => {
|
7 | test('1, zero', () => {
|
8 | expect(bahtText(0)).toBe('ศูนย์บาทถ้วน')
|
9 | })
|
10 |
|
11 | test('1 digit', () => {
|
12 | expect(bahtText(1)).toBe('หนึ่งบาทถ้วน')
|
13 | expect(bahtText(2)).toBe('สองบาทถ้วน')
|
14 | expect(bahtText(3)).toBe('สามบาทถ้วน')
|
15 | expect(bahtText(4)).toBe('สี่บาทถ้วน')
|
16 | expect(bahtText(5)).toBe('ห้าบาทถ้วน')
|
17 | expect(bahtText(6)).toBe('หกบาทถ้วน')
|
18 | expect(bahtText(7)).toBe('เจ็ดบาทถ้วน')
|
19 | expect(bahtText(8)).toBe('แปดบาทถ้วน')
|
20 | expect(bahtText(9)).toBe('เก้าบาทถ้วน')
|
21 | })
|
22 |
|
23 | test('2', () => {
|
24 | expect(bahtText(37)).toBe('สามสิบเจ็ดบาทถ้วน')
|
25 | expect(bahtText(48)).toBe('สี่สิบแปดบาทถ้วน')
|
26 | })
|
27 |
|
28 | test('3', () => {
|
29 | expect(bahtText(232)).toBe('สองร้อยสามสิบสองบาทถ้วน')
|
30 | })
|
31 |
|
32 | test('4', () => {
|
33 | expect(bahtText(3333)).toBe('สามพันสามร้อยสามสิบสามบาทถ้วน')
|
34 | })
|
35 |
|
36 | test('5', () => {
|
37 | expect(bahtText(44444)).toBe('สี่หมื่นสี่พันสี่ร้อยสี่สิบสี่บาทถ้วน')
|
38 | })
|
39 |
|
40 | test('6', () => {
|
41 | expect(bahtText(555555)).toBe('ห้าแสนห้าหมื่นห้าพันห้าร้อยห้าสิบห้าบาทถ้วน')
|
42 | })
|
43 |
|
44 | test('7', () => {
|
45 | expect(bahtText(6666666)).toBe('หกล้านหกแสนหกหมื่นหกพันหกร้อยหกสิบหกบาทถ้วน')
|
46 | })
|
47 |
|
48 | test('8', () => {
|
49 | expect(bahtText(77777777)).toBe('เจ็ดสิบเจ็ดล้านเจ็ดแสนเจ็ดหมื่นเจ็ดพันเจ็ดร้อยเจ็ดสิบเจ็ดบาทถ้วน')
|
50 | })
|
51 |
|
52 | test('9', () => {
|
53 | expect(bahtText(888888888)).toBe('แปดร้อยแปดสิบแปดล้านแปดแสนแปดหมื่นแปดพันแปดร้อยแปดสิบแปดบาทถ้วน')
|
54 | })
|
55 |
|
56 | test('10', () => {
|
57 | expect(bahtText(9999999999)).toBe('เก้าพันเก้าร้อยเก้าสิบเก้าล้านเก้าแสนเก้าหมื่นเก้าพันเก้าร้อยเก้าสิบเก้าบาทถ้วน')
|
58 | })
|
59 |
|
60 | test('11', () => {
|
61 | expect(bahtText(12345678901)).toBe('หนึ่งหมื่นสองพันสามร้อยสี่สิบห้าล้านหกแสนเจ็ดหมื่นแปดพันเก้าร้อยเอ็ดบาทถ้วน')
|
62 | })
|
63 |
|
64 | test('12', () => {
|
65 | expect(bahtText(123456789012)).toBe('หนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบหกล้านเจ็ดแสนแปดหมื่นเก้าพันสิบสองบาทถ้วน')
|
66 | })
|
67 |
|
68 | test('13', () => {
|
69 | expect(bahtText(1234567890133)).toBe('หนึ่งล้านสองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ดล้านแปดแสนเก้าหมื่นหนึ่งร้อยสามสิบสามบาทถ้วน')
|
70 | })
|
71 |
|
72 | test('14', () => {
|
73 | expect(bahtText(12345678901234)).toBe('สิบสองล้านสามแสนสี่หมื่นห้าพันหกร้อยเจ็ดสิบแปดล้านเก้าแสนหนึ่งพันสองร้อยสามสิบสี่บาทถ้วน')
|
74 | })
|
75 |
|
76 | test('15', () => {
|
77 | expect(bahtText(123456789012345)).toBe('หนึ่งร้อยยี่สิบสามล้านสี่แสนห้าหมื่นหกพันเจ็ดร้อยแปดสิบเก้าล้านหนึ่งหมื่นสองพันสามร้อยสี่สิบห้าบาทถ้วน')
|
78 | })
|
79 |
|
80 | test('16 digits, not more than Number.MAX_SAFE_INTEGER', () => {
|
81 |
|
82 |
|
83 | expect(bahtText(1234567890123450)).toBe('หนึ่งพันสองร้อยสามสิบสี่ล้านห้าแสนหกหมื่นเจ็ดพันแปดร้อยเก้าสิบล้านหนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบบาทถ้วน')
|
84 | })
|
85 |
|
86 | test('16 digits, more than Number.MAX_SAFE_INTEGER', () => {
|
87 |
|
88 | expect(bahtText(9007199254750991)).toBe(defaultResult)
|
89 | })
|
90 |
|
91 | test('more than 16', () => {
|
92 | expect(bahtText(12345678901234500)).toBe(defaultResult)
|
93 | expect(bahtText(123456789012345000)).toBe(defaultResult)
|
94 | expect(bahtText(1234567890123450000)).toBe(defaultResult)
|
95 | expect(bahtText(123456789012345000000)).toBe(defaultResult)
|
96 | })
|
97 | })
|
98 |
|
99 | describe('grammar', () => {
|
100 | test('end with 1 (more than 10)', () => {
|
101 | expect(bahtText(11)).toBe('สิบเอ็ดบาทถ้วน')
|
102 | expect(bahtText(201)).toBe('สองร้อยเอ็ดบาทถ้วน')
|
103 |
|
104 | expect(bahtText(3061.21)).toBe('สามพันหกสิบเอ็ดบาทยี่สิบเอ็ดสตางค์')
|
105 | expect(bahtText(456011.71)).toBe('สี่แสนห้าหมื่นหกพันสิบเอ็ดบาทเจ็ดสิบเอ็ดสตางค์')
|
106 | })
|
107 |
|
108 | test('end with 1x', () => {
|
109 | expect(bahtText(14)).toBe('สิบสี่บาทถ้วน')
|
110 | expect(bahtText(40019)).toBe('สี่หมื่นสิบเก้าบาทถ้วน')
|
111 |
|
112 | expect(bahtText(317.10)).toBe('สามร้อยสิบเจ็ดบาทสิบสตางค์')
|
113 | expect(bahtText(40019.17)).toBe('สี่หมื่นสิบเก้าบาทสิบเจ็ดสตางค์')
|
114 | })
|
115 |
|
116 | test('end with 2x', () => {
|
117 | expect(bahtText(22)).toBe('ยี่สิบสองบาทถ้วน')
|
118 | expect(bahtText(5723)).toBe('ห้าพันเจ็ดร้อยยี่สิบสามบาทถ้วน')
|
119 |
|
120 | expect(bahtText(57.23)).toBe('ห้าสิบเจ็ดบาทยี่สิบสามสตางค์')
|
121 | expect(bahtText(422.26)).toBe('สี่ร้อยยี่สิบสองบาทยี่สิบหกสตางค์')
|
122 | })
|
123 | })
|
124 |
|
125 | describe('decimals', () => {
|
126 | test('general', () => {
|
127 | expect(bahtText(1.01)).toBe('หนึ่งบาทหนึ่งสตางค์')
|
128 | expect(bahtText(1.02)).toBe('หนึ่งบาทสองสตางค์')
|
129 | expect(bahtText(1.03)).toBe('หนึ่งบาทสามสตางค์')
|
130 | expect(bahtText(1.04)).toBe('หนึ่งบาทสี่สตางค์')
|
131 | expect(bahtText(1.05)).toBe('หนึ่งบาทห้าสตางค์')
|
132 | expect(bahtText(1.06)).toBe('หนึ่งบาทหกสตางค์')
|
133 | expect(bahtText(1.07)).toBe('หนึ่งบาทเจ็ดสตางค์')
|
134 | expect(bahtText(1.08)).toBe('หนึ่งบาทแปดสตางค์')
|
135 | expect(bahtText(1.09)).toBe('หนึ่งบาทเก้าสตางค์')
|
136 |
|
137 | expect(bahtText(32.23)).toBe('สามสิบสองบาทยี่สิบสามสตางค์')
|
138 | expect(bahtText(474.45)).toBe('สี่ร้อยเจ็ดสิบสี่บาทสี่สิบห้าสตางค์')
|
139 | expect(bahtText(5789.67)).toBe('ห้าพันเจ็ดร้อยแปดสิบเก้าบาทหกสิบเจ็ดสตางค์')
|
140 | expect(bahtText(63147.89)).toBe('หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์')
|
141 | })
|
142 |
|
143 | test('more than 2 decimals, using 2 decimals', () => {
|
144 | expect(bahtText(25.1230)).toBe('ยี่สิบห้าบาทสิบสองสตางค์')
|
145 | expect(bahtText(345.2345)).toBe('สามร้อยสี่สิบห้าบาทยี่สิบสามสตางค์')
|
146 | expect(bahtText(5678.4567)).toBe('ห้าพันหกร้อยเจ็ดสิบแปดบาทสี่สิบหกสตางค์')
|
147 | })
|
148 |
|
149 | test('not more than 1', () => {
|
150 | expect(bahtText(0.25)).toBe('ยี่สิบห้าสตางค์')
|
151 | expect(bahtText(0.36)).toBe('สามสิบหกสตางค์')
|
152 | expect(bahtText(0.69)).toBe('หกสิบเก้าสตางค์')
|
153 | })
|
154 | })
|
155 |
|
156 | describe('string', () => {
|
157 | test('should convert number in string format', () => {
|
158 | expect(bahtText(('1234' as unknown) as number)).toBe(
|
159 | 'หนึ่งพันสองร้อยสามสิบสี่บาทถ้วน'
|
160 | );
|
161 | expect(bahtText(('-1' as unknown) as number)).toBe('ลบหนึ่งบาทถ้วน');
|
162 | expect(bahtText(('-30' as unknown) as number)).toBe('ลบสามสิบบาทถ้วน');
|
163 | expect(bahtText(('123456789.999' as unknown) as number)).toEqual(
|
164 | 'หนึ่งร้อยยี่สิบสามล้านสี่แสนห้าหมื่นหกพันเจ็ดร้อยเก้าสิบบาทถ้วน'
|
165 | );
|
166 | });
|
167 | test('test more striing', () => {
|
168 | expect(bahtText(('01' as unknown) as number)).toBe(
|
169 | 'หนึ่งบาทถ้วน'
|
170 | );
|
171 | expect(bahtText(('01.01' as unknown) as number)).toBe(
|
172 | 'หนึ่งบาทหนึ่งสตางค์'
|
173 | );
|
174 | expect(bahtText(('-01' as unknown) as number)).toBe('ลบหนึ่งบาทถ้วน');
|
175 | expect(bahtText(('-01.01' as unknown) as number)).toBe('ลบหนึ่งบาทหนึ่งสตางค์');
|
176 | });
|
177 | })
|