2020-11-26 09:22:30 -05:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import sinon from 'sinon'
|
2021-03-18 05:52:36 -04:00
|
|
|
import { screen, fireEvent, cleanup } from '@testing-library/react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import renderWithContext from '../../helpers/render-with-context'
|
|
|
|
|
|
|
|
import FileTreeItemName from '../../../../../../frontend/js/features/file-tree/components/file-tree-item/file-tree-item-name'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('<FileTreeItemName />', function () {
|
2021-03-18 05:52:36 -04:00
|
|
|
const sandbox = sinon.createSandbox()
|
2021-02-09 04:50:16 -05:00
|
|
|
const setIsDraggable = sinon.stub()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2021-03-18 05:52:36 -04:00
|
|
|
sandbox.spy(window, 'requestAnimationFrame')
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2021-03-18 05:52:36 -04:00
|
|
|
sandbox.restore()
|
2021-02-09 04:50:16 -05:00
|
|
|
setIsDraggable.reset()
|
2021-03-18 05:52:36 -04:00
|
|
|
cleanup()
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders name as button', function () {
|
2021-02-09 04:50:16 -05:00
|
|
|
renderWithContext(
|
|
|
|
<FileTreeItemName
|
|
|
|
name="foo.tex"
|
|
|
|
isSelected
|
|
|
|
setIsDraggable={setIsDraggable}
|
|
|
|
/>
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
screen.getByRole('button', { name: 'foo.tex' })
|
|
|
|
expect(screen.queryByRole('textbox')).to.not.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it("doesn't start renaming on unselected component", function () {
|
2021-02-09 04:50:16 -05:00
|
|
|
renderWithContext(
|
|
|
|
<FileTreeItemName
|
|
|
|
name="foo.tex"
|
|
|
|
isSelected={false}
|
|
|
|
setIsDraggable={setIsDraggable}
|
|
|
|
/>
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const button = screen.queryByRole('button')
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.doubleClick(button)
|
|
|
|
expect(screen.queryByRole('textbox')).to.not.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('start renaming on double-click', function () {
|
2021-02-09 04:50:16 -05:00
|
|
|
renderWithContext(
|
|
|
|
<FileTreeItemName
|
|
|
|
name="foo.tex"
|
|
|
|
isSelected
|
|
|
|
setIsDraggable={setIsDraggable}
|
|
|
|
/>
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const button = screen.queryByRole('button')
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.doubleClick(button)
|
|
|
|
screen.getByRole('textbox')
|
|
|
|
expect(screen.queryByRole('button')).to.not.exist
|
2021-03-18 05:52:36 -04:00
|
|
|
expect(window.requestAnimationFrame).to.be.calledOnce
|
2021-02-09 04:50:16 -05:00
|
|
|
expect(setIsDraggable).to.be.calledWith(false)
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('cannot start renaming in read-only', function () {
|
2021-02-09 04:50:16 -05:00
|
|
|
renderWithContext(
|
|
|
|
<FileTreeItemName
|
|
|
|
name="foo.tex"
|
|
|
|
isSelected
|
|
|
|
setIsDraggable={setIsDraggable}
|
|
|
|
/>,
|
|
|
|
{
|
2022-01-10 10:46:46 -05:00
|
|
|
contextProps: { permissionsLevel: 'readOnly' },
|
2021-02-09 04:50:16 -05:00
|
|
|
}
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const button = screen.queryByRole('button')
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.doubleClick(button)
|
|
|
|
|
|
|
|
expect(screen.queryByRole('textbox')).to.not.exist
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('stop renaming', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-09 04:50:16 -05:00
|
|
|
renderWithContext(
|
|
|
|
<FileTreeItemName
|
|
|
|
name="foo.tex"
|
|
|
|
isSelected
|
|
|
|
setIsDraggable={setIsDraggable}
|
|
|
|
/>
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const button = screen.getByRole('button')
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.click(button)
|
|
|
|
fireEvent.doubleClick(button)
|
|
|
|
|
|
|
|
const input = screen.getByRole('textbox')
|
|
|
|
fireEvent.change(input, { target: { value: 'bar.tex' } })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('on Escape', function () {
|
2020-11-26 09:22:30 -05:00
|
|
|
const input = screen.getByRole('textbox')
|
|
|
|
fireEvent.keyDown(input, { key: 'Escape' })
|
|
|
|
|
|
|
|
screen.getByRole('button', { name: 'foo.tex' })
|
2021-02-09 04:50:16 -05:00
|
|
|
expect(setIsDraggable).to.be.calledWith(true)
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|