chore: Move factories and helpers to a better place (#4551)
* chore: Move factories and helpers to a better place * align factory product variant * fix factory cart * add simple store fac * fix tests * fix tests * fix * fix cart seeder
This commit is contained in:
1 parent
26e2f81c24
commit
4d326fbbdf
197 files changed
+1073
-4332
No files matched your search
@@ -1,7 +1,6 @@
|
||||
import path from "path"
|
||||
import { ProductCategory } from "@medusajs/medusa"
|
||||
import { initDb, useDb } from "../../../helpers/use-db"
|
||||
import { simpleProductCategoryFactory } from '../../factories'
|
||||
import { initDb, useDb } from "../../../environment-helpers/use-db"
|
||||
import { simpleProductCategoryFactory } from "../../../factories"
|
||||
import { ProductCategoryRepository } from "@medusajs/medusa/dist/repositories/product-category"
|
||||
|
||||
describe("Product Categories", () => {
|
||||
@@ -28,31 +27,33 @@ describe("Product Categories", () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
a1 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: 'a1',
|
||||
name: "a1",
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
})
|
||||
a11 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: 'a11',
|
||||
name: "a11",
|
||||
parent_category: a1,
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
})
|
||||
a111 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: 'a111',
|
||||
name: "a111",
|
||||
parent_category: a11,
|
||||
is_active: true,
|
||||
is_internal: true,
|
||||
rank: 0,
|
||||
})
|
||||
a12 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: 'a12',
|
||||
name: "a12",
|
||||
parent_category: a1,
|
||||
is_active: false,
|
||||
rank: 1,
|
||||
})
|
||||
|
||||
productCategoryRepository = dbConnection.manager.withRepository(ProductCategoryRepository)
|
||||
productCategoryRepository = dbConnection.manager.withRepository(
|
||||
ProductCategoryRepository
|
||||
)
|
||||
})
|
||||
|
||||
it("can fetch all root categories", async () => {
|
||||
@@ -61,7 +62,7 @@ describe("Product Categories", () => {
|
||||
expect(rootCategories).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "a1",
|
||||
})
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
@@ -103,51 +104,46 @@ describe("Product Categories", () => {
|
||||
let productCategoryRepository
|
||||
|
||||
beforeEach(async () => {
|
||||
a1 = await simpleProductCategoryFactory(
|
||||
dbConnection, {
|
||||
name: 'skinny jeans',
|
||||
handle: 'skinny-jeans',
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
}
|
||||
)
|
||||
a1 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "skinny jeans",
|
||||
handle: "skinny-jeans",
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
a11 = await simpleProductCategoryFactory(
|
||||
dbConnection, {
|
||||
name: 'winter shirts',
|
||||
handle: 'winter-shirts',
|
||||
parent_category: a1,
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
}
|
||||
)
|
||||
a11 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "winter shirts",
|
||||
handle: "winter-shirts",
|
||||
parent_category: a1,
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
a111 = await simpleProductCategoryFactory(
|
||||
dbConnection, {
|
||||
name: 'running shoes',
|
||||
handle: 'running-shoes',
|
||||
parent_category: a11,
|
||||
rank: 0,
|
||||
}
|
||||
)
|
||||
a111 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "running shoes",
|
||||
handle: "running-shoes",
|
||||
parent_category: a11,
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
a12 = await simpleProductCategoryFactory(
|
||||
dbConnection, {
|
||||
name: 'casual shoes',
|
||||
handle: 'casual-shoes',
|
||||
parent_category: a1,
|
||||
is_internal: true,
|
||||
rank: 1,
|
||||
}
|
||||
)
|
||||
a12 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "casual shoes",
|
||||
handle: "casual-shoes",
|
||||
parent_category: a1,
|
||||
is_internal: true,
|
||||
rank: 1,
|
||||
})
|
||||
|
||||
productCategoryRepository = dbConnection.manager.withRepository(ProductCategoryRepository)
|
||||
productCategoryRepository = dbConnection.manager.withRepository(
|
||||
ProductCategoryRepository
|
||||
)
|
||||
})
|
||||
|
||||
it("fetches all active categories", async () => {
|
||||
const [ categories, count ] = await productCategoryRepository.getFreeTextSearchResultsAndCount(
|
||||
{ where: { is_active: true } },
|
||||
)
|
||||
const [categories, count] =
|
||||
await productCategoryRepository.getFreeTextSearchResultsAndCount({
|
||||
where: { is_active: true },
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(categories).toEqual([
|
||||
@@ -161,9 +157,10 @@ describe("Product Categories", () => {
|
||||
})
|
||||
|
||||
it("fetches all internal categories", async () => {
|
||||
const [ categories, count ] = await productCategoryRepository.getFreeTextSearchResultsAndCount(
|
||||
{ where: { is_internal: true } },
|
||||
)
|
||||
const [categories, count] =
|
||||
await productCategoryRepository.getFreeTextSearchResultsAndCount({
|
||||
where: { is_internal: true },
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(categories).toEqual([
|
||||
@@ -174,10 +171,11 @@ describe("Product Categories", () => {
|
||||
})
|
||||
|
||||
it("fetches all categories with query shoes", async () => {
|
||||
const [ categories, count ] = await productCategoryRepository.getFreeTextSearchResultsAndCount(
|
||||
{ where: {} },
|
||||
'shoes'
|
||||
)
|
||||
const [categories, count] =
|
||||
await productCategoryRepository.getFreeTextSearchResultsAndCount(
|
||||
{ where: {} },
|
||||
"shoes"
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(categories).toEqual([
|
||||
@@ -191,10 +189,11 @@ describe("Product Categories", () => {
|
||||
})
|
||||
|
||||
it("fetches all categories with query casual-", async () => {
|
||||
const [ categories, count ] = await productCategoryRepository.getFreeTextSearchResultsAndCount(
|
||||
{ where: {} },
|
||||
'casual-'
|
||||
)
|
||||
const [categories, count] =
|
||||
await productCategoryRepository.getFreeTextSearchResultsAndCount(
|
||||
{ where: {} },
|
||||
"casual-"
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(categories).toEqual([
|
||||
@@ -205,12 +204,11 @@ describe("Product Categories", () => {
|
||||
})
|
||||
|
||||
it("builds relations for categories", async () => {
|
||||
const [ categories, count ] = await productCategoryRepository.getFreeTextSearchResultsAndCount(
|
||||
{
|
||||
const [categories, count] =
|
||||
await productCategoryRepository.getFreeTextSearchResultsAndCount({
|
||||
where: { id: a11.id },
|
||||
relations: { parent_category: true, category_children: true },
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(categories[0]).toEqual(
|
||||
@@ -222,8 +220,8 @@ describe("Product Categories", () => {
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: a111.id,
|
||||
})
|
||||
]
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./simple-product-category-factory"
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Connection } from "typeorm"
|
||||
import { ProductCategory } from "@medusajs/medusa"
|
||||
|
||||
export const simpleProductCategoryFactory = async (
|
||||
connection: Connection,
|
||||
data: Partial<ProductCategory> = {}
|
||||
): Promise<ProductCategory> => {
|
||||
const manager = connection.manager
|
||||
const address = manager.create(ProductCategory, data)
|
||||
|
||||
return await manager.save(address)
|
||||
}
|
||||
Reference in new issue
Block a user