2020-11-26 09:22:30 -05:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import sinon from 'sinon'
|
2021-05-12 06:31:18 -04:00
|
|
|
import { screen, fireEvent } 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 Rename Entity 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 () {
|
2022-03-31 07:22:36 -04:00
|
|
|
window.metaAttributesCache = new Map()
|
|
|
|
window.metaAttributesCache.set('ol-user', { id: 'user1' })
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
fetchMock.restore()
|
|
|
|
onSelect.reset()
|
|
|
|
onInit.reset()
|
2021-05-12 06:31:18 -04:00
|
|
|
cleanUpContext()
|
2022-03-31 07:22:36 -04:00
|
|
|
window.metaAttributesCache = new Map()
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(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: 'a.tex' }],
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
_id: '987jkl',
|
|
|
|
name: 'folder',
|
|
|
|
docs: [],
|
2020-12-09 06:55:36 -05:00
|
|
|
fileRefs: [
|
|
|
|
{ _id: '789ghi', name: 'c.tex' },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: '981gkp', name: 'e.tex' },
|
2020-12-09 06:55:36 -05:00
|
|
|
],
|
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
|
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
|
|
|
/>,
|
2022-01-10 10:46:46 -05:00
|
|
|
{
|
|
|
|
socket: new MockedSocket(),
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2022-01-10 10:46:46 -05:00
|
|
|
projectId: '123abc',
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
2021-12-14 08:24:41 -05:00
|
|
|
onSelect.reset()
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renames doc', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const fetchMatcher = /\/project\/\w+\/doc\/\w+\/rename/
|
|
|
|
fetchMock.post(fetchMatcher, 204)
|
|
|
|
|
|
|
|
const input = initItemRename('a.tex')
|
|
|
|
fireEvent.change(input, { target: { value: 'b.tex' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'b.tex' })
|
|
|
|
|
|
|
|
const lastFetchBody = getLastFetchBody(fetchMatcher)
|
|
|
|
expect(lastFetchBody.name).to.equal('b.tex')
|
2021-12-14 08:24:41 -05:00
|
|
|
|
|
|
|
// onSelect should have been called once only: when the doc was selected for
|
|
|
|
// rename
|
|
|
|
sinon.assert.calledOnce(onSelect)
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renames folder', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const fetchMatcher = /\/project\/\w+\/folder\/\w+\/rename/
|
|
|
|
fetchMock.post(fetchMatcher, 204)
|
|
|
|
|
|
|
|
const input = initItemRename('folder')
|
|
|
|
fireEvent.change(input, { target: { value: 'new folder name' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'new folder name' })
|
|
|
|
|
|
|
|
const lastFetchBody = getLastFetchBody(fetchMatcher)
|
|
|
|
expect(lastFetchBody.name).to.equal('new folder name')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renames file in subfolder', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const fetchMatcher = /\/project\/\w+\/file\/\w+\/rename/
|
|
|
|
fetchMock.post(fetchMatcher, 204)
|
|
|
|
|
2021-01-07 09:22:40 -05:00
|
|
|
const expandButton = screen.queryByRole('button', { name: 'Expand' })
|
|
|
|
if (expandButton) fireEvent.click(expandButton)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const input = initItemRename('c.tex')
|
|
|
|
fireEvent.change(input, { target: { value: 'd.tex' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'folder' })
|
|
|
|
screen.getByRole('treeitem', { name: 'd.tex' })
|
|
|
|
|
|
|
|
const lastFetchBody = getLastFetchBody(fetchMatcher)
|
|
|
|
expect(lastFetchBody.name).to.equal('d.tex')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('reverts rename on error', async function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const fetchMatcher = /\/project\/\w+\/doc\/\w+\/rename/
|
|
|
|
fetchMock.post(fetchMatcher, 500)
|
|
|
|
|
|
|
|
const input = initItemRename('a.tex')
|
|
|
|
fireEvent.change(input, { target: { value: 'b.tex' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'b.tex' })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('shows error modal on invalid filename', async function () {
|
2020-12-09 06:55:36 -05:00
|
|
|
const input = initItemRename('a.tex')
|
|
|
|
fireEvent.change(input, { target: { value: '///' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
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-04-14 09:17:21 -04:00
|
|
|
it('shows error modal on duplicate filename', async function () {
|
2020-12-09 06:55:36 -05:00
|
|
|
const input = initItemRename('a.tex')
|
|
|
|
fireEvent.change(input, { target: { value: 'folder' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('shows error modal on duplicate filename in subfolder', async function () {
|
2021-01-07 09:22:40 -05:00
|
|
|
const expandButton = screen.queryByRole('button', { name: 'Expand' })
|
|
|
|
if (expandButton) fireEvent.click(expandButton)
|
2020-12-09 06:55:36 -05:00
|
|
|
|
|
|
|
const input = initItemRename('c.tex')
|
|
|
|
fireEvent.change(input, { target: { value: 'e.tex' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('shows error modal on blocked filename', async function () {
|
2021-01-14 08:57:20 -05:00
|
|
|
const input = initItemRename('a.tex')
|
|
|
|
fireEvent.change(input, { target: { value: 'prototype' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
|
|
|
|
await screen.findByRole('alert', {
|
|
|
|
name: 'This file name is blocked.',
|
2021-04-27 03:52:58 -04:00
|
|
|
hidden: true,
|
2021-01-14 08:57:20 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('via socket event', function () {
|
|
|
|
it('renames doc', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
screen.getByRole('treeitem', { name: 'a.tex' })
|
|
|
|
|
|
|
|
window._ide.socket.socketClient.emit(
|
|
|
|
'reciveEntityRename',
|
|
|
|
'456def',
|
|
|
|
'socket.tex'
|
|
|
|
)
|
|
|
|
|
|
|
|
screen.getByRole('treeitem', { name: 'socket.tex' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function initItemRename(treeitemName) {
|
|
|
|
const treeitem = screen.getByRole('treeitem', { name: treeitemName })
|
|
|
|
fireEvent.click(treeitem)
|
|
|
|
|
2021-04-29 04:16:42 -04:00
|
|
|
const toggleButton = screen.getByRole('button', { name: 'Menu' })
|
|
|
|
fireEvent.click(toggleButton)
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
const renameButton = screen.getByRole('menuitem', { name: 'Rename' })
|
|
|
|
fireEvent.click(renameButton)
|
|
|
|
|
|
|
|
return screen.getByRole('textbox')
|
|
|
|
}
|
|
|
|
function getLastFetchBody(matcher) {
|
|
|
|
const [, { body }] = fetchMock.lastCall(matcher)
|
|
|
|
return JSON.parse(body)
|
|
|
|
}
|
|
|
|
})
|