2020-09-18 06:05:05 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import sinon from 'sinon'
|
2020-12-14 06:44:10 -05:00
|
|
|
import { screen, fireEvent } from '@testing-library/react'
|
2020-09-18 06:05:05 -04:00
|
|
|
|
|
|
|
import OutlinePane from '../../../../../frontend/js/features/outline/components/outline-pane'
|
2020-12-14 06:44:10 -05:00
|
|
|
import { renderWithEditorContext } from '../../../helpers/render-with-context'
|
2020-09-18 06:05:05 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('<OutlinePane />', function () {
|
2020-09-18 06:05:05 -04:00
|
|
|
const jumpToLine = () => {}
|
|
|
|
const onToggle = sinon.stub()
|
|
|
|
const eventTracking = { sendMB: sinon.stub() }
|
|
|
|
|
2020-12-14 06:44:10 -05:00
|
|
|
function render(children) {
|
|
|
|
renderWithEditorContext(children, { projectId: '123abc' })
|
|
|
|
}
|
|
|
|
|
2021-02-09 10:39:44 -05:00
|
|
|
let originalLocalStorage
|
2021-04-14 09:17:21 -04:00
|
|
|
before(function () {
|
2021-02-09 10:39:44 -05:00
|
|
|
originalLocalStorage = global.localStorage
|
|
|
|
|
|
|
|
Object.defineProperty(global, 'localStorage', {
|
|
|
|
value: {
|
|
|
|
getItem: sinon.stub().returns(null),
|
2021-04-27 03:52:58 -04:00
|
|
|
setItem: sinon.stub(),
|
2021-11-15 11:33:57 -05:00
|
|
|
removeItem: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2021-02-09 10:39:44 -05:00
|
|
|
})
|
2020-09-18 06:05:05 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-09-18 06:05:05 -04:00
|
|
|
onToggle.reset()
|
|
|
|
eventTracking.sendMB.reset()
|
|
|
|
global.localStorage.getItem.resetHistory()
|
|
|
|
global.localStorage.setItem.resetHistory()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
after(function () {
|
2021-02-09 10:39:44 -05:00
|
|
|
Object.defineProperty(global, 'localStorage', {
|
2021-04-27 03:52:58 -04:00
|
|
|
value: originalLocalStorage,
|
2021-02-09 10:39:44 -05:00
|
|
|
})
|
2020-09-18 06:05:05 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders expanded outline', function () {
|
2020-09-18 06:05:05 -04:00
|
|
|
const outline = [
|
|
|
|
{
|
|
|
|
title: 'Section',
|
|
|
|
line: 1,
|
2021-04-27 03:52:58 -04:00
|
|
|
level: 10,
|
|
|
|
},
|
2020-09-18 06:05:05 -04:00
|
|
|
]
|
|
|
|
render(
|
|
|
|
<OutlinePane
|
|
|
|
isTexFile
|
|
|
|
outline={outline}
|
|
|
|
jumpToLine={jumpToLine}
|
|
|
|
onToggle={onToggle}
|
|
|
|
eventTracking={eventTracking}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
screen.getByRole('tree')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders disabled outline', function () {
|
2020-09-18 06:05:05 -04:00
|
|
|
const outline = []
|
|
|
|
render(
|
|
|
|
<OutlinePane
|
|
|
|
isTexFile={false}
|
|
|
|
outline={outline}
|
|
|
|
jumpToLine={jumpToLine}
|
|
|
|
onToggle={onToggle}
|
|
|
|
eventTracking={eventTracking}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(screen.queryByRole('tree')).to.be.null
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('expand outline and use local storage', function () {
|
2021-11-15 10:44:06 -05:00
|
|
|
global.localStorage.getItem.callsFake(key => {
|
|
|
|
if (key.startsWith('file_outline.expanded.')) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2020-09-18 06:05:05 -04:00
|
|
|
const outline = [
|
|
|
|
{
|
|
|
|
title: 'Section',
|
|
|
|
line: 1,
|
2021-04-27 03:52:58 -04:00
|
|
|
level: 10,
|
|
|
|
},
|
2020-09-18 06:05:05 -04:00
|
|
|
]
|
|
|
|
render(
|
|
|
|
<OutlinePane
|
|
|
|
isTexFile
|
|
|
|
outline={outline}
|
|
|
|
jumpToLine={jumpToLine}
|
|
|
|
onToggle={onToggle}
|
|
|
|
eventTracking={eventTracking}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(screen.queryByRole('tree')).to.be.null
|
|
|
|
const collapseButton = screen.getByRole('button', {
|
2021-04-27 03:52:58 -04:00
|
|
|
name: 'Show File outline',
|
2020-09-18 06:05:05 -04:00
|
|
|
})
|
|
|
|
fireEvent.click(collapseButton)
|
|
|
|
|
|
|
|
screen.getByRole('tree')
|
|
|
|
expect(global.localStorage.setItem).to.be.calledOnce
|
|
|
|
expect(global.localStorage.setItem).to.be.calledWithMatch(/123abc/, 'true')
|
|
|
|
expect(onToggle).to.be.calledTwice
|
|
|
|
})
|
|
|
|
})
|