|
1 | 1 | import React from 'react'; |
2 | | -import { render, fireEvent, vi, mockDelay } from '@test/utils'; |
| 2 | +import { fireEvent, mockDelay, render, vi } from '@test/utils'; |
3 | 3 | import { Textarea } from '..'; |
4 | 4 |
|
5 | 5 | describe('Textarea 组件测试', () => { |
@@ -94,4 +94,24 @@ describe('Textarea 组件测试', () => { |
94 | 94 |
|
95 | 95 | expect(container.getElementsByTagName('textarea')[0].selectionStart).toBe(value.length); |
96 | 96 | }); |
| 97 | + |
| 98 | + test('count follows maxcharacter and maxlength correctly', async () => { |
| 99 | + const LIMIT_SELECTOR = '.t-textarea__limit'; |
| 100 | + |
| 101 | + const { container: container1 } = render(<Textarea maxcharacter={15} value="hello世界" />); |
| 102 | + const limitText1 = container1.querySelector(LIMIT_SELECTOR); |
| 103 | + expect(limitText1?.textContent).toBe('9/15'); // hello(5) + 世(2) + 界(2) = 9 |
| 104 | + |
| 105 | + const { container: container2 } = render(<Textarea maxlength={15} value="hello世界" />); |
| 106 | + const limitText2 = container2.querySelector(LIMIT_SELECTOR); |
| 107 | + expect(limitText2?.textContent).toBe('7/15'); |
| 108 | + |
| 109 | + const { container: container3 } = render(<Textarea maxcharacter={15} value="hi👋🌍" />); |
| 110 | + const limitText3 = container3.querySelector(LIMIT_SELECTOR); |
| 111 | + expect(limitText3?.textContent).toBe('10/15'); // h(1) + i(1) + 👋(4) + 🌍(4) = 10 |
| 112 | + |
| 113 | + const { container: container4 } = render(<Textarea maxlength={15} value="hi👋🌍" />); |
| 114 | + const limitText4 = container4.querySelector(LIMIT_SELECTOR); |
| 115 | + expect(limitText4?.textContent).toBe('4/15'); |
| 116 | + }); |
97 | 117 | }); |
0 commit comments