Advertisement
Kenedy-Hernique

create-new-user.spec.js AFTER

Jan 28th, 2021
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import createNewUserUseCase  from '../create-new-user.usecase';
  2. import User from "../../../models/User";
  3. import "regenerator-runtime/runtime.js";
  4. jest.mock("../../../models/User");
  5.  
  6.  
  7. describe("Create new user usecase test", () => {
  8.     const resMock = {
  9.         status: jest.fn(() => resMock),
  10.         send: jest.fn(() => resMock)
  11.     };
  12.     const reqMock = {
  13.         body: {
  14.             name: "name",
  15.             email: "email",
  16.             role: "role",
  17.             password: "123456",
  18.             password_confirmation: "123456"
  19.         }
  20.     }
  21.  
  22.     it(`It should:
  23.     - Call findOne method to verify if a user user with the same email already exists
  24.     - Call the constructor of the User model with the right parameters
  25.     - Call the User prototype save method `, async () => {
  26.  
  27.         await createNewUserUseCase(reqMock, resMock);
  28.  
  29.         expect(User.findOne).toHaveBeenCalledTimes(1);
  30.  
  31.         expect(User).toHaveBeenCalledTimes(1);
  32.         expect(User.mock.calls[0][0].name).toBe('name');
  33.         expect(User.mock.calls[0][0].email).toBe('email');
  34.         expect(User.mock.calls[0][0].role).toBe('role');
  35.         expect(User.mock.calls[0][0].created_at).toBeInstanceOf(Date);
  36.         expect(User.prototype.save).toHaveBeenCalledTimes(1);
  37.  
  38.         expect(resMock.status).toHaveBeenCalledTimes(1);
  39.         expect(resMock.status.mock.calls[0][0]).toBe(201);
  40.         expect(resMock.send).toHaveBeenCalledTimes(1);
  41.     });
  42.  
  43.     it(`It should:
  44.     - Fail to save the user
  45.     - Response obj send error responses`, async () => {
  46.         User.prototype.save.mockImplementationOnce(() => Promise.reject('Fail to save the user'));
  47.  
  48.         await createNewUserUseCase(reqMock, resMock);
  49.  
  50.         expect(resMock.status).toHaveBeenCalledTimes(2);
  51.         expect(resMock.status.mock.calls[1][0]).toBe(500);
  52.         expect(resMock.send).toHaveBeenCalledTimes(2);
  53.         expect(resMock.send.mock.calls[1][0]).toStrictEqual({error: "Houve um erro interno."});
  54.     })
  55. });
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement