Advertisement
hinagawa

Untitled

Sep 23rd, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. render, fireEvent, screen, waitFor,
  4. } from '@testing-library/react';
  5. import { MemoryRouter } from 'react-router-dom';
  6.  
  7. import SignUpPage from '.';
  8.  
  9. const signUpPage = render(
  10. <MemoryRouter>
  11. <SignUpPage />
  12. </MemoryRouter>,
  13. );
  14. const { getByTestId } = signUpPage;
  15.  
  16. const firstNameInput = signUpPage.getByPlaceholderText('First name');
  17. const lastNameInput = signUpPage.getByPlaceholderText('Last name');
  18. const emailInput = signUpPage.getByPlaceholderText('Email');
  19. const passwordInput = signUpPage.getByPlaceholderText('Password(6 digits at least, case sensitive)');
  20. const confirmPasswordInput = signUpPage.getByPlaceholderText('Confirm password');
  21. const submitForm = signUpPage.getByTestId('signUpForm');
  22.  
  23. const mockValues = {
  24. firstName: 'qwe',
  25. lastName: 'qwe',
  26. correctEmail: 'qwe@gmail.com',
  27. incorrectEmail: 'hinagawa@gmail.com',
  28. password: 'Qwerty007',
  29. incorrectConfirmPassword: 'Qwerty000',
  30. confirmPassword: 'Qwerty007',
  31. };
  32.  
  33. it('should render correctly', () => {
  34. const { asFragment } = signUpPage;
  35. expect(asFragment()).toMatchSnapshot();
  36. });
  37.  
  38. it('should call onSubmit 1 time when we submit form', () => {
  39. const handleSubmit = jest.fn();
  40. submitForm.onsubmit = handleSubmit;
  41. fireEvent.submit(submitForm);
  42. expect(handleSubmit).toHaveBeenCalledTimes(1);
  43. });
  44.  
  45. it('should find error message if password and confirm password do not match', async () => {
  46. fireEvent.change(firstNameInput, { target: { value: mockValues.firstName } });
  47. fireEvent.change(lastNameInput, { target: { value: mockValues.lastName } });
  48. fireEvent.change(emailInput, { target: { value: mockValues.correctEmail } });
  49. fireEvent.change(passwordInput, { target: { value: mockValues.password } });
  50. fireEvent.change(
  51. confirmPasswordInput,
  52. { target: { value: mockValues.incorrectConfirmPassword } },
  53. );
  54.  
  55. const handleSubmit = jest.fn();
  56. submitForm.onsubmit = handleSubmit;
  57. await waitFor(() => {
  58. fireEvent.submit(submitForm);
  59. });
  60. expect(screen.queryByText(/Password and confirm password don't match/i)).toBeInTheDocument();
  61. });
  62.  
  63. it('check error handling', () => {
  64. const form = getByTestId('FORM');
  65. expect(screen.queryByText(/Entered passwords do not match!/i)).toBeNull();
  66. fireEvent.change(screen.getByPlaceholderText('Password (6 digits at least, case sensetive)'), {
  67. target: { value: 'TestPassword123!' },
  68. });
  69. fireEvent.change(screen.getByPlaceholderText('Confirm password'), {
  70. target: { value: 'TestPassword123!!' },
  71. });
  72. fireEvent.submit(form);
  73. expect(screen.queryByText(/Entered passwords do not match!/i)).toBeInTheDocument();
  74. });
  75.  
  76. it('should find error message when we pass email which already exist', async () => {
  77. fireEvent.change(firstNameInput, mockValues.firstName);
  78. fireEvent.change(lastNameInput, mockValues.lastName);
  79. fireEvent.change(emailInput, { target: { value: mockValues.incorrectEmail } });
  80. fireEvent.change(passwordInput, { target: { value: mockValues.password } });
  81. fireEvent.change(
  82. confirmPasswordInput,
  83. { target: { value: mockValues.confirmPassword } },
  84. );
  85. await waitFor(() => {
  86. fireEvent.submit(submitForm);
  87. });
  88. expect(screen.getByText('User with this email already exist')).toBeInTheDocument();
  89. });
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement