2020-09-15 08:48:08 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import React from 'react'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
import { screen, render, fireEvent } from '@testing-library/react'
|
|
|
|
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
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 })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('click and double-click jump to location', 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 })
|
|
|
|
|
|
|
|
fireEvent.click(titleButton)
|
2020-09-18 06:05:05 -04:00
|
|
|
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()
|
2020-09-15 08:48:08 -04:00
|
|
|
fireEvent.doubleClick(titleButton)
|
2020-09-18 06:05:05 -04:00
|
|
|
expect(jumpToLine).to.be.calledOnce
|
|
|
|
expect(jumpToLine).to.be.calledWith(1, true)
|
2020-09-15 08:48:08 -04:00
|
|
|
})
|
|
|
|
})
|