import { axe } from 'jest-axe'
import React from 'react'
import { render } from '@testing-library/react'

import Text from './text'

it('renders without crashing', () => {
  const { container } = render(
    <div>
      <Text
        text={'Hello World! https://en.wikipedia.org/wiki/Second_Life ' +
        '[http://wiki.secondlife.com/wiki/Main_Page the second life wiki]'}
      />
    </div>
  )

  expect(container).toBeTruthy()
})

it('should render plain URLs as <a>', () => {
  const { queryByText } = render(
    <div>
      <Text text='Hello World! https://en.wikipedia.org/wiki/Second_Life' />
    </div>
  )

  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life')).toBeTruthy()
  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life').tagName).toBe('A')
  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life').href)
    .toBe('https://en.wikipedia.org/wiki/Second_Life')
  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life').rel)
    .toBe('noopener noreferrer')
})

it('should render formatted URLs as <a> with the text as its child', () => {
  const { queryByText } = render(
    <div>
      <Text text='Hello World! [http://wiki.secondlife.com/wiki/Main_Page the second life wiki]!' />
    </div>
  )

  expect(queryByText('the second life wiki')).toBeTruthy()
  expect(queryByText('the second life wiki').tagName).toBe('A')
  expect(queryByText('the second life wiki').href)
    .toBe('http://wiki.secondlife.com/wiki/Main_Page')
  expect(queryByText('the second life wiki').rel)
    .toBe('noopener noreferrer')
})

it('should use its className for the <a>', () => {
  const { queryByText } = render(
    <div>
      <Text
        text={'Hello World! https://en.wikipedia.org/wiki/Second_Life ' +
        '[http://wiki.secondlife.com/wiki/Main_Page the second life wiki]'}
        className='text-class'
      />
    </div>
  )

  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life').className)
    .toBe('text-class')

  expect(queryByText('the second life wiki').className)
    .toBe('text-class')
})

it('should support multiline mode', () => {
  const { queryByText } = render(
    <div>
      <Text
        text={'Hello World! https://en.wikipedia.org/wiki/Second_Life\n' +
        '[http://wiki.secondlife.com/wiki/Main_Page the second life\nwiki]'}
        multiline
      />
    </div>
  )

  const sibling = queryByText('https://en.wikipedia.org/wiki/Second_Life').nextElementSibling
  expect(sibling).toBeTruthy()
  expect(sibling.nodeName).toBe('BR')

  const brChild = queryByText('the second life', { exact: false }).querySelector('br')
  expect(brChild).toBeTruthy()
  expect(brChild.nodeName).toBe('BR')
})

it('should pass aXe', async () => {
  const { container } = render(
    <div>
      <Text
        text={'Hello World! https://en.wikipedia.org/wiki/Second_Life ' +
        '[http://wiki.secondlife.com/wiki/Main_Page the second life wiki]'}
      />
    </div>
  )

  expect(await axe(container)).toHaveNoViolations()
})

@@ -1006,32 +1006,1103 @@
 oreferrer')%0A%7D)%0A%0A
+it('should render plain URLs after a firefox chat info', () =%3E %7B%0A  const %7B queryByText %7D = render(%0A    %3Cdiv%3E%0A      %3CText%0A        text=%7B'(32W 6.3.2f*) http://wiki.secondlife.com/wiki/Main_Page ' +%0A        'https://en.wikipedia.org/wiki/Second_Life'%7D%0A      /%3E%0A    %3C/div%3E%0A  )%0A%0A  expect(queryByText('http://wiki.secondlife.com/wiki/Main_Page')).toBeTruthy()%0A  expect(queryByText('http://wiki.secondlife.com/wiki/Main_Page').tagName).toBe('A')%0A  expect(queryByText('http://wiki.secondlife.com/wiki/Main_Page').href)%0A    .toBe('http://wiki.secondlife.com/wiki/Main_Page')%0A  expect(queryByText('http://wiki.secondlife.com/wiki/Main_Page').rel)%0A    .toBe('noopener noreferrer')%0A%0A  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life')).toBeTruthy()%0A  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life').tagName).toBe('A')%0A  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life').href)%0A    .toBe('https://en.wikipedia.org/wiki/Second_Life')%0A  expect(queryByText('https://en.wikipedia.org/wiki/Second_Life').rel)%0A    .toBe('noopener noreferrer')%0A%7D)%0A%0A
 it('should rende

add test to check if urls after ) are parsed