actions-copy-project.test.jsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { fireEvent, screen, waitFor } from '@testing-library/react'
  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 { location } from '@/shared/components/location'
  8. describe('<ActionsCopyProject />', function () {
  9. beforeEach(function () {
  10. this.locationWrapperSandbox = sinon.createSandbox()
  11. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  12. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  13. })
  14. afterEach(function () {
  15. this.locationWrapperSandbox.restore()
  16. fetchMock.removeRoutes().clearHistory()
  17. })
  18. it('shows correct modal when clicked', async function () {
  19. renderWithEditorContext(<ActionsCopyProject />)
  20. fireEvent.click(screen.getByRole('button', { name: 'Copy Project' }))
  21. screen.getByPlaceholderText('New project name')
  22. })
  23. it('loads the project page when submitted', async function () {
  24. fetchMock.post('express:/project/:id/clone', {
  25. status: 200,
  26. body: {
  27. project_id: 'new-project',
  28. },
  29. })
  30. renderWithEditorContext(<ActionsCopyProject />)
  31. fireEvent.click(screen.getByRole('button', { name: 'Copy Project' }))
  32. const input = screen.getByPlaceholderText('New project name')
  33. fireEvent.change(input, { target: { value: 'New Project' } })
  34. const button = screen.getByRole('button', { name: 'Copy' })
  35. button.click()
  36. await waitFor(() => {
  37. expect(button.textContent).to.equal('Copying…')
  38. })
  39. const assignStub = this.locationWrapperStub.assign
  40. await waitFor(() => {
  41. expect(assignStub).to.have.been.calledOnceWith('/project/new-project')
  42. })
  43. })
  44. })