actions-copy-project.test.jsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { fireEvent, screen } from '@testing-library/dom'
  2. import fetchMock from 'fetch-mock'
  3. import sinon from 'sinon'
  4. import { expect } from 'chai'
  5. import ActionsCopyProject from '../../../../../frontend/js/features/editor-left-menu/components/actions-copy-project'
  6. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  7. import { waitFor } from '@testing-library/react'
  8. import * as useLocationModule from '../../../../../frontend/js/shared/hooks/use-location'
  9. describe('<ActionsCopyProject />', function () {
  10. let assignStub
  11. beforeEach(function () {
  12. assignStub = sinon.stub()
  13. this.locationStub = sinon.stub(useLocationModule, 'useLocation').returns({
  14. assign: assignStub,
  15. replace: sinon.stub(),
  16. reload: sinon.stub(),
  17. })
  18. })
  19. afterEach(function () {
  20. this.locationStub.restore()
  21. fetchMock.reset()
  22. })
  23. it('shows correct modal when clicked', async function () {
  24. renderWithEditorContext(<ActionsCopyProject />)
  25. fireEvent.click(screen.getByRole('button', { name: 'Copy Project' }))
  26. screen.getByPlaceholderText('New Project Name')
  27. })
  28. it('loads the project page when submitted', async function () {
  29. fetchMock.post('express:/project/:id/clone', {
  30. status: 200,
  31. body: {
  32. project_id: 'new-project',
  33. },
  34. })
  35. renderWithEditorContext(<ActionsCopyProject />)
  36. fireEvent.click(screen.getByRole('button', { name: 'Copy Project' }))
  37. const input = screen.getByPlaceholderText('New Project Name')
  38. fireEvent.change(input, { target: { value: 'New Project' } })
  39. const button = screen.getByRole('button', { name: 'Copy' })
  40. button.click()
  41. await waitFor(() => {
  42. expect(button.textContent).to.equal('Copying…')
  43. })
  44. await waitFor(() => {
  45. expect(assignStub).to.have.been.calledOnceWith('/project/new-project')
  46. })
  47. })
  48. })