feat(medusa, medusa-js, medusa-react): Implement Sales Channel list (#1815)

**What**
Support sales channel list in medusa, medusa-js and medusa-react

**How**

By implementing a new endpoint and the associated service method as well as the repository methods.

Medusa-js new list method in the resource

Medusa-react new hook in the queries

**Tests**

Endpoint test
Service test
Integration test
Hook tests

Fixes CORE-280
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2022-07-13 10:28:53 +00:00
1 parent c20d720040
commit a1a5848827
23 files changed
+750 -53

No files matched your search

+10 -1
View File
@@ -1270,6 +1270,15 @@
"updated_at": "2022-07-05T15:16:01.959Z",
"created_at": "2022-07-05T15:16:01.959Z",
"deleted_at": null
}
},
"sales_channels": [{
"id": "sc_01F0YES4R67TXXC1QBQ8P54A8Y",
"name": "sales channel 1 name",
"description": "sales channel 1 description",
"is_disabled": false,
"updated_at": "2022-07-05T15:16:01.959Z",
"created_at": "2022-07-05T15:16:01.959Z",
"deleted_at": null
}]
}
}
@@ -1682,6 +1682,18 @@ export const adminHandlers = [
)
}),
rest.get("/admin/sales-channels", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
count: 1,
limit: 20,
offset: 20,
sales_channels: fixtures.get("sales_channels"),
})
)
}),
rest.post("/admin/sales-channels/:id", (req, res, ctx) => {
return res(
ctx.status(200),
@@ -1,4 +1,8 @@
import { AdminSalesChannelsRes } from "@medusajs/medusa"
import {
AdminSalesChannelsRes,
AdminSalesChannelsListRes,
AdminGetSalesChannelsParams,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { useQuery } from "react-query"
import { useMedusa } from "../../../contexts"
@@ -36,21 +40,26 @@ export const useAdminSalesChannel = (
return { ...data, ...rest } as const
}
/*
/**
* retrieve a list of sales channels
* @experimental This feature is under development and may change in the future.
* To use this feature please enable feature flag `sales_channels` in your medusa backend project.
* @description Retrieve a list of sales channel
* @returns a list of sales channel as well as the pagination properties
*/
export const useAdminSalesChannels = (
query?: AdminGetSalesChannelsParams,
options?: UseQueryOptionsWrapper<
Response<AdminProductsListRes>,
Response<AdminSalesChannelsListRes>,
Error,
ReturnType<SalesChannelsQueryKeys["list"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
adminProductKeys.list(query),
adminSalesChannelsKeys.list(query),
() => client.admin.salesChannels.list(query),
options
)
return { ...data, ...rest } as const
}
*/
@@ -1,10 +1,10 @@
import { useAdminSalesChannel } from "../../../../src"
import { useAdminSalesChannel, useAdminSalesChannels } from "../../../../src"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminSalesChannel hook", () => {
test("returns a product", async () => {
test("returns a sales channel", async () => {
const salesChannel = fixtures.get("sales_channel")
const { result, waitFor } = renderHook(
() => useAdminSalesChannel(salesChannel.id),
@@ -19,3 +19,17 @@ describe("useAdminSalesChannel hook", () => {
expect(result.current.sales_channel).toEqual(salesChannel)
})
})
describe("useAdminSalesChannels hook", () => {
test("returns a list of sales channels", async () => {
const salesChannels = fixtures.get("sales_channels")
const { result, waitFor } = renderHook(() => useAdminSalesChannels(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response.status).toEqual(200)
expect(result.current.sales_channels).toEqual(salesChannels)
})
})