Skip to content

Commit

Permalink
test(restaurant): list restaurant use case
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianosl1 committed Jan 10, 2025
1 parent 6b407a7 commit 5092437
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
49 changes: 34 additions & 15 deletions tests/helpers/restaurant-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@ import { Restaurant } from "@/restaurant/domain/restaurant.entity";
export function makeRestaurant(data: {
id?: string;
picture?: string | null;
name: string;
address: Address;
name?: string;
address?: Address;
}) {
const { city, neighborhood, number, state, street, zipcode } = data.address;
const { id, picture, name } = data;

const address = Address.builder()
.withStreet(street)
.withCity(city)
.withNumber(number)
.withNeighborhood(neighborhood)
.withState(state)
.withZipcode(zipcode)
.build();

return new Restaurant(id ?? "1", name, picture ?? null, address);
const { id, picture, name, address } = data;
return new Restaurant(
id ?? "1",
name ?? "rango brabo",
picture ?? null,
address ?? makeAddress({}),
);
}

export function makeCreateRestaurantDto(
Expand All @@ -43,3 +37,28 @@ export function makeCreateRestaurantDto(

return dto;
}

function makeAddress({
city,
street,
number,
state,
neighborhood,
zipcode,
}: {
city?: string;
street?: string;
number?: string | null;
state?: string;
neighborhood?: string;
zipcode?: string;
}): Address {
return new Address(
street ?? "rua da luz",
number ?? null,
state ?? "RJ",
city ?? "Rio de janeiro",
neighborhood ?? "centro",
zipcode ?? "0001",
);
}
8 changes: 8 additions & 0 deletions tests/mocks/restaurant-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ mockRestaurantRepository.create.mockImplementation(async (name, address) => {
const id = mockRestaurantRepository.create.mock.calls.length.toString();
return makeRestaurant({ id, name, address });
});

mockRestaurantRepository.list.mockImplementation(async () => {
return [
makeRestaurant({ id: "1" }),
makeRestaurant({ id: "2" }),
makeRestaurant({ id: "3" }),
];
});
30 changes: 30 additions & 0 deletions tests/restaurant/application/list-restaurant.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ListRestaurantUseCase } from "@/restaurant/application/useCases/list-restaurant";
import { mockRestaurantRepository } from "tests/mocks/restaurant-repository";

describe("list restaurant test use case", () => {
let listRestaurant: ListRestaurantUseCase;

beforeEach(() => {
jest.clearAllMocks();
listRestaurant = new ListRestaurantUseCase(mockRestaurantRepository);
});

test("when list restaurant then output format", async () => {
const result = await listRestaurant.execute();

expect(result).toBeDefined();
expect(result[0].id).toBeDefined();
expect(result[0].name).toBeDefined();
expect(result[0].picture).toBeDefined();
expect(typeof result[0].address).toBe("string");
});

test("when empty list ten ok", async () => {
mockRestaurantRepository.list.mockResolvedValueOnce([]);
const result = await listRestaurant.execute();

expect(result).toBeDefined();
expect(result.length).toBe(0);
expect(mockRestaurantRepository.list.mock.calls.length).toBe(1);
});
});

0 comments on commit 5092437

Please sign in to comment.