actions-copy-project.test.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. reload: sinon.stub(),
  16. })
  17. })
  18. afterEach(function () {
  19. this.locationStub.restore()
  20. fetchMock.reset()
  21. })
  22. it('shows correct modal when clicked', async function () {
  23. renderWithEditorContext(<ActionsCopyProject />)
  24. fireEvent.click(screen.getByRole('button', { name: 'Copy Project' }))
  25. screen.getByPlaceholderText('New Project Name')
  26. })
  27. it('loads the project page when submitted', async function () {
  28. fetchMock.post('express:/project/:id/clone', {
  29. status: 200,
  30. body: {
  31. project_id: 'new-project',
  32. },
  33. })
  34. renderWithEditorContext(<ActionsCopyProject />)
  35. fireEvent.click(screen.getByRole('button', { name: 'Copy Project' }))
  36. const input = screen.getByPlaceholderText('New Project Name')
  37. fireEvent.change(input, { target: { value: 'New Project' } })
  38. const button = screen.getByRole('button', { name: 'Copy' })
  39. button.click()
  40. await waitFor(() => {
  41. expect(button.textContent).to.equal('Copying…')
  42. })
  43. await waitFor(() => {
  44. expect(assignStub).to.have.been.calledOnceWith('/project/new-project')
  45. })
  46. })
  47. })