2020-09-15 08:48:08 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import sinon from 'sinon'
|
2022-04-08 05:07:13 -04:00
|
|
|
import { screen, render, fireEvent, waitFor } from '@testing-library/react'
|
2020-09-15 08:48:08 -04:00
|
|
|
|
|
|
|
import OutlineItem from '../../../../../frontend/js/features/outline/components/outline-item'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('<OutlineItem />', function () {
|
2020-09-18 06:05:05 -04:00
|
|
|
const jumpToLine = sinon.stub()
|
2020-09-15 08:48:08 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-09-18 06:05:05 -04:00
|
|
|
jumpToLine.reset()
|
2020-09-15 08:48:08 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders basic item', function () {
|
2020-09-15 08:48:08 -04:00
|
|
|
const outlineItem = {
|
|
|
|
title: 'Test Title',
|
2021-04-27 03:52:58 -04:00
|
|
|
line: 1,
|
2020-09-15 08:48:08 -04:00
|
|
|
}
|
2020-09-18 06:05:05 -04:00
|
|
|
render(<OutlineItem outlineItem={outlineItem} jumpToLine={jumpToLine} />)
|
2020-09-15 08:48:08 -04:00
|
|
|
|
|
|
|
screen.getByRole('treeitem', { current: false })
|
|
|
|
screen.getByRole('button', { name: outlineItem.title })
|
|
|
|
expect(screen.queryByRole('button', { name: 'Collapse' })).to.not.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('collapses and expands', function () {
|
2020-09-15 08:48:08 -04:00
|
|
|
const outlineItem = {
|
|
|
|
title: 'Parent',
|
|
|
|
line: 1,
|
2021-04-27 03:52:58 -04:00
|
|
|
children: [{ title: 'Child', line: 2 }],
|
2020-09-15 08:48:08 -04:00
|
|
|
}
|
2020-09-18 06:05:05 -04:00
|
|
|
render(<OutlineItem outlineItem={outlineItem} jumpToLine={jumpToLine} />)
|
2020-09-15 08:48:08 -04:00
|
|
|
|
|
|
|
const collapseButton = screen.getByRole('button', { name: 'Collapse' })
|
|
|
|
|
|
|
|
// test that children are rendered
|
|
|
|
screen.getByRole('button', { name: 'Child' })
|
|
|
|
|
|
|
|
fireEvent.click(collapseButton)
|
|
|
|
|
|
|
|
screen.getByRole('button', { name: 'Expand' })
|
|
|
|
|
|
|
|
expect(screen.queryByRole('button', { name: 'Child' })).to.not.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('highlights', function () {
|
2020-09-15 08:48:08 -04:00
|
|
|
const outlineItem = {
|
|
|
|
title: 'Parent',
|
2021-04-27 03:52:58 -04:00
|
|
|
line: 1,
|
2020-09-15 08:48:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
render(
|
|
|
|
<OutlineItem
|
|
|
|
outlineItem={outlineItem}
|
2020-09-18 06:05:05 -04:00
|
|
|
jumpToLine={jumpToLine}
|
2020-09-15 08:48:08 -04:00
|
|
|
highlightedLine={1}
|
2022-11-14 04:42:06 -05:00
|
|
|
matchesHighlightedLine
|
2020-09-15 08:48:08 -04:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { current: true })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('highlights when has collapsed highlighted child', function () {
|
2020-09-15 08:48:08 -04:00
|
|
|
const outlineItem = {
|
|
|
|
title: 'Parent',
|
|
|
|
line: 1,
|
2021-04-27 03:52:58 -04:00
|
|
|
children: [{ title: 'Child', line: 2 }],
|
2020-09-15 08:48:08 -04:00
|
|
|
}
|
|
|
|
render(
|
|
|
|
<OutlineItem
|
|
|
|
outlineItem={outlineItem}
|
2020-09-18 06:05:05 -04:00
|
|
|
jumpToLine={jumpToLine}
|
2020-09-15 08:48:08 -04:00
|
|
|
highlightedLine={2}
|
2022-11-14 04:42:06 -05:00
|
|
|
containsHighlightedLine
|
2020-09-15 08:48:08 -04:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'Parent', current: false })
|
|
|
|
screen.getByRole('treeitem', { name: 'Child', current: true })
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Collapse' }))
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'Parent', current: true })
|
|
|
|
})
|
|
|
|
|
2022-04-08 05:07:13 -04:00
|
|
|
it('click and double-click jump to location', async function () {
|
2020-09-15 08:48:08 -04:00
|
|
|
const outlineItem = {
|
|
|
|
title: 'Parent',
|
2021-04-27 03:52:58 -04:00
|
|
|
line: 1,
|
2020-09-15 08:48:08 -04:00
|
|
|
}
|
2020-09-18 06:05:05 -04:00
|
|
|
render(<OutlineItem outlineItem={outlineItem} jumpToLine={jumpToLine} />)
|
2020-09-15 08:48:08 -04:00
|
|
|
|
|
|
|
const titleButton = screen.getByRole('button', { name: outlineItem.title })
|
|
|
|
|
2022-04-08 05:07:13 -04:00
|
|
|
fireEvent.click(titleButton, { detail: 1 })
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(jumpToLine).to.be.calledOnce
|
|
|
|
expect(jumpToLine).to.be.calledWith(1, false)
|
|
|
|
})
|
2020-09-15 08:48:08 -04:00
|
|
|
|
2020-09-18 06:05:05 -04:00
|
|
|
jumpToLine.reset()
|
2022-04-08 05:07:13 -04:00
|
|
|
fireEvent.click(titleButton, { detail: 2 })
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(jumpToLine).to.be.calledOnce
|
|
|
|
expect(jumpToLine).to.be.calledWith(1, true)
|
|
|
|
})
|
2020-09-15 08:48:08 -04:00
|
|
|
})
|
|
|
|
})
|