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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 />
  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. />
  20. </FileTreeCreateNameProvider>
  21. )
  22. cy.findByLabelText('File name in this project')
  23. cy.findByPlaceholderText('Enter a file name…')
  24. })
  25. it('uses an initial name', function () {
  26. cy.mount(
  27. <FileTreeCreateNameProvider initialName="test.tex">
  28. <FileTreeCreateNameInput />
  29. </FileTreeCreateNameProvider>
  30. )
  31. cy.findByLabelText('File Name').should('have.value', 'test.tex')
  32. })
  33. it('focuses the name', function () {
  34. cy.spy(window, 'requestAnimationFrame').as('requestAnimationFrame')
  35. cy.mount(
  36. <FileTreeCreateNameProvider initialName="test.tex">
  37. <FileTreeCreateNameInput focusName />
  38. </FileTreeCreateNameProvider>
  39. )
  40. cy.findByLabelText('File Name').as('input')
  41. cy.get('@input').should('have.value', 'test.tex')
  42. cy.get('@requestAnimationFrame').should('have.been.calledOnce')
  43. // https://github.com/jsdom/jsdom/issues/2995
  44. // "window.getSelection doesn't work with selection of <input> element"
  45. // const selection = window.getSelection().toString()
  46. // expect(selection).to.equal('test')
  47. // wait for the selection to update
  48. // eslint-disable-next-line cypress/no-unnecessary-waiting
  49. cy.wait(100)
  50. cy.get<HTMLInputElement>('@input').then(element => {
  51. expect(element.get(0).selectionStart).to.equal(0)
  52. expect(element.get(0).selectionEnd).to.equal(4)
  53. })
  54. })
  55. })