project-name-editable-label.test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { fireEvent, render, screen } from '@testing-library/react'
  4. import ProjectNameEditableLabel from '../../../../../frontend/js/features/editor-navigation-toolbar/components/project-name-editable-label'
  5. describe('<ProjectNameEditableLabel />', function () {
  6. const defaultProps = { projectName: 'test-project', onChange: () => {} }
  7. it('displays the project name', function () {
  8. render(<ProjectNameEditableLabel {...defaultProps} />)
  9. screen.getByText('test-project')
  10. })
  11. describe('when the name is editable', function () {
  12. const editableProps = { ...defaultProps, hasRenamePermissions: true }
  13. it('displays an editable input when the edit button is clicked', function () {
  14. render(<ProjectNameEditableLabel {...editableProps} />)
  15. fireEvent.click(screen.getByRole('button'))
  16. screen.getByRole('textbox')
  17. })
  18. it('displays an editable input when the project name is double clicked', function () {
  19. render(<ProjectNameEditableLabel {...editableProps} />)
  20. fireEvent.doubleClick(screen.getByText('test-project'))
  21. screen.getByRole('textbox')
  22. })
  23. it('calls "onChange" when the project name is updated', function () {
  24. const props = {
  25. ...editableProps,
  26. onChange: sinon.stub(),
  27. }
  28. render(<ProjectNameEditableLabel {...props} />)
  29. fireEvent.doubleClick(screen.getByText('test-project'))
  30. const input = screen.getByRole('textbox')
  31. fireEvent.change(input, { target: { value: 'new project name' } })
  32. fireEvent.keyDown(input, { key: 'Enter' })
  33. expect(props.onChange).to.be.calledWith('new project name')
  34. })
  35. it('calls "onChange" when the input loses focus', function () {
  36. const props = {
  37. ...editableProps,
  38. onChange: sinon.stub(),
  39. }
  40. render(<ProjectNameEditableLabel {...props} />)
  41. fireEvent.doubleClick(screen.getByText('test-project'))
  42. const input = screen.getByRole('textbox')
  43. fireEvent.change(input, { target: { value: 'new project name' } })
  44. fireEvent.blur(screen.getByRole('textbox'))
  45. expect(props.onChange).to.be.calledWith('new project name')
  46. })
  47. })
  48. describe('when the name is not editable', function () {
  49. const nonEditableProps = { hasRenamePermissions: false, ...defaultProps }
  50. it('the edit button is not displayed', function () {
  51. render(<ProjectNameEditableLabel {...nonEditableProps} />)
  52. expect(screen.queryByRole('button')).to.not.exist
  53. })
  54. it('does not display an editable input when the project name is double clicked', function () {
  55. render(<ProjectNameEditableLabel {...nonEditableProps} />)
  56. fireEvent.doubleClick(screen.getByText('test-project'))
  57. expect(screen.queryByRole('textbox')).to.not.exist
  58. })
  59. })
  60. })