-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,89 @@ | ||
import React from 'react' | ||
|
||
import { getElementText } from '../elementText' | ||
import { render } from '@testing-library/react' | ||
|
||
describe('getElementText', () => { | ||
const Test = ({ children }: { children?: React.ReactNode }) => ( | ||
<div>{children}</div> | ||
) | ||
import { getElementText, wrapElementText } from '../elementText' | ||
|
||
it('handles strings', () => { | ||
expect(getElementText('This is a test')).toBe('This is a test') | ||
}) | ||
describe('elementText', () => { | ||
describe('getElementText', () => { | ||
const Test = ({ children }: { children?: React.ReactNode }) => ( | ||
<div>{children}</div> | ||
) | ||
|
||
it('handles numbers', () => { | ||
expect(getElementText(100)).toBe(100) | ||
}) | ||
it('handles strings', () => { | ||
expect(getElementText('This is a test')).toBe('This is a test') | ||
}) | ||
|
||
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 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() | ||
}) | ||
}) | ||
|
||
it('handles React elements', () => { | ||
expect( | ||
getElementText( | ||
<Test> | ||
<span>Level 1</span> | ||
<span>Level 2</span> | ||
<span>Level 3</span> | ||
</Test> | ||
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' | ||
)} | ||
</> | ||
) | ||
).toBe('Level 1 Level 2 Level 3') | ||
}) | ||
|
||
it('returns undefined if no text found', () => { | ||
expect(getElementText(<Test />)).toBeUndefined() | ||
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>' | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters