Skip to content

Commit

Permalink
feat: apply feedback, change factories setup
Browse files Browse the repository at this point in the history
  • Loading branch information
wielopolski committed Jan 28, 2025
1 parent 8c48a86 commit d70b2c6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apps/api/test/factory/category.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { categories } from "../../src/storage/schema";
import type { InferSelectModel } from "drizzle-orm";
import type { DatabasePg } from "src/common";

export type CategoryTest = InferSelectModel<typeof categories>;
type CategoryTest = InferSelectModel<typeof categories>;
export type CategoriesTest = CategoryTest[];

export const createCategoryFactory = (db: DatabasePg) => {
Expand Down
43 changes: 38 additions & 5 deletions apps/api/test/factory/chapter.factory.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
import { faker } from "@faker-js/faker";
import { Factory } from "fishery";

import { chapters } from "src/storage/schema";
import { chapters, users } from "src/storage/schema";

import { createCourseFactory } from "./course.factory";

import type { InferSelectModel } from "drizzle-orm";
import type { DatabasePg } from "src/common";
import type { DatabasePg, UUIDType } from "src/common";

type ChapterTest = InferSelectModel<typeof chapters>;

const ensureCourse = async (db: DatabasePg, courseId?: UUIDType): Promise<UUIDType> => {
if (courseId) return courseId;

const courseFactory = createCourseFactory(db);
const course = await courseFactory.create();
return course.id;
};

const ensureAuthor = async (db: DatabasePg, authorId?: UUIDType): Promise<UUIDType> => {
if (authorId) return authorId;

export type ChapterTest = InferSelectModel<typeof chapters>;
const [author] = await db
.insert(users)
.values({
id: faker.string.uuid(),
email: faker.internet.email(),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
.returning();

return author.id;
};

export const createChapterFactory = (db: DatabasePg) => {
return Factory.define<ChapterTest>(({ onCreate }) => {
onCreate(async (chapter) => {
const courseId = await ensureCourse(db, chapter.courseId);
const authorId = await ensureAuthor(db, chapter.authorId);

const [inserted] = await db
.insert(chapters)
.values({
...chapter,
courseId,
authorId,
})
.returning();

Expand All @@ -26,8 +59,8 @@ export const createChapterFactory = (db: DatabasePg) => {
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
title: faker.commerce.productName(),
courseId: "2990e3f6-0c2c-45c1-9099-41d7e069a415",
authorId: "2990e3f6-0c2c-45c1-9099-41d7e069a415",
courseId: "", // Will be auto-created if empty
authorId: "", // Will be auto-created if empty
isFreemium: false,
displayOrder: faker.number.int({ min: 1, max: 100 }),
lessonCount: faker.number.int({ min: 0, max: 20 }),
Expand Down
49 changes: 44 additions & 5 deletions apps/api/test/factory/course.factory.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
import { faker } from "@faker-js/faker";
import { Factory } from "fishery";

import { courses } from "../../src/storage/schema";
import { categories, courses, users } from "../../src/storage/schema";

import type { InferSelectModel } from "drizzle-orm";
import type { DatabasePg } from "src/common";
import type { DatabasePg, UUIDType } from "src/common";

export type CourseTest = InferSelectModel<typeof courses>;
type CourseTest = InferSelectModel<typeof courses>;

const ensureCategory = async (db: DatabasePg, categoryId?: UUIDType): Promise<UUIDType> => {
if (categoryId) return categoryId;

const [category] = await db
.insert(categories)
.values({
id: faker.string.uuid(),
title: faker.commerce.department(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
.returning();

return category.id;
};

const ensureAuthor = async (db: DatabasePg, authorId?: UUIDType): Promise<UUIDType> => {
if (authorId) return authorId;

const [author] = await db
.insert(users)
.values({
id: faker.string.uuid(),
email: faker.internet.email(),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
.returning();

return author.id;
};

export const createCourseFactory = (db: DatabasePg) => {
return Factory.define<CourseTest>(({ onCreate }) => {
onCreate(async (course) => {
const categoryId = await ensureCategory(db, course.categoryId);
const authorId = await ensureAuthor(db, course.authorId);

const [inserted] = await db
.insert(courses)
.values({
...course,
categoryId,
authorId,
})
.returning();

Expand All @@ -34,8 +73,8 @@ export const createCourseFactory = (db: DatabasePg) => {
priceInCents: faker.number.int({ min: 1000, max: 100000 }),
currency: "usd",
chapterCount: faker.number.int({ min: 1, max: 20 }),
authorId: "2990e3f6-0c2c-45c1-9099-41d7e069a415",
categoryId: "406054f3-dc49-4598-8a38-a72fe5d7a11b",
authorId: "", // Will be auto-created if empty
categoryId: "", // Will be auto-created if empty
isScorm: false,
};
});
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/components/Navigation/NavigationMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function NavigationMenuItem({ item, setIsMobileNavOpen }: NavigationMenuI
onClick={() => setIsMobileNavOpen(false)}
className={({ isActive }) =>
cn(
"flex gap-x-3 items-center py-3.5 px-4 rounded-lg 2xl:p-2 hover:bg-primary-700 hover:text-white",
"flex items-center gap-x-3 rounded-lg px-4 py-3.5 hover:bg-primary-700 hover:text-white 2xl:p-2",
{
"bg-primary-700 text-white": isActive,
"bg-white text-neutral-900": !isActive,
Expand Down
8 changes: 4 additions & 4 deletions apps/web/app/components/ui/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const SelectTrigger = React.forwardRef<
<SelectPrimitive.Trigger
ref={ref}
className={cn(
"flex w-full items-center justify-between rounded-lg h-[42px] border border-input bg-background px-3 py-2 text-sm ring-offset-background data-[placeholder]:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
"flex h-[42px] w-full items-center justify-between rounded-lg border border-input bg-background px-3 py-2 text-sm ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[placeholder]:text-muted-foreground [&>span]:line-clamp-1",
className,
)}
{...props}
Expand Down Expand Up @@ -66,7 +66,7 @@ const SelectContent = React.forwardRef<
<SelectPrimitive.Content
ref={ref}
className={cn(
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border shadow-md",
"relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
position === "popper" &&
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
className,
Expand Down Expand Up @@ -109,7 +109,7 @@ const SelectItem = React.forwardRef<
<SelectPrimitive.Item
ref={ref}
className={cn(
"focus:bg-accent focus:text-accent-foreground relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
"relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
className,
)}
{...props}
Expand All @@ -131,7 +131,7 @@ const SelectSeparator = React.forwardRef<
>(({ className, ...props }, ref) => (
<SelectPrimitive.Separator
ref={ref}
className={cn("bg-muted -mx-1 my-1 h-px", className)}
className={cn("-mx-1 my-1 h-px bg-muted", className)}
{...props}
/>
));
Expand Down

0 comments on commit d70b2c6

Please sign in to comment.