Created
August 20, 2021 15:00
-
-
Save daltonmenezes/d62777e047cf7b2a37d36b9cbb1202d8 to your computer and use it in GitHub Desktop.
CreateRentalUseCase.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dayjs from "dayjs"; | |
import { CarsRepositoryInMemory } from "@modules/cars/repositories/in-memory/CarsRepositoryInMemory"; | |
import { RentalsRepositoryInMemory } from "@modules/rentals/repositories/RentalsRepositoryInMemory"; | |
import { DayjsDateProvider } from "@shared/container/providers/DateProvider/implementations/DayjsDateProvider"; | |
import { AppError } from "@shared/errors/AppError"; | |
import { CreateRentalUseCase } from "./CreateRentalUseCase"; | |
let createRentalUseCase: CreateRentalUseCase; | |
let rentalsRepositoryInMemory: RentalsRepositoryInMemory; | |
let dayjsDateProvider: DayjsDateProvider; | |
let carsRepositoryInMemory: CarsRepositoryInMemory; | |
describe("Create Rental", () => { | |
const dayAdd24Hours = dayjs().add(1, "day").toDate(); | |
beforeEach(() => { | |
rentalsRepositoryInMemory = new RentalsRepositoryInMemory(); | |
dayjsDateProvider = new DayjsDateProvider(); | |
carsRepositoryInMemory = new CarsRepositoryInMemory(); | |
createRentalUseCase = new CreateRentalUseCase( | |
rentalsRepositoryInMemory, | |
dayjsDateProvider, | |
carsRepositoryInMemory | |
); | |
}); | |
it("Should be able to create a new rental", async () => { | |
const car = await carsRepositoryInMemory.create({ | |
name: "Test", | |
description: "Car Test", | |
daily_rate: 100, | |
license_plate: "test", | |
fine_amount: 40, | |
category_id: "1234", | |
brand: "brand", | |
}); | |
const rental = await createRentalUseCase.execute({ | |
user_id: "12345", | |
car_id: car.id, | |
expected_return_date: dayAdd24Hours, | |
}); | |
expect(rental).toHaveProperty("id"); | |
expect(rental).toHaveProperty("start_date"); | |
}); | |
it("Should not be able to create a new rental if there is another open to the same user", async () => { | |
await rentalsRepositoryInMemory.create({ | |
car_id: "1111", | |
expected_return_date: dayAdd24Hours, | |
user_id: "12345", | |
}); | |
await expect( | |
createRentalUseCase.execute({ | |
user_id: "12345", | |
car_id: "121212", | |
expected_return_date: dayAdd24Hours, | |
}) | |
).rejects.toEqual(new AppError("There is a rental in progress for user!")); | |
}); | |
it("Should not be able to create a new rental if there is another open to the same car", async () => { | |
await rentalsRepositoryInMemory.create({ | |
car_id: "test", | |
expected_return_date: dayAdd24Hours, | |
user_id: "12345", | |
}); | |
await expect( | |
createRentalUseCase.execute({ | |
user_id: "321", | |
car_id: "test", | |
expected_return_date: dayAdd24Hours, | |
}) | |
).rejects.toEqual(new AppError("Car is unavailable")); | |
}); | |
it("Should not be able to create a new rental with invalid return time", async () => { | |
await expect( | |
createRentalUseCase.execute({ | |
user_id: "123", | |
car_id: "test", | |
expected_return_date: dayjs().toDate(), | |
}) | |
).rejects.toEqual(new AppError("Invalid return time!")); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment