import AppError from '@shared/errors/AppError' ; import FakeAppointmentsRepository from '../repositories/fakes/FakeAppointmentsRepository' ; import CreateAppointmentService from './CreateAppointmentService' ; let fakeAppointmentsRepository ; let createAppointment ; describe ( 'CreateAppointment' , ( ) => { beforeEach ( ( ) => { fakeAppointmentsRepository = new FakeAppointmentsRepository ( ) ; createAppointment = new CreateAppointmentService ( fakeAppointmentsRepository ) ; } ) ; it ( 'should be able to create a new appointment' , async ( ) => { jest . spyOn ( Date , 'now' ) . mockImplementationOnce ( ( ) => { return new Date ( 2020 , 4 , 10 , 12 ) . getTime ( ) ; } ) ; const appointment = await createAppointment . execute ( { date : new Date ( 2020 , 4 , 10 , 13 ) , user_id : 'user-id' , provider_id : 'provider-id' , } ) ; expect ( appointment ) . toHaveProperty ( 'id' ) ; expect ( appointment . provider_id ) . toBe ( 'provider-id' ) ; } ) ; it ( 'should NOT be able to create two appointments on the same time' , async ( ) => { const appointmentDate = new Date ( 2020 , 4 , 10 , 11 ) ; await createAppointment . execute ( { date : appointmentDate , user_id : 'user-id' , provider_id : 'provider-id' , } ) ; await expect ( createAppointment . execute ( { date : appointmentDate , user_id : 'user-id' , provider_id : 'provider-id' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; it ( 'should NOT be able to create an appointment on a past date' , async ( ) => { jest . spyOn ( Date , 'now' ) . mockImplementationOnce ( ( ) => { return new Date ( 2020 , 4 , 10 , 12 ) . getTime ( ) ; } ) ; await expect ( createAppointment . execute ( { date : new Date ( 2020 , 4 , 10 , 11 ) , user_id : 'user-id' , provider_id : 'provider-id' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; it ( 'should NOT be able to create an appointment with same user and provider' , async ( ) => { jest . spyOn ( Date , 'now' ) . mockImplementationOnce ( ( ) => { return new Date ( 2020 , 4 , 10 , 12 ) . getTime ( ) ; } ) ; await expect ( createAppointment . execute ( { date : new Date ( 2020 , 4 , 10 , 13 ) , user_id : 'user-id' , provider_id : 'user-id' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; it ( 'should NOT be able to create an appointment out of working hours' , async ( ) => { jest . spyOn ( Date , 'now' ) . mockImplementationOnce ( ( ) => { return new Date ( 2020 , 4 , 10 , 12 ) . getTime ( ) ; } ) ; await expect ( createAppointment . execute ( { date : new Date ( 2020 , 4 , 11 , 7 ) , user_id : 'user-id' , provider_id : 'provider-id' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; await expect ( createAppointment . execute ( { date : new Date ( 2020 , 4 , 11 , 18 ) , user_id : 'user-id' , provider_id : 'provider-id' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; } ) ;