register-form.test.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { expect } from 'chai'
  2. import { render, screen, fireEvent } from '@testing-library/react'
  3. import fetchMock from 'fetch-mock'
  4. import sinon from 'sinon'
  5. import RegisterForm from '../../../../frontend/js/components/register-form'
  6. describe('RegisterForm', function () {
  7. beforeEach(function () {
  8. fetchMock.removeRoutes().clearHistory()
  9. })
  10. afterEach(function () {
  11. fetchMock.removeRoutes().clearHistory()
  12. })
  13. it('should render the register form', async function () {
  14. const setRegistrationSuccessStub = sinon.stub()
  15. const setEmailsStub = sinon.stub()
  16. const setRegisterErrorStub = sinon.stub()
  17. const setFailedEmailsStub = sinon.stub()
  18. render(
  19. <RegisterForm
  20. setRegistrationSuccess={setRegistrationSuccessStub}
  21. setEmails={setEmailsStub}
  22. setRegisterError={setRegisterErrorStub}
  23. setFailedEmails={setFailedEmailsStub}
  24. />
  25. )
  26. await screen.findByLabelText('Emails to register new users')
  27. screen.getByRole('button', { name: /register/i })
  28. })
  29. it('should call the fetch request when register button is pressed', async function () {
  30. const email = 'abc@gmail.com'
  31. const setRegistrationSuccessStub = sinon.stub()
  32. const setEmailsStub = sinon.stub()
  33. const setRegisterErrorStub = sinon.stub()
  34. const setFailedEmailsStub = sinon.stub()
  35. const endPointResponse = {
  36. status: 200,
  37. body: {
  38. email,
  39. setNewPasswordUrl: 'SetNewPasswordURL',
  40. },
  41. }
  42. const registerMock = fetchMock.post('/admin/register', endPointResponse)
  43. render(
  44. <RegisterForm
  45. setRegistrationSuccess={setRegistrationSuccessStub}
  46. setEmails={setEmailsStub}
  47. setRegisterError={setRegisterErrorStub}
  48. setFailedEmails={setFailedEmailsStub}
  49. />
  50. )
  51. const registerInput = screen.getByLabelText('Emails to register new users')
  52. const registerButton = screen.getByRole('button', { name: /register/i })
  53. fireEvent.change(registerInput, { target: { value: email } })
  54. fireEvent.click(registerButton)
  55. expect(registerMock.callHistory.called()).to.be.true
  56. })
  57. })