Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Anchorme.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ it('should render with custom component', () => {
expect(customCallback).toHaveBeenCalledWith(URL)
})

it('should render mixed text node and expression children as links', () => {
const { container } = render(<Anchorme>testa.com {'testb.com'}</Anchorme>)
const links = container.querySelectorAll('a')
expect(links).toHaveLength(2)
expect(links[0]?.href).toBe('http://testa.com/')
expect(links[1]?.href).toBe('http://testb.com/')
})

it('should render expression children with surrounding spaces', () => {
const { container } = render(<Anchorme>{'testa.com'} testb.com</Anchorme>)
const links = container.querySelectorAll('a')
expect(links).toHaveLength(2)
expect(links[0]?.href).toBe('http://testa.com/')
expect(links[1]?.href).toBe('http://testb.com/')
})

it('should render plain text mixed with expression URL', () => {
const { container, getByText } = render(
<Anchorme>hello {'example.com'} world</Anchorme>,
)
const links = container.querySelectorAll('a')
expect(links).toHaveLength(1)
expect(links[0]?.href).toBe('http://example.com/')
expect(getByText(/hello/)).toBeInTheDocument()
expect(getByText(/world/)).toBeInTheDocument()
})

it('should render with custom inline component', () => {
const customCallback = jest.fn<void, [string]>()

Expand Down
4 changes: 2 additions & 2 deletions src/Anchorme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { AnchorProps, LinkComponent } from './types'
import { Link } from './Link'

type Props = {
children: string
children: string | string[]
linkComponent?: LinkComponent
} & AnchorProps

const Anchorme = ({ children, ...rest }: Props) => {
const text = children
const text = Array.isArray(children) ? children.join('') : children

const parse = useCallback(() => {
const matches = anchorme.list(text)
Expand Down
Loading