Adds list products

This commit is contained in:
Sebastian Rindom
2020-02-25 12:40:14 +01:00
parent 1926ad396a
commit 379aa8f83f
6 changed files with 61 additions and 5 deletions

View File

@@ -4,7 +4,7 @@ import getProduct from "../get-product"
describe("Get product by id", () => {
const testId = `${mongoose.Types.ObjectId("56cb91bdc3464f14678934ca")}`
const productServiceMock = {
getProduct: jest.fn().mockImplementation(id => {
retrieve: jest.fn().mockImplementation(id => {
if (id === testId) {
return Promise.resolve({ _id: id, title: "test" })
}
@@ -42,8 +42,8 @@ describe("Get product by id", () => {
})
it("calls get product from productSerice", () => {
expect(productServiceMock.getProduct).toHaveBeenCalledTimes(1)
expect(productServiceMock.getProduct).toHaveBeenCalledWith(testId)
expect(productServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(productServiceMock.retrieve).toHaveBeenCalledWith(testId)
})
it("calls res.json", () => {

View File

@@ -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"))
})
})
})

View File

@@ -11,7 +11,7 @@ export default async (req, res) => {
}
const productService = req.scope.resolve("productService")
const product = await productService.getProduct(value)
const product = await productService.retrieve(value)
if (!product) {
res.sendStatus(404)

View File

@@ -6,6 +6,7 @@ const route = Router()
export default app => {
app.use("/products", route)
route.get("/", middlewares.wrap(require("./list-products").default))
route.get("/:productId", middlewares.wrap(require("./get-product").default))
return app

View File

@@ -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)
}

View File

@@ -1,10 +1,23 @@
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 = {
createDraft: jest.fn().mockImplementation(data => {
return Promise.resolve(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")) {
return Promise.resolve([
{
@@ -19,7 +32,12 @@ export const ProductServiceMock = {
},
])
}
return Promise.resolve([])
if (data.variants === IdMap.getId("failId")) {
return Promise.resolve([])
}
return Promise.resolve([products.product1, products.product2])
}),
}