Adds list products
This commit is contained in:
@@ -4,7 +4,7 @@ import getProduct from "../get-product"
|
|||||||
describe("Get product by id", () => {
|
describe("Get product by id", () => {
|
||||||
const testId = `${mongoose.Types.ObjectId("56cb91bdc3464f14678934ca")}`
|
const testId = `${mongoose.Types.ObjectId("56cb91bdc3464f14678934ca")}`
|
||||||
const productServiceMock = {
|
const productServiceMock = {
|
||||||
getProduct: jest.fn().mockImplementation(id => {
|
retrieve: jest.fn().mockImplementation(id => {
|
||||||
if (id === testId) {
|
if (id === testId) {
|
||||||
return Promise.resolve({ _id: id, title: "test" })
|
return Promise.resolve({ _id: id, title: "test" })
|
||||||
}
|
}
|
||||||
@@ -42,8 +42,8 @@ describe("Get product by id", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("calls get product from productSerice", () => {
|
it("calls get product from productSerice", () => {
|
||||||
expect(productServiceMock.getProduct).toHaveBeenCalledTimes(1)
|
expect(productServiceMock.retrieve).toHaveBeenCalledTimes(1)
|
||||||
expect(productServiceMock.getProduct).toHaveBeenCalledWith(testId)
|
expect(productServiceMock.retrieve).toHaveBeenCalledWith(testId)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls res.json", () => {
|
it("calls res.json", () => {
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { IdMap } from "medusa-test-utils"
|
||||||
|
import { request } from "../../../../../helpers/test-request"
|
||||||
|
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
|
||||||
|
|
||||||
|
describe("GET /store/products", () => {
|
||||||
|
describe("list all products", () => {
|
||||||
|
let subject
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
subject = await request("GET", "/store/products")
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("calls get product from productSerice", () => {
|
||||||
|
expect(ProductServiceMock.list).toHaveBeenCalledTimes(1)
|
||||||
|
expect(ProductServiceMock.list).toHaveBeenCalledWith({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns products", () => {
|
||||||
|
expect(subject.body[0]._id).toEqual(IdMap.getId("product1"))
|
||||||
|
expect(subject.body[1]._id).toEqual(IdMap.getId("product2"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -11,7 +11,7 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const productService = req.scope.resolve("productService")
|
const productService = req.scope.resolve("productService")
|
||||||
const product = await productService.getProduct(value)
|
const product = await productService.retrieve(value)
|
||||||
|
|
||||||
if (!product) {
|
if (!product) {
|
||||||
res.sendStatus(404)
|
res.sendStatus(404)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const route = Router()
|
|||||||
export default app => {
|
export default app => {
|
||||||
app.use("/products", route)
|
app.use("/products", route)
|
||||||
|
|
||||||
|
route.get("/", middlewares.wrap(require("./list-products").default))
|
||||||
route.get("/:productId", middlewares.wrap(require("./get-product").default))
|
route.get("/:productId", middlewares.wrap(require("./get-product").default))
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { Validator } from "medusa-core-utils"
|
||||||
|
|
||||||
|
export default async (req, res) => {
|
||||||
|
const selector = {}
|
||||||
|
|
||||||
|
const productService = req.scope.resolve("productService")
|
||||||
|
const products = await productService.list(selector)
|
||||||
|
|
||||||
|
res.json(products)
|
||||||
|
}
|
||||||
@@ -1,10 +1,23 @@
|
|||||||
import { IdMap } from "medusa-test-utils"
|
import { IdMap } from "medusa-test-utils"
|
||||||
|
|
||||||
|
export const products = {
|
||||||
|
product1: {
|
||||||
|
_id: IdMap.getId("product1"),
|
||||||
|
name: "Product 1",
|
||||||
|
},
|
||||||
|
product2: {
|
||||||
|
_id: IdMap.getId("product2"),
|
||||||
|
name: "Product 2",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
export const ProductServiceMock = {
|
export const ProductServiceMock = {
|
||||||
createDraft: jest.fn().mockImplementation(data => {
|
createDraft: jest.fn().mockImplementation(data => {
|
||||||
return Promise.resolve(data)
|
return Promise.resolve(data)
|
||||||
}),
|
}),
|
||||||
list: jest.fn().mockImplementation(data => {
|
list: jest.fn().mockImplementation(data => {
|
||||||
|
// Used to retrieve a product based on a variant id see
|
||||||
|
// ProductVariantService.addOptionValue
|
||||||
if (data.variants === IdMap.getId("testVariant")) {
|
if (data.variants === IdMap.getId("testVariant")) {
|
||||||
return Promise.resolve([
|
return Promise.resolve([
|
||||||
{
|
{
|
||||||
@@ -19,7 +32,12 @@ export const ProductServiceMock = {
|
|||||||
},
|
},
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data.variants === IdMap.getId("failId")) {
|
||||||
return Promise.resolve([])
|
return Promise.resolve([])
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve([products.product1, products.product2])
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user