2020-11-26 09:22:30 -05:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import React from 'react'
|
|
|
|
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'
|
|
|
|
import MockedSocket from 'socket.io-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('FileTree Create Folder Flow', 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()
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('add to root when no files are selected', async function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
|
|
|
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
|
|
|
renderWithEditorContext(
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeRoot
|
|
|
|
rootFolder={rootFolder}
|
|
|
|
projectId="123abc"
|
|
|
|
hasWritePermissions
|
2021-03-24 06:06:56 -04:00
|
|
|
userHasFeature={() => true}
|
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
|
2021-05-12 06:31:18 -04:00
|
|
|
/>,
|
|
|
|
{ socket: new MockedSocket() }
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const newFolderName = 'Foo Bar In Root'
|
|
|
|
const matcher = /\/project\/\w+\/folder/
|
|
|
|
const response = {
|
|
|
|
folders: [],
|
|
|
|
fileRefs: [],
|
|
|
|
docs: [],
|
|
|
|
_id: fakeId(),
|
2021-04-27 03:52:58 -04:00
|
|
|
name: newFolderName,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
fetchMock.post(matcher, response)
|
|
|
|
|
2021-01-05 05:57:45 -05:00
|
|
|
await fireCreateFolder(newFolderName)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const lastCallBody = JSON.parse(fetchMock.lastCall(matcher)[1].body)
|
|
|
|
expect(lastCallBody.name).to.equal(newFolderName)
|
|
|
|
expect(lastCallBody.parent_folder_id).to.equal('root-folder-id')
|
|
|
|
|
|
|
|
window._ide.socket.socketClient.emit('reciveNewFolder', 'root-folder-id', {
|
|
|
|
_id: fakeId(),
|
|
|
|
name: newFolderName,
|
|
|
|
docs: [],
|
|
|
|
fileRefs: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: [],
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
await screen.findByRole('treeitem', { name: newFolderName })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('add to folder from folder', async function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
|
|
|
docs: [],
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
_id: '789ghi',
|
|
|
|
name: 'thefolder',
|
|
|
|
docs: [],
|
|
|
|
fileRefs: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: [],
|
|
|
|
},
|
2020-11-26 09:22:30 -05:00
|
|
|
],
|
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
|
|
|
|
rootFolder={rootFolder}
|
|
|
|
projectId="123abc"
|
|
|
|
hasWritePermissions
|
2021-03-24 06:06:56 -04:00
|
|
|
userHasFeature={() => true}
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2020-11-26 09:22:30 -05:00
|
|
|
rootDocId="789ghi"
|
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
2021-01-14 08:57:33 -05:00
|
|
|
isConnected
|
2021-05-12 06:31:18 -04:00
|
|
|
/>,
|
|
|
|
{ socket: new MockedSocket() }
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const expandButton = screen.getByRole('button', { name: 'Expand' })
|
|
|
|
fireEvent.click(expandButton)
|
|
|
|
|
|
|
|
const newFolderName = 'Foo Bar In thefolder'
|
|
|
|
const matcher = /\/project\/\w+\/folder/
|
|
|
|
const response = {
|
|
|
|
folders: [],
|
|
|
|
fileRefs: [],
|
|
|
|
docs: [],
|
|
|
|
_id: fakeId(),
|
2021-04-27 03:52:58 -04:00
|
|
|
name: newFolderName,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
fetchMock.post(matcher, response)
|
|
|
|
|
2021-01-05 05:57:45 -05:00
|
|
|
await fireCreateFolder(newFolderName)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const lastCallBody = JSON.parse(fetchMock.lastCall(matcher)[1].body)
|
|
|
|
expect(lastCallBody.name).to.equal(newFolderName)
|
|
|
|
expect(lastCallBody.parent_folder_id).to.equal('789ghi')
|
|
|
|
|
|
|
|
window._ide.socket.socketClient.emit('reciveNewFolder', '789ghi', {
|
|
|
|
_id: fakeId(),
|
|
|
|
name: newFolderName,
|
|
|
|
docs: [],
|
|
|
|
fileRefs: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: [],
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// find the created folder
|
|
|
|
await screen.findByRole('treeitem', { name: newFolderName })
|
|
|
|
|
|
|
|
// collapse the parent folder; created folder should not be rendered anymore
|
|
|
|
fireEvent.click(expandButton)
|
|
|
|
expect(screen.queryByRole('treeitem', { name: newFolderName })).to.not.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('add to folder from child', async function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
|
|
|
docs: [],
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
_id: '789ghi',
|
|
|
|
name: 'thefolder',
|
|
|
|
docs: [],
|
|
|
|
fileRefs: [{ _id: '456def', name: 'sub.tex' }],
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: [],
|
|
|
|
},
|
2020-11-26 09:22:30 -05:00
|
|
|
],
|
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
|
|
|
|
rootFolder={rootFolder}
|
|
|
|
projectId="123abc"
|
|
|
|
hasWritePermissions
|
2021-03-24 06:06:56 -04:00
|
|
|
userHasFeature={() => true}
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2020-11-26 09:22:30 -05:00
|
|
|
rootDocId="456def"
|
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
2021-01-14 08:57:33 -05:00
|
|
|
isConnected
|
2021-05-12 06:31:18 -04:00
|
|
|
/>,
|
|
|
|
{ socket: new MockedSocket() }
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const newFolderName = 'Foo Bar In thefolder'
|
|
|
|
const matcher = /\/project\/\w+\/folder/
|
|
|
|
const response = {
|
|
|
|
folders: [],
|
|
|
|
fileRefs: [],
|
|
|
|
docs: [],
|
|
|
|
_id: fakeId(),
|
2021-04-27 03:52:58 -04:00
|
|
|
name: newFolderName,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
fetchMock.post(matcher, response)
|
|
|
|
|
2021-01-05 05:57:45 -05:00
|
|
|
await fireCreateFolder(newFolderName)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const lastCallBody = JSON.parse(fetchMock.lastCall(matcher)[1].body)
|
|
|
|
expect(lastCallBody.name).to.equal(newFolderName)
|
|
|
|
expect(lastCallBody.parent_folder_id).to.equal('789ghi')
|
|
|
|
|
|
|
|
window._ide.socket.socketClient.emit('reciveNewFolder', '789ghi', {
|
|
|
|
_id: fakeId(),
|
|
|
|
name: newFolderName,
|
|
|
|
docs: [],
|
|
|
|
fileRefs: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: [],
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// find the created folder
|
|
|
|
await screen.findByRole('treeitem', { name: newFolderName })
|
|
|
|
|
|
|
|
// collapse the parent folder; created folder should not be rendered anymore
|
2021-01-07 09:22:40 -05:00
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Collapse' }))
|
2020-11-26 09:22:30 -05:00
|
|
|
expect(screen.queryByRole('treeitem', { name: newFolderName })).to.not.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('prevents adding duplicate or invalid names', async function () {
|
2020-12-09 06:55:36 -05:00
|
|
|
const rootFolder = [
|
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
|
|
|
docs: [{ _id: '456def', name: 'existingFile' }],
|
|
|
|
folders: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
|
|
|
},
|
2020-12-09 06:55:36 -05:00
|
|
|
]
|
2021-05-12 06:31:18 -04:00
|
|
|
renderWithEditorContext(
|
2020-12-09 06:55:36 -05:00
|
|
|
<FileTreeRoot
|
|
|
|
rootFolder={rootFolder}
|
|
|
|
projectId="123abc"
|
|
|
|
hasWritePermissions
|
2021-03-24 06:06:56 -04:00
|
|
|
userHasFeature={() => true}
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={{}}
|
|
|
|
reindexReferences={() => null}
|
|
|
|
setRefProviderEnabled={() => null}
|
|
|
|
setStartedFreeTrial={() => null}
|
2020-12-09 06:55:36 -05:00
|
|
|
rootDocId="456def"
|
|
|
|
onSelect={onSelect}
|
|
|
|
onInit={onInit}
|
2021-01-14 08:57:33 -05:00
|
|
|
isConnected
|
2021-05-12 06:31:18 -04:00
|
|
|
/>,
|
|
|
|
{ socket: new MockedSocket() }
|
2020-12-09 06:55:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var newFolderName = 'existingFile'
|
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
await fireCreateFolder(newFolderName)
|
2020-12-09 06:55:36 -05:00
|
|
|
|
|
|
|
expect(fetchMock.called()).to.be.false
|
|
|
|
|
|
|
|
await screen.findByRole('alert', {
|
|
|
|
name: 'A file or folder with this name already exists',
|
2021-04-27 03:52:58 -04:00
|
|
|
hidden: true,
|
2020-12-09 06:55:36 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
newFolderName = 'in/valid '
|
|
|
|
setFolderName(newFolderName)
|
|
|
|
await screen.findByRole('alert', {
|
|
|
|
name: 'File name is empty or contains invalid characters',
|
2021-04-27 03:52:58 -04:00
|
|
|
hidden: true,
|
2020-12-09 06:55:36 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-05 05:57:45 -05:00
|
|
|
async function fireCreateFolder(name) {
|
2020-11-26 09:22:30 -05:00
|
|
|
const createFolderButton = screen.getByRole('button', {
|
2021-04-27 03:52:58 -04:00
|
|
|
name: 'New Folder',
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
fireEvent.click(createFolderButton)
|
|
|
|
|
2020-12-09 06:55:36 -05:00
|
|
|
setFolderName(name)
|
|
|
|
|
2021-01-05 05:57:45 -05:00
|
|
|
const modalCreateButton = await getModalCreateButton()
|
2020-12-09 06:55:36 -05:00
|
|
|
fireEvent.click(modalCreateButton)
|
|
|
|
}
|
|
|
|
|
|
|
|
function setFolderName(name) {
|
2021-01-05 05:57:45 -05:00
|
|
|
const input = screen.getByRole('textbox')
|
2020-11-26 09:22:30 -05:00
|
|
|
fireEvent.change(input, { target: { value: name } })
|
|
|
|
}
|
|
|
|
|
|
|
|
function fakeId() {
|
2021-04-14 09:17:21 -04:00
|
|
|
return Math.random().toString(16).replace(/0\./, 'random-test-id-')
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
2021-01-05 05:57:45 -05:00
|
|
|
async function getModalCreateButton() {
|
|
|
|
return waitFor(() => screen.getByRole('button', { name: 'Create' }))
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
})
|