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'
describe('', function() {
const jumpToLine = sinon.stub()
afterEach(function() {
jumpToLine.reset()
})
it('renders basic item', function() {
const outlineItem = {
title: 'Test Title',
line: 1
}
render()
screen.getByRole('treeitem', { current: false })
screen.getByRole('button', { name: outlineItem.title })
expect(screen.queryByRole('button', { name: 'Collapse' })).to.not.exist
})
it('collapses and expands', function() {
const outlineItem = {
title: 'Parent',
line: 1,
children: [{ title: 'Child', line: 2 }]
}
render()
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
})
it('highlights', function() {
const outlineItem = {
title: 'Parent',
line: 1
}
render(
)
screen.getByRole('treeitem', { current: true })
})
it('highlights when has collapsed highlighted child', function() {
const outlineItem = {
title: 'Parent',
line: 1,
children: [{ title: 'Child', line: 2 }]
}
render(
)
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 })
})
it('click and double-click jump to location', function() {
const outlineItem = {
title: 'Parent',
line: 1
}
render()
const titleButton = screen.getByRole('button', { name: outlineItem.title })
fireEvent.click(titleButton)
expect(jumpToLine).to.be.calledOnce
expect(jumpToLine).to.be.calledWith(1, false)
jumpToLine.reset()
fireEvent.doubleClick(titleButton)
expect(jumpToLine).to.be.calledOnce
expect(jumpToLine).to.be.calledWith(1, true)
})
})