file-tree-create-name-input.spec.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import FileTreeCreateNameInput from '../../../../../../frontend/js/features/file-tree/components/file-tree-create/file-tree-create-name-input'
  2. import FileTreeCreateNameProvider from '../../../../../../frontend/js/features/file-tree/contexts/file-tree-create-name'
  3. describe('<FileTreeCreateNameInput/>', function () {
  4. it('renders an empty input', function () {
  5. cy.mount(
  6. <FileTreeCreateNameProvider>
  7. <FileTreeCreateNameInput inFlight={false} />
  8. </FileTreeCreateNameProvider>
  9. )
  10. cy.findByLabelText('File Name')
  11. cy.findByPlaceholderText('File Name')
  12. })
  13. it('renders a custom label and placeholder', function () {
  14. cy.mount(
  15. <FileTreeCreateNameProvider>
  16. <FileTreeCreateNameInput
  17. label="File name in this project"
  18. placeholder="Enter a file name…"
  19. inFlight={false}
  20. />
  21. </FileTreeCreateNameProvider>
  22. )
  23. cy.findByLabelText('File name in this project')
  24. cy.findByPlaceholderText('Enter a file name…')
  25. })
  26. it('uses an initial name', function () {
  27. cy.mount(
  28. <FileTreeCreateNameProvider initialName="test.tex">
  29. <FileTreeCreateNameInput inFlight={false} />
  30. </FileTreeCreateNameProvider>
  31. )
  32. cy.findByLabelText('File Name').should('have.value', 'test.tex')
  33. })
  34. it('focuses the name', function () {
  35. cy.spy(window, 'requestAnimationFrame').as('requestAnimationFrame')
  36. cy.mount(
  37. <FileTreeCreateNameProvider initialName="test.tex">
  38. <FileTreeCreateNameInput focusName inFlight={false} />
  39. </FileTreeCreateNameProvider>
  40. )
  41. cy.findByLabelText('File Name').as('input')
  42. cy.get('@input').should('have.value', 'test.tex')
  43. cy.get('@requestAnimationFrame').should('have.been.calledOnce')
  44. // https://github.com/jsdom/jsdom/issues/2995
  45. // "window.getSelection doesn't work with selection of <input> element"
  46. // const selection = window.getSelection().toString()
  47. // expect(selection).to.equal('test')
  48. // wait for the selection to update
  49. // eslint-disable-next-line cypress/no-unnecessary-waiting
  50. cy.wait(100)
  51. cy.get<HTMLInputElement>('@input').then(element => {
  52. expect(element.get(0).selectionStart).to.equal(0)
  53. expect(element.get(0).selectionEnd).to.equal(4)
  54. })
  55. })
  56. it('disables the input when in flight', function () {
  57. cy.mount(
  58. <FileTreeCreateNameProvider initialName="test.tex">
  59. <FileTreeCreateNameInput inFlight={false} />
  60. </FileTreeCreateNameProvider>
  61. ).then(({ rerender }) => {
  62. cy.findByLabelText('File Name').should('not.be.disabled')
  63. rerender(
  64. <FileTreeCreateNameProvider initialName="test.tex">
  65. <FileTreeCreateNameInput inFlight />
  66. </FileTreeCreateNameProvider>
  67. )
  68. cy.findByLabelText('File Name').should('be.disabled')
  69. rerender(
  70. <FileTreeCreateNameProvider initialName="test.tex">
  71. <FileTreeCreateNameInput inFlight={false} />
  72. </FileTreeCreateNameProvider>
  73. )
  74. cy.findByLabelText('File Name').should('not.be.disabled')
  75. })
  76. })
  77. })