Advertisement
Guest User

Untitled

a guest
Mar 17th, 2022
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ########################################
  2. #
  3. # /page/signUp/SignUpForm.jsx
  4. #
  5. #########################################
  6. import React from "react";
  7. import { useFormik } from "formik";
  8. import schema from "./formik.schema";
  9. import initialValues from "./formik.initialValues";
  10. import { onSubmit } from "./formik.submit";
  11.  
  12. const SignUpForm = () => {
  13.   const formik = useFormik({
  14.     initialValues: initialValues,
  15.     validationSchema: schema,
  16.     onSubmit: onSubmit,
  17.   });
  18.  
  19.   return (
  20.     <form onSubmit={formik.handleSubmit}>
  21.       <label htmlFor="email">Email</label>
  22.       <input
  23.         id="email"
  24.         name="email"
  25.         type="email"
  26.         onChange={formik.handleChange}
  27.         value={formik.values.email}
  28.       />
  29.       {formik.errors.email ? <div>{formik.errors.email}</div> : null}
  30.  
  31.       <label htmlFor="firstName">First Name</label>
  32.  
  33.       <input
  34.         id="firstName"
  35.         name="firstName"
  36.         type="text"
  37.         onChange={formik.handleChange}
  38.         value={formik.values.firstName}
  39.       />
  40.       {formik.errors.firstName ? <div>{formik.errors.firstName}</div> : null}
  41.  
  42.       <label htmlFor="lastName">Last Name</label>
  43.       <input
  44.         id="lastName"
  45.         name="lastName"
  46.         type="text"
  47.         onChange={formik.handleChange}
  48.         value={formik.values.lastName}
  49.       />
  50.       {formik.errors.lastName ? <div>{formik.errors.lastName}</div> : null}
  51.  
  52.       <label htmlFor="password">password</label>
  53.       <input
  54.         id="password"
  55.         name="password"
  56.         type="password"
  57.         onChange={formik.handleChange}
  58.         value={formik.values.password}
  59.       />
  60.       {formik.errors.password ? <div>{formik.errors.password}</div> : null}
  61.  
  62.       <button type="submit">Submit</button>
  63.     </form>
  64.   );
  65. };
  66.  
  67. export default SignUpForm;
  68.  
  69.  
  70. ########################################
  71. #
  72. # /page/signUp/formik.submit.js
  73. #
  74. #########################################
  75.  
  76. export const onSubmit = (values) => {
  77.   console.log(values);
  78. };
  79.  
  80.  
  81.  
  82. ########################################
  83. #
  84. # /page/signUp/signUp.test.js
  85. #
  86. #########################################
  87.  
  88. import React from "react";
  89. import { render, screen, waitFor } from "@testing-library/react";
  90. import userEvent from "@testing-library/user-event";
  91. import SignUpForm from "./signUpForm";
  92. import * as onSubmit from "./formik.submit";
  93.  
  94. test("renderiog signUp form", async () => {
  95.   render(<SignUpForm />);
  96.  
  97.   const email = screen.getByLabelText(/email/i);
  98.   const firstName = screen.getByLabelText(/first name/i);
  99.   const lastName = screen.getByLabelText(/last name/i);
  100.   const password = screen.getByLabelText(/password/i);
  101.  
  102.   expect(email).toBeInTheDocument();
  103.   expect(firstName).toBeInTheDocument();
  104.   expect(lastName).toBeInTheDocument();
  105.   expect(password).toBeInTheDocument();
  106. });
  107.  
  108. test("submitting signUp form", async () => {
  109.   jest.spyOn(onSubmit, "onSubmit");
  110.   render(<SignUpForm />);
  111.   userEvent.type(screen.getByLabelText(/Email/i), "[email protected]");
  112.   userEvent.type(screen.getByLabelText(/First Name/i), "John");
  113.   userEvent.type(screen.getByLabelText(/Last Name/i), "Dee");
  114.   userEvent.type(screen.getByLabelText(/password/i), "password");
  115.  
  116.   userEvent.click(screen.getByRole("button", { name: /Submit/i }));
  117.  
  118.   await waitFor(() => expect(onSubmit.onSubmit).toHaveBeenCalled());
  119. });
  120.  
  121.  
  122. ######################################
  123. #
  124. # Error when running the test
  125. #
  126. #####################################
  127.  FAIL  src/pages/signUp/signUp.test.js
  128.   ✓ renderiog signUp form (73 ms)
  129.   ✕ submitting signUp form (524 ms)
  130.  
  131.   ● submitting signUp form
  132.  
  133.     expect(jest.fn()).toHaveBeenCalledWith(...expected)
  134.  
  135.     - Expected
  136.     + Received
  137.  
  138.       {"email": "[email protected]", "firstName": "John", "lastName": "Dee", "password": "password"},
  139.     + {"resetForm": [Function anonymous], "setErrors": [Function anonymous], "setFieldError": [Function anonymous], "setFieldTouched": [Function anonymous], "setFieldValue": [Function anonymous], "setFormikState": [Function anonymous], "setStatus": [Function anonymous], "setSubmitting": [Function anonymous], "setTouched": [Function anonymous], "setValues": [Function anonymous], "submitForm": [Function anonymous], "validateField": [Function anonymous], "validateForm": [Function anonymous]},
  140.  
  141.     Number of calls: 1
  142.  
  143.       31 |   await waitFor(() => expect(onSubmit.onSubmit).toHaveBeenCalled());
  144.       32 |
  145.     > 33 |   expect(onSubmit.onSubmit).toHaveBeenCalledWith({
  146.          |                             ^
  147.       34 |     email: "[email protected]",
  148.       35 |     firstName: "John",
  149.       36 |     lastName: "Dee",
  150.  
  151.       at Object.<anonymous> (src/pages/signUp/signUp.test.js:33:29)
  152.  
  153. Test Suites: 1 failed, 1 total
  154. Tests:       1 failed, 1 passed, 2 total
  155. Snapshots:   0 total
  156. Time:        2.952 s, estimated 3 s
  157. Ran all test suites related to changed files.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement