1 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
2
3 import { IStockLocationService } from "@medusajs/framework/types"
4 import { Module, Modules } from "@medusajs/framework/utils"
5 import { StockLocationModuleService } from "../../src/services"
6
7 jest.setTimeout(100000)
8
9 moduleIntegrationTestRunner<IStockLocationService>({
10 moduleName: Modules.STOCK_LOCATION,
11 testSuite: ({ service }) => {
12 describe("Stock Location Module Service", () => {
13 it(`should export the appropriate linkable configuration`, () => {
14 const linkable = Module(Modules.STOCK_LOCATION, {
15 service: StockLocationModuleService,
16 }).linkable
17
18 expect(Object.keys(linkable)).toEqual([
19 "stockLocation",
20 "stockLocationAddress",
21 ])
22
23 Object.keys(linkable).forEach((key) => {
24 delete linkable[key].toJSON
25 })
26
27 expect(linkable).toEqual({
28 stockLocationAddress: {
29 id: {
30 linkable: "stock_location_address_id",
31 entity: "StockLocationAddress",
32 primaryKey: "id",
33 serviceName: "stock_location",
34 field: "stockLocationAddress",
35 },
36 },
37 stockLocation: {
38 id: {
39 field: "stockLocation",
40 entity: "StockLocation",
41 linkable: "stock_location_id",
42 primaryKey: "id",
43 serviceName: "stock_location",
44 },
45 location_id: {
46 linkable: "location_id",
47 entity: "StockLocation",
48 primaryKey: "location_id",
49 serviceName: "stock_location",
50 field: "stockLocation",
51 },
52 },
53 })
54 })
55
56 describe("create", () => {
57 it("should create a stock location", async () => {
58 const data = { name: "location" }
59 const location = await service.createStockLocations(data)
60
61 expect(location).toEqual(
62 expect.objectContaining({ id: expect.any(String), ...data })
63 )
64 })
65
66 it("should create stock locations for arrray", async () => {
67 const data = [{ name: "location" }, { name: "location-1" }]
68 const locations = await service.createStockLocations(data)
69
70 expect(locations).toEqual([
71 expect.objectContaining({ id: expect.any(String), ...data[0] }),
72 expect.objectContaining({ id: expect.any(String), ...data[1] }),
73 ])
74 })
75
76 it("should create a stock location with addresses", async () => {
77 const data = {
78 name: "location",
79 address: { city: "city", address_1: "street", country_code: "US" },
80 }
81 const location = await service.createStockLocations(data)
82
83 expect(location).toEqual(
84 expect.objectContaining({
85 id: expect.any(String),
86 ...data,
87 address: expect.objectContaining({
88 id: expect.any(String),
89 ...data.address,
90 }),
91 })
92 )
93 })
94
95 it("should create stock locations with addresses", async () => {
96 const data = [
97 {
98 name: "location",
99 address: {
100 city: "city",
101 address_1: "street",
102 country_code: "US",
103 },
104 },
105 {
106 name: "location-1",
107 address: {
108 city: "city 1",
109 address_1: "street 1",
110 country_code: "US",
111 },
112 },
113 ]
114 const location = await service.createStockLocations(data)
115
116 expect(location).toEqual(
117 expect.arrayContaining([
118 expect.objectContaining({
119 id: expect.any(String),
120 ...data[0],
121 address: expect.objectContaining({
122 id: expect.any(String),
123 ...data[0].address,
124 }),
125 }),
126 expect.objectContaining({
127 id: expect.any(String),
128 ...data[1],
129 address: expect.objectContaining({
130 id: expect.any(String),
131 ...data[1].address,
132 }),
133 }),
134 ])
135 )
136 })
137 })
138
139 describe("update", () => {
140 let stockLocation
141 beforeEach(async () => {
142 stockLocation = await service.createStockLocations({
143 name: "location",
144 })
145 })
146
147 it("should update a stock location", async () => {
148 const data = {
149 id: stockLocation.id,
150 name: "updated location",
151 }
152 const location = await service.upsertStockLocations(data)
153
154 expect(location).toEqual(expect.objectContaining(data))
155 })
156
157 it("should update a stock location with an address", async () => {
158 const data = {
159 id: stockLocation.id,
160 address: {
161 address_1: "street",
162 city: "city",
163 country_code: "US",
164 },
165 }
166
167 const location = await service.upsertStockLocations(data)
168
169 expect(location).toEqual(
170 expect.objectContaining({
171 address: expect.objectContaining(data.address),
172 })
173 )
174 })
175 })
176
177 describe("updateStockLocationAddress", () => {
178 let stockLocation
179 beforeEach(async () => {
180 stockLocation = await service.createStockLocations({
181 name: "location",
182 address: { city: "city", address_1: "street", country_code: "US" },
183 })
184 })
185
186 it("should update an address", async () => {
187 const data = {
188 id: stockLocation.address.id,
189 city: "updated city",
190 address_1: "updated address_1",
191 country_code: "updated country_code",
192 }
193 const location = await (service as any).updateStockLocationAddresses(
194 data
195 )
196
197 expect(location).toEqual(expect.objectContaining(data))
198 })
199 })
200 })
201 },
202 })