-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelementText.test.tsx
87 lines (74 loc) · 2.27 KB
/
elementText.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { render } from '@testing-library/react'
import { getElementText, wrapElementText } from '../elementText'
describe('elementText', () => {
describe('getElementText', () => {
const Test = ({ children }: { children?: React.ReactNode }) => (
<div>{children}</div>
)
it('handles strings', () => {
expect(getElementText('This is a test')).toBe('This is a test')
})
it('handles numbers', () => {
expect(getElementText(100)).toBe(100)
})
it('handles nested markup', () => {
expect(
getElementText(
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
</ul>
)
).toBe('Option 1 Option 2 Option 3 Option 4')
})
it('handles React elements', () => {
expect(
getElementText(
<Test>
<span>Level 1</span>
<span>Level 2</span>
<span>Level 3</span>
</Test>
)
).toBe('Level 1 Level 2 Level 3')
})
it('returns undefined if no text found', () => {
expect(getElementText(<Test />)).toBeUndefined()
})
})
describe('wrapElementText', () => {
it('can handle strings', () => {
const Result = () => <>{wrapElementText(<div>Level One</div>, 'one')}</>
const { container } = render(<Result />)
expect(container.innerHTML).toBe('<div>Level <mark>One</mark></div>')
})
it('can handle elements with children', () => {
const Result = () => (
<>
{wrapElementText(
<div>
<ul>
<li>
<a href="https://twitter.com">Twitter</a>
</li>
<li>
<a href="https://facebook.com">Facebook</a>
</li>
<li>
<a href="https://reddit.com">Reddit</a>
</li>
</ul>
</div>,
'twit'
)}
</>
)
const { container } = render(<Result />)
expect(container.innerHTML).toBe(
'<div><ul><li><a href="https://twitter.com"><mark>Twit</mark>ter</a></li><li><a href="https://facebook.com">Facebook</a></li><li><a href="https://reddit.com">Reddit</a></li></ul></div>'
)
})
})
})