Skip to content

Commit 7643698

Browse files
authored
Merge pull request #187 from codeableorg/category-service-test
Category-service-test
2 parents b8ef29d + 8b9941a commit 7643698

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
import { createTestCategory } from "@/lib/utils.tests";
4+
import * as categoriesRepository from "@/repositories/category.repository";
5+
import {
6+
getAllCategories,
7+
getCategoryBySlug,
8+
} from "@/services/category.service";
9+
10+
// Mock the repository
11+
vi.mock("@/repositories/category.repository");
12+
13+
describe("Category Service", () => {
14+
beforeEach(() => {
15+
vi.clearAllMocks();
16+
});
17+
18+
describe("getAllCategories", () => {
19+
it("should return all categories", async () => {
20+
const mockCategories = [
21+
createTestCategory(),
22+
createTestCategory({
23+
id: 2,
24+
slug: "stickers",
25+
title: "Stickers",
26+
imgSrc: "/img/stickers.jpg",
27+
alt: "Colección de stickers para programadores",
28+
description:
29+
"Explora nuestra colección de stickers para programadores",
30+
}),
31+
];
32+
33+
vi.mocked(categoriesRepository.getAllCategories).mockResolvedValue(
34+
mockCategories
35+
);
36+
37+
const result = await getAllCategories();
38+
39+
expect(result).toEqual(mockCategories);
40+
expect(categoriesRepository.getAllCategories).toHaveBeenCalledTimes(1);
41+
});
42+
43+
it("should handle empty categories", async () => {
44+
vi.mocked(categoriesRepository.getAllCategories).mockResolvedValue([]);
45+
46+
const result = await getAllCategories();
47+
48+
expect(result).toEqual([]);
49+
expect(categoriesRepository.getAllCategories).toHaveBeenCalledTimes(1);
50+
});
51+
});
52+
53+
describe("getCategoryBySlug", () => {
54+
it("should return category when found", async () => {
55+
const mockCategory = createTestCategory();
56+
57+
vi.mocked(categoriesRepository.getCategoryBySlug).mockResolvedValue(
58+
mockCategory
59+
);
60+
61+
const result = await getCategoryBySlug("polos");
62+
63+
expect(result).toEqual(mockCategory);
64+
expect(categoriesRepository.getCategoryBySlug).toHaveBeenCalledWith(
65+
"polos"
66+
);
67+
});
68+
69+
it("should throw error when category not found", async () => {
70+
vi.mocked(categoriesRepository.getCategoryBySlug).mockResolvedValue(null);
71+
72+
await expect(getCategoryBySlug("non-existent")).rejects.toThrow(
73+
'Category with slug "non-existent" not found'
74+
);
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)