1 import { ISalesChannelModuleService } from "@medusajs/framework/types"
2 import { Module, Modules } from "@medusajs/framework/utils"
3 import { SalesChannelModuleService } from "@services"
4 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
5
6 jest.setTimeout(30000)
7
8 const salesChannelData = [
9 {
10 id: "channel-1",
11 name: "Channel 1",
12 description: "Channel description 1",
13 is_disabled: false,
14 },
15 {
16 id: "channel-2",
17 name: "Channel 2",
18 description: "Channel description 2",
19 is_disabled: false,
20 },
21 {
22 id: "channel-3",
23 name: "Channel 3",
24 description: "Channel description 3",
25 is_disabled: true,
26 },
27 ]
28
29 moduleIntegrationTestRunner<ISalesChannelModuleService>({
30 moduleName: Modules.SALES_CHANNEL,
31 testSuite: ({ service }) => {
32 describe("Sales Channel Service", () => {
33 beforeEach(async () => {
34 await service.createSalesChannels(salesChannelData)
35 })
36
37 it(`should export the appropriate linkable configuration`, () => {
38 const linkable = Module(Modules.SALES_CHANNEL, {
39 service: SalesChannelModuleService,
40 }).linkable
41
42 expect(Object.keys(linkable)).toEqual(["salesChannel"])
43
44 Object.keys(linkable).forEach((key) => {
45 delete linkable[key].toJSON
46 })
47
48 expect(linkable).toEqual({
49 salesChannel: {
50 id: {
51 linkable: "sales_channel_id",
52 entity: "SalesChannel",
53 primaryKey: "id",
54 serviceName: "sales_channel",
55 field: "salesChannel",
56 },
57 },
58 })
59 })
60
61 describe("create", () => {
62 it("should create a SalesChannel successfully", async () => {
63 const [created] = await service.createSalesChannels([
64 {
65 name: "test",
66 description: "test",
67 },
68 ])
69
70 const [channel] = await service.listSalesChannels({
71 name: [created.name],
72 })
73
74 expect(channel.name).toEqual("test")
75 expect(channel.description).toEqual("test")
76 })
77 })
78
79 describe("retrieve", () => {
80 const id = "channel-1"
81
82 it("should return SalesChannel for the given id", async () => {
83 const result = await service.retrieveSalesChannel(id)
84
85 expect(result).toEqual(
86 expect.objectContaining({
87 id,
88 })
89 )
90 })
91
92 it("should throw an error when SalesChannelId with id does not exist", async () => {
93 let error
94
95 try {
96 await service.retrieveSalesChannel("does-not-exist")
97 } catch (e) {
98 error = e
99 }
100
101 expect(error.message).toEqual(
102 "SalesChannel with id: does-not-exist was not found"
103 )
104 })
105 })
106
107 describe("update", () => {
108 const id = "channel-2"
109
110 it("should update the name of the SalesChannel successfully", async () => {
111 await service.updateSalesChannels(id, {
112 name: "Update name 2",
113 is_disabled: true,
114 })
115
116 const channel = await service.retrieveSalesChannel(id)
117
118 expect(channel.name).toEqual("Update name 2")
119 expect(channel.is_disabled).toEqual(true)
120 })
121
122 it("should throw an error when a id does not exist", async () => {
123 let error
124
125 try {
126 await service.updateSalesChannels("does-not-exist", {
127 name: "does-not-exist",
128 })
129 } catch (e) {
130 error = e
131 }
132
133 expect(error.message).toEqual(
134 'SalesChannel with id "does-not-exist" not found'
135 )
136 })
137 })
138
139 describe("list", () => {
140 it("should return a list of SalesChannels", async () => {
141 const result = await service.listSalesChannels()
142
143 expect(result).toEqual([
144 expect.objectContaining({
145 id: "channel-1",
146 }),
147 expect.objectContaining({
148 id: "channel-2",
149 }),
150 expect.objectContaining({
151 id: "channel-3",
152 }),
153 ])
154 })
155
156 it("should list SalesChannels by name", async () => {
157 const result = await service.listSalesChannels({
158 name: ["Channel 2", "Channel 3"],
159 })
160
161 expect(result).toEqual([
162 expect.objectContaining({
163 id: "channel-2",
164 }),
165 expect.objectContaining({
166 id: "channel-3",
167 }),
168 ])
169 })
170 })
171
172 describe("listAndCount", () => {
173 it("should return sales channels and count", async () => {
174 const [result, count] = await service.listAndCountSalesChannels()
175
176 expect(count).toEqual(3)
177 expect(result).toEqual([
178 expect.objectContaining({
179 id: "channel-1",
180 }),
181 expect.objectContaining({
182 id: "channel-2",
183 }),
184 expect.objectContaining({
185 id: "channel-3",
186 }),
187 ])
188 })
189
190 it("should return sales channels and count when filtered", async () => {
191 const [result, count] = await service.listAndCountSalesChannels({
192 id: ["channel-2"],
193 })
194
195 expect(count).toEqual(1)
196 expect(result).toEqual([
197 expect.objectContaining({
198 id: "channel-2",
199 }),
200 ])
201 })
202
203 it("should return sales channels and count when using skip and take", async () => {
204 const [results, count] = await service.listAndCountSalesChannels(
205 {},
206 { skip: 1, take: 1 }
207 )
208
209 expect(count).toEqual(3)
210 expect(results).toEqual([
211 expect.objectContaining({
212 id: "channel-2",
213 }),
214 ])
215 })
216
217 it("should return requested fields", async () => {
218 const [result, count] = await service.listAndCountSalesChannels(
219 {},
220 {
221 take: 1,
222 select: ["id", "name"],
223 }
224 )
225
226 const serialized = JSON.parse(JSON.stringify(result))
227
228 expect(count).toEqual(3)
229 expect(serialized).toEqual([
230 {
231 id: "channel-1",
232 name: "Channel 1",
233 },
234 ])
235 })
236
237 it("should filter disabled channels", async () => {
238 const [result, count] = await service.listAndCountSalesChannels(
239 { is_disabled: true },
240 { select: ["id"] }
241 )
242
243 const serialized = JSON.parse(JSON.stringify(result))
244
245 expect(count).toEqual(1)
246 expect(serialized).toEqual([
247 {
248 id: "channel-3",
249 },
250 ])
251 })
252 })
253
254 describe("delete", () => {
255 const id = "channel-2"
256
257 it("should delete the SalesChannel given an id successfully", async () => {
258 await service.deleteSalesChannels([id])
259
260 const result = await service.listSalesChannels({
261 id: [id],
262 })
263
264 expect(result).toHaveLength(0)
265 })
266 })
267 })
268 },
269 })