Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: testing
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
lint:
Expand Down
2 changes: 1 addition & 1 deletion prisma/initial_data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CategorySlug } from "generated/prisma/client.js";
import type { CategorySlug } from "@/../generated/prisma/client";

export const categories = [
{
Expand Down
3 changes: 2 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { categories, products } from "./initial_data";
import { PrismaClient } from "../generated/prisma/client.js";

import { PrismaClient } from "@/../generated/prisma/client";

const prisma = new PrismaClient();

Expand Down
2 changes: 1 addition & 1 deletion src/db/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient } from "generated/prisma/client.js";
import { PrismaClient } from "@/../generated/prisma/client";

// Global variable to store the Prisma client instance
declare global {
Expand Down
18 changes: 0 additions & 18 deletions src/e2e/example.spec.ts

This file was deleted.

63 changes: 63 additions & 0 deletions src/lib/utils.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import type { Order, OrderDetails, OrderItem } from "@/models/order.model";
import type { Product } from "@/models/product.model";
import type { User } from "@/models/user.model";

import type {
OrderItem as PrismaOrderItem,
Order as PrismaOrder,
Product as PrismaProduct,
} from "@/../generated/prisma/client";
import type { Session } from "react-router";

import { Decimal } from "@/../generated/prisma/internal/prismaNamespace";

type TestRequestConfig = {
url?: string;
headers?: HeadersInit;
Expand Down Expand Up @@ -61,6 +68,23 @@ export const createTestProduct = (overrides?: Partial<Product>): Product => ({
...overrides,
});

export const createTestDBProduct = (
overrides?: Partial<PrismaProduct>
): PrismaProduct => ({
id: 1,
title: "Test Product",
imgSrc: "/test-image.jpg",
alt: "Test alt text",
price: new Decimal(100),
description: "Test description",
categoryId: 1,
isOnSale: false,
features: ["Feature 1", "Feature 2"],
createdAt: new Date(),
updatedAt: new Date(),
...overrides,
});

export const createTestCategory = (
overrides?: Partial<Category>
): Category => ({
Expand Down Expand Up @@ -107,6 +131,22 @@ export const createTestOrderItem = (
...overrides,
} satisfies OrderItem);

export const createTestDBOrderItem = (
overrides: Partial<PrismaOrderItem> = {}
): PrismaOrderItem =>
({
id: 1,
orderId: 1,
productId: 1,
quantity: 1,
title: "Test Product",
price: new Decimal(100),
imgSrc: "test-image.jpg",
createdAt: new Date(),
updatedAt: new Date(),
...overrides,
} satisfies PrismaOrderItem);

export const createTestOrder = (overrides: Partial<Order> = {}): Order => {
const details = overrides.details ?? createTestOrderDetails();
return {
Expand All @@ -121,3 +161,26 @@ export const createTestOrder = (overrides: Partial<Order> = {}): Order => {
...overrides,
} satisfies Order;
};

export const createTestDBOrder = (
overrides: Partial<PrismaOrder> = {}
): PrismaOrder => {
return {
id: 1,
userId: 1,
email: "[email protected]",
totalAmount: new Decimal(100),
firstName: "Test",
lastName: "User",
company: null,
address: "Test Address",
city: "Test City",
country: "Test Country",
region: "Test Region",
zip: "12345",
phone: "123456789",
createdAt: new Date(),
updatedAt: new Date(),
...overrides,
} satisfies PrismaOrder;
};
2 changes: 1 addition & 1 deletion src/models/cart.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Product } from "./product.model";
import type {
Cart as PrismaCart,
CartItem as PrismaCartItem,
} from "generated/prisma/client";
} from "@/../generated/prisma/client";

export type CartItem = PrismaCartItem & {
product: Pick<
Expand Down
2 changes: 1 addition & 1 deletion src/models/category.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Category as PrismaCategory } from "generated/prisma/client";
import type { Category as PrismaCategory } from "@/../generated/prisma/client";

export const VALID_SLUGS = ["polos", "stickers", "tazas"] as const;

Expand Down
2 changes: 1 addition & 1 deletion src/models/order.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
Order as PrismaOrder,
OrderItem as PrismaOrderItem,
} from "generated/prisma/client";
} from "@/../generated/prisma/client";

export type OrderDetails = Pick<
PrismaOrder,
Expand Down
2 changes: 1 addition & 1 deletion src/models/product.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Product as PrismaProduct } from "generated/prisma/client";
import type { Product as PrismaProduct } from "@/../generated/prisma/client";

export type Product = Omit<PrismaProduct, "price"> & {
price: number;
Expand Down
2 changes: 1 addition & 1 deletion src/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { User as PrismaUser } from "generated/prisma/client";
import type { User as PrismaUser } from "@/../generated/prisma/client";

export type User = PrismaUser;

Expand Down
8 changes: 5 additions & 3 deletions src/routes/order-confirmation/order-confirmation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";

import OrderConfirmation from ".";

import type { Route } from "./+types";

// Creates minimal test props for OrderConfirmation component
const createTestProps = (orderId = "test-123"): Route.ComponentProps => ({
loaderData: { orderId },
params: vi.fn() as any,
matches: vi.fn() as any,
params: { orderId },
// Hack to satisfy type requirements
matches: [] as unknown as Route.ComponentProps["matches"],
});

describe("OrderConfirmation", () => {
Expand Down
9 changes: 5 additions & 4 deletions src/routes/product/product.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Route } from "./+types";

// Helper function to create a test navigation object
const createTestNavigation = (overrides = {}) => ({
state: "idle",
state: "idle" as const,
location: undefined,
formMethod: undefined,
formAction: undefined,
Expand All @@ -33,8 +33,9 @@ const createTestProps = (
productData: Partial<ProductType> = {}
): Route.ComponentProps => ({
loaderData: { product: createTestProduct(productData) },
params: vi.fn() as any,
matches: vi.fn() as any,
params: { id: "123" },
// Hack to satisfy type requirements
matches: [] as unknown as Route.ComponentProps["matches"],
});

describe("Product Component", () => {
Expand Down Expand Up @@ -133,7 +134,7 @@ describe("Product Component", () => {
const props = createTestProps();
const expectedNavigation = createTestNavigation({ state: "submitting" });
// Step 2: Mock - Override navigation state to simulate loading
vi.mocked(useNavigation).mockReturnValue(expectedNavigation as any);
vi.mocked(useNavigation).mockReturnValue(expectedNavigation);
// Step 3: Call
render(<Product {...props} />);
// Step 4: Verify
Expand Down
24 changes: 11 additions & 13 deletions src/services/category.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

import { prisma as mockPrisma } from "@/db/prisma";
import { createTestCategory } from "@/lib/utils.tests";
import {
getAllCategories,
getCategoryBySlug,
} from "@/services/category.service";

// Mock Prisma client
const mockPrisma = {
category: {
findMany: vi.fn(),
findUnique: vi.fn(),
},
};

vi.mock("@/db/prisma", () => ({
prisma: mockPrisma,
prisma: {
category: {
findMany: vi.fn(),
findUnique: vi.fn(),
},
},
}));

describe("Category Service", () => {
Expand All @@ -38,7 +36,7 @@ describe("Category Service", () => {
}),
];

mockPrisma.category.findMany.mockResolvedValue(mockCategories);
vi.mocked(mockPrisma.category.findMany).mockResolvedValue(mockCategories);

const result = await getAllCategories();

Expand All @@ -47,7 +45,7 @@ describe("Category Service", () => {
});

it("should handle empty categories", async () => {
mockPrisma.category.findMany.mockResolvedValue([]);
vi.mocked(mockPrisma.category.findMany).mockResolvedValue([]);

const result = await getAllCategories();

Expand All @@ -60,7 +58,7 @@ describe("Category Service", () => {
it("should return category when found", async () => {
const mockCategory = createTestCategory();

mockPrisma.category.findUnique.mockResolvedValue(mockCategory);
vi.mocked(mockPrisma.category.findUnique).mockResolvedValue(mockCategory);

const result = await getCategoryBySlug("polos");

Expand All @@ -71,7 +69,7 @@ describe("Category Service", () => {
});

it("should throw error when category not found", async () => {
mockPrisma.category.findUnique.mockResolvedValue(null);
vi.mocked(mockPrisma.category.findUnique).mockResolvedValue(null);

await expect(getCategoryBySlug("polos")).rejects.toThrow(
'Category with slug "polos" not found'
Expand Down
4 changes: 2 additions & 2 deletions src/services/category.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Category, type CategorySlug } from "generated/prisma/client.js";

import { prisma } from "@/db/prisma";

import { type Category, type CategorySlug } from "@/../generated/prisma/client";

export async function getAllCategories(): Promise<Category[]> {
const categories = await prisma.category.findMany();
return categories;
Expand Down
Loading