refactor(medusa): move repository specs into its own folder (#2952)
**What:** Introduces a new folder under which repository specs will be placed. Why: We don't currently have a good place to test ORM logic or custom queries against the database. The repository folder tests are a place for just exactly that. How: Creates an internal package similar to other integration tests - api and plugins. CORE-965
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import path from "path"
|
||||
import { ProductCategory } from "@medusajs/medusa"
|
||||
import { initDb, useDb } from "../../../helpers/use-db"
|
||||
import { simpleProductCategoryFactory } from '../../factories'
|
||||
|
||||
describe("Product Categories", () => {
|
||||
let dbConnection
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
describe("Tree Queries (Materialized Paths)", () => {
|
||||
let a1, a11, a111, a12
|
||||
let productCategoryRepository
|
||||
|
||||
beforeEach(async () => {
|
||||
a1 = await simpleProductCategoryFactory(dbConnection, { name: 'a1', handle: 'a1' })
|
||||
a11 = await simpleProductCategoryFactory(dbConnection, { name: 'a11', handle: 'a11', parent_category: a1 })
|
||||
a111 = await simpleProductCategoryFactory(dbConnection, { name: 'a111', handle: 'a111', parent_category: a11 })
|
||||
a12 = await simpleProductCategoryFactory(dbConnection, { name: 'a12', handle: 'a12', parent_category: a1 })
|
||||
productCategoryRepository = dbConnection.getTreeRepository(ProductCategory)
|
||||
})
|
||||
|
||||
it("can fetch all root categories", async () => {
|
||||
const rootCategories = await productCategoryRepository.findRoots()
|
||||
|
||||
expect(rootCategories).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "a1",
|
||||
})
|
||||
])
|
||||
})
|
||||
|
||||
it("can fetch all ancestors of a category", async () => {
|
||||
const a11Parent = await productCategoryRepository.findAncestors(a11)
|
||||
|
||||
expect(a11Parent).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "a1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "a11",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
|
||||
it("can fetch all root descendants of a category", async () => {
|
||||
const a1Children = await productCategoryRepository.findDescendants(a1)
|
||||
|
||||
expect(a1Children).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "a1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "a11",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "a111",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "a12",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
1
integration-tests/repositories/factories/index.ts
Normal file
1
integration-tests/repositories/factories/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./simple-product-category-factory"
|
||||
@@ -0,0 +1,12 @@
|
||||
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)
|
||||
}
|
||||
21
integration-tests/repositories/jest.config.js
Normal file
21
integration-tests/repositories/jest.config.js
Normal file
@@ -0,0 +1,21 @@
|
||||
module.exports = {
|
||||
name: "repositories",
|
||||
testEnvironment: `node`,
|
||||
rootDir: "./",
|
||||
testTimeout: 10000,
|
||||
testPathIgnorePatterns: [
|
||||
`/examples/`,
|
||||
`/www/`,
|
||||
`/dist/`,
|
||||
`/node_modules/`,
|
||||
`__tests__/fixtures`,
|
||||
`__testfixtures__`,
|
||||
`.cache`,
|
||||
],
|
||||
transformIgnorePatterns: [`/dist`],
|
||||
transform: { "^.+\\.[jt]s$": `../../jest-transformer.js` },
|
||||
setupFiles: ["../setup-env.js"],
|
||||
setupFilesAfterEnv: ["../setup.js"],
|
||||
globalSetup: "../globalSetup.js",
|
||||
globalTeardown: "../globalTeardown.js",
|
||||
}
|
||||
15
integration-tests/repositories/medusa-config.js
Normal file
15
integration-tests/repositories/medusa-config.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const DB_HOST = process.env.DB_HOST
|
||||
const DB_USERNAME = process.env.DB_USERNAME
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD
|
||||
const DB_NAME = process.env.DB_TEMP_NAME
|
||||
|
||||
module.exports = {
|
||||
plugins: [],
|
||||
projectConfig: {
|
||||
redis_url: process.env.REDIS_URL,
|
||||
database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`,
|
||||
database_type: "postgres",
|
||||
jwt_secret: "test",
|
||||
cookie_secret: "test",
|
||||
},
|
||||
}
|
||||
22
integration-tests/repositories/package.json
Normal file
22
integration-tests/repositories/package.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "integration-tests-repositories",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"test": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/medusa": "*",
|
||||
"medusa-interfaces": "*",
|
||||
"typeorm": "^0.2.31"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.12.10",
|
||||
"@babel/core": "^7.12.10",
|
||||
"@babel/node": "^7.12.10",
|
||||
"babel-preset-medusa-package": "*",
|
||||
"jest": "^26.6.3"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user