date-fns#getMilliseconds TypeScript Examples

The following examples show how to use date-fns#getMilliseconds. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResetPasswordService.spec.ts    From hotseat-api with MIT License 4 votes vote down vote up
describe('Reset User Password', () => {
  beforeEach(() => {
    usersRepository = new FakeUsersRepository();
    recoverPasswordRequestsRepository = new FakeRecoverPasswordRequestsRepository();

    hashProvider = new FakeBCryptHashProvider();

    resetPasswordService = new ResetPasswordService(
      recoverPasswordRequestsRepository,
      usersRepository,
      hashProvider,
    );
  });

  it('should be able to reset the user password', async () => {
    const { id } = await usersRepository.create({
      name: 'Jackie Chan',
      email: '[email protected]',
      password: 'meaningless password',
    });

    const recoverPasswordRequest = await recoverPasswordRequestsRepository.create(
      id,
    );

    const newPassword = 'meaningless new password';

    const generateHash = jest.spyOn(hashProvider, 'generateHash');

    await resetPasswordService.execute({
      token: recoverPasswordRequest.token,
      password: newPassword,
    });

    const updatedUser = await usersRepository.findById(id);

    expect(updatedUser?.password).toBe(newPassword);
    expect(generateHash).toBeCalledWith(updatedUser?.password);
  });

  it('should not be able to reset the password of an nonexisting request', async () => {
    await expect(
      resetPasswordService.execute({
        password: 'new meaningless  password',
        token: 'non existing request token',
      }),
    ).rejects.toBeInstanceOf(AppError);
  });

  it('should not be able to reset the password of an nonexisting user', async () => {
    const { token } = await recoverPasswordRequestsRepository.create(
      'non existing user id',
    );

    await expect(
      resetPasswordService.execute({
        password: 'new meaningless password',
        token,
      }),
    ).rejects.toBeInstanceOf(AppError);
  });

  it('should not be able to reset the password if the request is expired', async () => {
    jest.spyOn(Date, 'now').mockImplementationOnce(() => {
      return getMilliseconds(
        addHours(Date.now(), RESET_PASSWORD_REQUEST_EXPIRES_IN_HOURS),
      );
    });

    const user = await usersRepository.create({
      name: 'Jackie Chan',
      email: '[email protected]',
      password: 'meaningless password',
    });

    const { token } = await recoverPasswordRequestsRepository.create(user.id);

    await expect(
      resetPasswordService.execute({
        password: 'new meaningless password',
        token,
      }),
    ).rejects.toBeInstanceOf(AppError);
  });

  it('should delete recover password request after reseting the password', async () => {
    const user = await usersRepository.create({
      name: 'Jackie Chan',
      email: '[email protected]',
      password: 'meaningless password',
    });

    const { token } = await recoverPasswordRequestsRepository.create(user.id);

    await resetPasswordService.execute({
      token,
      password: 'new meaningless password',
    });

    const checkIfRequestExists = await recoverPasswordRequestsRepository.findByToken(
      token,
    );

    expect(checkIfRequestExists).toBeFalsy();
  });
});