2021-01-14 08:57:33 -05:00
|
|
|
import { expect } from 'chai'
|
2020-11-26 09:22:30 -05:00
|
|
|
import sinon from 'sinon'
|
2021-05-12 06:31:18 -04:00
|
|
|
import { screen, fireEvent, waitFor } from '@testing-library/react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
|
2021-05-12 06:31:18 -04:00
|
|
|
import {
|
|
|
|
renderWithEditorContext,
|
|
|
|
cleanUpContext,
|
|
|
|
} from '../../../helpers/render-with-context'
|
2020-11-26 09:22:30 -05:00
|
|
|
import FileTreeRoot from '../../../../../frontend/js/features/file-tree/components/file-tree-root'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('<FileTreeRoot/>', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const onSelect = sinon.stub()
|
|
|
|
const onInit = sinon.stub()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
global.requestAnimationFrame = sinon.stub()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
delete global.requestAnimationFrame
|
|
|
|
fetchMock.restore()
|
|
|
|
onSelect.reset()
|
|
|
|
onInit.reset()
|
2021-05-12 06:31:18 -04:00
|
|
|
cleanUpContext()
|
2021-01-08 05:03:10 -05:00
|
|
|
global.localStorage.clear()
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
2020-12-09 06:55:36 -05:00
|
|
|
_id: 'root-folder-id',
|
2022-01-10 10:46:46 -05:00
|
|
|
name: 'rootFolder',
|
2020-11-26 09:22:30 -05:00
|
|
|
docs: [{ _id: '456def', name: 'main.tex' }],
|
|
|
|
folders: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
|
|
|
},
|
2020-11-26 09:22:30 -05:00
|
|
|
]
|
2021-05-12 06:31:18 -04:00
|
|
|
const { container } = renderWithEditorContext(
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeRoot
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2020-11-26 09:22:30 -05:00
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
2021-01-14 08:57:33 -05:00
|
|
|
isConnected
|
2022-01-10 10:46:46 -05:00
|
|
|
/>,
|
|
|
|
{
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2022-01-10 10:46:46 -05:00
|
|
|
projectId: '123abc',
|
|
|
|
rootDocId: '456def',
|
|
|
|
features: {},
|
|
|
|
permissionsLevel: 'owner',
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
screen.queryByRole('tree')
|
|
|
|
screen.getByRole('treeitem')
|
|
|
|
screen.getByRole('treeitem', { name: 'main.tex', selected: true })
|
2021-01-14 08:57:33 -05:00
|
|
|
expect(container.querySelector('.disconnected-overlay')).to.not.exist
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders with invalid selected doc in local storage', async function () {
|
2021-01-08 05:03:10 -05:00
|
|
|
global.localStorage.setItem(
|
|
|
|
'doc.open_id.123abc',
|
|
|
|
JSON.stringify('not-a-valid-id')
|
|
|
|
)
|
|
|
|
const rootFolder = [
|
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
2022-01-10 10:46:46 -05:00
|
|
|
name: 'rootFolder',
|
2021-01-08 05:03:10 -05:00
|
|
|
docs: [{ _id: '456def', name: 'main.tex' }],
|
|
|
|
folders: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
|
|
|
},
|
2021-01-08 05:03:10 -05:00
|
|
|
]
|
2021-05-12 06:31:18 -04:00
|
|
|
renderWithEditorContext(
|
2021-01-08 05:03:10 -05:00
|
|
|
<FileTreeRoot
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2021-01-08 05:03:10 -05:00
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
2021-01-14 08:57:33 -05:00
|
|
|
isConnected
|
2022-01-10 10:46:46 -05:00
|
|
|
/>,
|
|
|
|
{
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2022-01-10 10:46:46 -05:00
|
|
|
projectId: '123abc',
|
|
|
|
rootDocId: '456def',
|
|
|
|
features: {},
|
|
|
|
permissionsLevel: 'owner',
|
|
|
|
}
|
2021-01-08 05:03:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// as a proxy to check that the invalid entity ha not been select we start
|
|
|
|
// a delete and ensure the modal is displayed (the cancel button can be
|
|
|
|
// selected) This is needed to make sure the test fail.
|
|
|
|
const treeitemFile = screen.getByRole('treeitem', { name: 'main.tex' })
|
|
|
|
fireEvent.click(treeitemFile, { ctrlKey: true })
|
2021-04-29 04:16:42 -04:00
|
|
|
const toggleButton = screen.getByRole('button', { name: 'Menu' })
|
|
|
|
fireEvent.click(toggleButton)
|
2021-01-08 05:03:10 -05:00
|
|
|
const deleteButton = screen.getByRole('menuitem', { name: 'Delete' })
|
|
|
|
fireEvent.click(deleteButton)
|
|
|
|
await waitFor(() => screen.getByRole('button', { name: 'Cancel' }))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders disconnected overlay', function () {
|
2021-01-14 08:57:33 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
2022-01-10 10:46:46 -05:00
|
|
|
name: 'rootFolder',
|
2021-01-14 08:57:33 -05:00
|
|
|
docs: [{ _id: '456def', name: 'main.tex' }],
|
|
|
|
folders: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
|
|
|
},
|
2021-01-14 08:57:33 -05:00
|
|
|
]
|
|
|
|
|
2021-05-12 06:31:18 -04:00
|
|
|
const { container } = renderWithEditorContext(
|
2021-01-14 08:57:33 -05:00
|
|
|
<FileTreeRoot
|
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
|
|
|
isConnected={false}
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2022-01-10 10:46:46 -05:00
|
|
|
/>,
|
|
|
|
{
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2022-01-10 10:46:46 -05:00
|
|
|
projectId: '123abc',
|
|
|
|
rootDocId: '456def',
|
|
|
|
features: {},
|
|
|
|
permissionsLevel: 'owner',
|
|
|
|
}
|
2021-01-14 08:57:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(container.querySelector('.disconnected-overlay')).to.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('fire onSelect', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
2020-12-09 06:55:36 -05:00
|
|
|
_id: 'root-folder-id',
|
2022-01-10 10:46:46 -05:00
|
|
|
name: 'rootFolder',
|
2020-11-26 09:22:30 -05:00
|
|
|
docs: [
|
|
|
|
{ _id: '456def', name: 'main.tex' },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: '789ghi', name: 'other.tex' },
|
2020-11-26 09:22:30 -05:00
|
|
|
],
|
|
|
|
folders: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
|
|
|
},
|
2020-11-26 09:22:30 -05:00
|
|
|
]
|
2021-05-12 06:31:18 -04:00
|
|
|
renderWithEditorContext(
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeRoot
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2020-11-26 09:22:30 -05:00
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
2021-01-14 08:57:33 -05:00
|
|
|
isConnected
|
2022-01-10 10:46:46 -05:00
|
|
|
/>,
|
|
|
|
{
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2022-01-10 10:46:46 -05:00
|
|
|
projectId: '123abc',
|
|
|
|
rootDocId: '456def',
|
|
|
|
features: {},
|
|
|
|
permissionsLevel: 'readOnly',
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
sinon.assert.calledOnce(onSelect)
|
|
|
|
sinon.assert.calledWithMatch(onSelect, [
|
|
|
|
sinon.match({
|
|
|
|
entity: {
|
|
|
|
_id: '456def',
|
2021-04-27 03:52:58 -04:00
|
|
|
name: 'main.tex',
|
|
|
|
},
|
|
|
|
}),
|
2020-11-26 09:22:30 -05:00
|
|
|
])
|
|
|
|
onSelect.reset()
|
|
|
|
|
|
|
|
screen.queryByRole('tree')
|
|
|
|
const treeitem = screen.getByRole('treeitem', { name: 'other.tex' })
|
|
|
|
fireEvent.click(treeitem)
|
|
|
|
sinon.assert.calledOnce(onSelect)
|
|
|
|
sinon.assert.calledWithMatch(onSelect, [
|
|
|
|
sinon.match({
|
|
|
|
entity: {
|
|
|
|
_id: '789ghi',
|
2021-04-27 03:52:58 -04:00
|
|
|
name: 'other.tex',
|
|
|
|
},
|
|
|
|
}),
|
2020-11-26 09:22:30 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('listen to editor.openDoc', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
2020-12-09 06:55:36 -05:00
|
|
|
_id: 'root-folder-id',
|
2022-01-10 10:46:46 -05:00
|
|
|
name: 'rootFolder',
|
2020-11-26 09:22:30 -05:00
|
|
|
docs: [
|
|
|
|
{ _id: '456def', name: 'main.tex' },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: '789ghi', name: 'other.tex' },
|
2020-11-26 09:22:30 -05:00
|
|
|
],
|
|
|
|
folders: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
|
|
|
},
|
2020-11-26 09:22:30 -05:00
|
|
|
]
|
2021-05-12 06:31:18 -04:00
|
|
|
renderWithEditorContext(
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeRoot
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2020-11-26 09:22:30 -05:00
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
2021-01-14 08:57:33 -05:00
|
|
|
isConnected
|
2022-01-10 10:46:46 -05:00
|
|
|
/>,
|
|
|
|
{
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2022-01-10 10:46:46 -05:00
|
|
|
projectId: '123abc',
|
|
|
|
rootDocId: '456def',
|
|
|
|
features: {},
|
|
|
|
permissionsLevel: 'owner',
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'main.tex', selected: true })
|
|
|
|
|
2021-01-27 04:54:29 -05:00
|
|
|
// entities not found should be ignored
|
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent('editor.openDoc', { detail: 'not-an-id' })
|
|
|
|
)
|
|
|
|
screen.getByRole('treeitem', { name: 'main.tex', selected: true })
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent('editor.openDoc', { detail: '789ghi' })
|
|
|
|
)
|
|
|
|
screen.getByRole('treeitem', { name: 'main.tex', selected: false })
|
|
|
|
screen.getByRole('treeitem', { name: 'other.tex', selected: true })
|
|
|
|
})
|
2021-06-03 09:45:05 -04:00
|
|
|
|
|
|
|
it('only shows a menu button when a single item is selected', function () {
|
|
|
|
const rootFolder = [
|
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
2022-01-10 10:46:46 -05:00
|
|
|
name: 'rootFolder',
|
2021-06-03 09:45:05 -04:00
|
|
|
docs: [
|
|
|
|
{ _id: '456def', name: 'main.tex' },
|
|
|
|
{ _id: '789ghi', name: 'other.tex' },
|
|
|
|
],
|
|
|
|
folders: [],
|
|
|
|
fileRefs: [],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
renderWithEditorContext(
|
|
|
|
<FileTreeRoot
|
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
|
|
|
isConnected
|
2022-01-10 10:46:46 -05:00
|
|
|
/>,
|
|
|
|
{
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2022-01-10 10:46:46 -05:00
|
|
|
projectId: '123abc',
|
|
|
|
rootDocId: '456def',
|
|
|
|
features: {},
|
|
|
|
permissionsLevel: 'owner',
|
|
|
|
}
|
2021-06-03 09:45:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const main = screen.getByRole('treeitem', {
|
|
|
|
name: 'main.tex',
|
|
|
|
selected: true,
|
|
|
|
})
|
|
|
|
const other = screen.getByRole('treeitem', {
|
|
|
|
name: 'other.tex',
|
|
|
|
selected: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
// single item selected: menu button is visible
|
|
|
|
expect(screen.queryAllByRole('button', { name: 'Menu' })).to.have.length(1)
|
|
|
|
|
|
|
|
// select the other item
|
|
|
|
fireEvent.click(other)
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'main.tex', selected: false })
|
|
|
|
screen.getByRole('treeitem', { name: 'other.tex', selected: true })
|
|
|
|
|
|
|
|
// single item selected: menu button is visible
|
|
|
|
expect(screen.queryAllByRole('button', { name: 'Menu' })).to.have.length(1)
|
|
|
|
|
|
|
|
// multi-select the main item
|
|
|
|
fireEvent.click(main, { ctrlKey: true })
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'main.tex', selected: true })
|
|
|
|
screen.getByRole('treeitem', { name: 'other.tex', selected: true })
|
|
|
|
|
|
|
|
// multiple items selected: no menu button is visible
|
|
|
|
expect(screen.queryAllByRole('button', { name: 'Menu' })).to.have.length(0)
|
|
|
|
})
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|