|
1
|
import { IUserModuleService } from "@medusajs/framework/types" |
|
2
|
import { Module, Modules, UserEvents } from "@medusajs/framework/utils" |
|
3
|
import { UserModuleService } from "@services" |
|
4
|
import { |
|
5
|
MockEventBusService, |
|
6
|
moduleIntegrationTestRunner, |
|
7
|
} from "@medusajs/test-utils" |
|
8
|
|
|
9
|
jest.setTimeout(30000) |
|
10
|
|
|
11
|
const defaultUserData = [ |
|
12
|
{ |
|
13
|
id: "1", |
|
14
|
email: "user_1@test.com", |
|
15
|
}, |
|
16
|
{ |
|
17
|
id: "2", |
|
18
|
email: "user_2@test.com", |
|
19
|
}, |
|
20
|
] |
|
21
|
|
|
22
|
moduleIntegrationTestRunner<IUserModuleService>({ |
|
23
|
moduleName: Modules.USER, |
|
24
|
moduleOptions: { |
|
25
|
jwt_secret: "test", |
|
26
|
}, |
|
27
|
injectedDependencies: { |
|
28
|
[Modules.EVENT_BUS]: new MockEventBusService(), |
|
29
|
}, |
|
30
|
testSuite: ({ service }) => { |
|
31
|
it(`should export the appropriate linkable configuration`, () => { |
|
32
|
const linkable = Module(Modules.USER, { |
|
33
|
service: UserModuleService, |
|
34
|
}).linkable |
|
35
|
|
|
36
|
expect(Object.keys(linkable)).toEqual(["user", "invite"]) |
|
37
|
|
|
38
|
Object.keys(linkable).forEach((key) => { |
|
39
|
delete linkable[key].toJSON |
|
40
|
}) |
|
41
|
|
|
42
|
expect(linkable).toEqual({ |
|
43
|
user: { |
|
44
|
id: { |
|
45
|
linkable: "user_id", |
|
46
|
entity: "User", |
|
47
|
primaryKey: "id", |
|
48
|
serviceName: "user", |
|
49
|
field: "user", |
|
50
|
}, |
|
51
|
}, |
|
52
|
invite: { |
|
53
|
id: { |
|
54
|
linkable: "invite_id", |
|
55
|
entity: "Invite", |
|
56
|
primaryKey: "id", |
|
57
|
serviceName: "user", |
|
58
|
field: "invite", |
|
59
|
}, |
|
60
|
}, |
|
61
|
}) |
|
62
|
}) |
|
63
|
|
|
64
|
describe("UserModuleService - User", () => { |
|
65
|
afterEach(async () => { |
|
66
|
jest.clearAllMocks() |
|
67
|
}) |
|
68
|
|
|
69
|
describe("list", () => { |
|
70
|
it("should list users", async () => { |
|
71
|
await service.createUsers(defaultUserData) |
|
72
|
const users = await service.listUsers() |
|
73
|
|
|
74
|
expect(users).toEqual([ |
|
75
|
expect.objectContaining({ |
|
76
|
id: "1", |
|
77
|
}), |
|
78
|
expect.objectContaining({ |
|
79
|
id: "2", |
|
80
|
}), |
|
81
|
]) |
|
82
|
}) |
|
83
|
|
|
84
|
it("should list users by id", async () => { |
|
85
|
await service.createUsers(defaultUserData) |
|
86
|
const users = await service.listUsers({ |
|
87
|
id: ["1"], |
|
88
|
}) |
|
89
|
|
|
90
|
expect(users).toEqual([ |
|
91
|
expect.objectContaining({ |
|
92
|
id: "1", |
|
93
|
}), |
|
94
|
]) |
|
95
|
}) |
|
96
|
}) |
|
97
|
|
|
98
|
describe("listAndCount", () => { |
|
99
|
it("should list and count users", async () => { |
|
100
|
await service.createUsers(defaultUserData) |
|
101
|
const [users, count] = await service.listAndCountUsers() |
|
102
|
|
|
103
|
expect(count).toEqual(2) |
|
104
|
expect(users).toEqual([ |
|
105
|
expect.objectContaining({ |
|
106
|
id: "1", |
|
107
|
}), |
|
108
|
expect.objectContaining({ |
|
109
|
id: "2", |
|
110
|
}), |
|
111
|
]) |
|
112
|
}) |
|
113
|
|
|
114
|
it("should list and count users by id", async () => { |
|
115
|
await service.createUsers(defaultUserData) |
|
116
|
const [Users, count] = await service.listAndCountUsers({ |
|
117
|
id: "1", |
|
118
|
}) |
|
119
|
|
|
120
|
expect(count).toEqual(1) |
|
121
|
expect(Users).toEqual([ |
|
122
|
expect.objectContaining({ |
|
123
|
id: "1", |
|
124
|
}), |
|
125
|
]) |
|
126
|
}) |
|
127
|
}) |
|
128
|
|
|
129
|
describe("retrieve", () => { |
|
130
|
const id = "1" |
|
131
|
|
|
132
|
it("should return an user for the given id", async () => { |
|
133
|
await service.createUsers(defaultUserData) |
|
134
|
|
|
135
|
const user = await service.retrieveUser(id) |
|
136
|
|
|
137
|
expect(user).toEqual( |
|
138
|
expect.objectContaining({ |
|
139
|
id, |
|
140
|
}) |
|
141
|
) |
|
142
|
}) |
|
143
|
|
|
144
|
it("should throw an error when an user with the given id does not exist", async () => { |
|
145
|
const error = await service |
|
146
|
.retrieveUser("does-not-exist") |
|
147
|
.catch((e) => e) |
|
148
|
|
|
149
|
expect(error.message).toEqual( |
|
150
|
"User with id: does-not-exist was not found" |
|
151
|
) |
|
152
|
}) |
|
153
|
|
|
154
|
it("should throw an error when a userId is not provided", async () => { |
|
155
|
const error = await service |
|
156
|
.retrieveUser(undefined as unknown as string) |
|
157
|
.catch((e) => e) |
|
158
|
|
|
159
|
expect(error.message).toEqual("user - id must be defined") |
|
160
|
}) |
|
161
|
|
|
162
|
it("should return user based on config select param", async () => { |
|
163
|
await service.createUsers(defaultUserData) |
|
164
|
|
|
165
|
const User = await service.retrieveUser(id, { |
|
166
|
select: ["id"], |
|
167
|
}) |
|
168
|
|
|
169
|
const serialized = JSON.parse(JSON.stringify(User)) |
|
170
|
|
|
171
|
expect(serialized).toEqual({ |
|
172
|
id, |
|
173
|
}) |
|
174
|
}) |
|
175
|
}) |
|
176
|
|
|
177
|
describe("delete", () => { |
|
178
|
const id = "1" |
|
179
|
|
|
180
|
it("should delete the users given an id successfully", async () => { |
|
181
|
await service.createUsers(defaultUserData) |
|
182
|
|
|
183
|
await service.deleteUsers([id]) |
|
184
|
|
|
185
|
const users = await service.listUsers({ |
|
186
|
id: [id], |
|
187
|
}) |
|
188
|
|
|
189
|
expect(users).toHaveLength(0) |
|
190
|
}) |
|
191
|
}) |
|
192
|
|
|
193
|
describe("update", () => { |
|
194
|
it("should throw an error when a id does not exist", async () => { |
|
195
|
const error = await service |
|
196
|
.updateUsers([ |
|
197
|
{ |
|
198
|
id: "does-not-exist", |
|
199
|
}, |
|
200
|
]) |
|
201
|
.catch((e) => e) |
|
202
|
|
|
203
|
expect(error.message).toEqual( |
|
204
|
'User with id "does-not-exist" not found' |
|
205
|
) |
|
206
|
}) |
|
207
|
|
|
208
|
it("should emit user created events", async () => { |
|
209
|
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit") |
|
210
|
await service.createUsers(defaultUserData) |
|
211
|
|
|
212
|
jest.clearAllMocks() |
|
213
|
|
|
214
|
await service.updateUsers([ |
|
215
|
{ |
|
216
|
id: "1", |
|
217
|
first_name: "John", |
|
218
|
}, |
|
219
|
]) |
|
220
|
|
|
221
|
expect(eventBusSpy).toHaveBeenCalledTimes(1) |
|
222
|
expect(eventBusSpy).toHaveBeenCalledWith( |
|
223
|
[ |
|
224
|
expect.objectContaining({ |
|
225
|
data: { id: "1" }, |
|
226
|
name: UserEvents.USER_UPDATED, |
|
227
|
}), |
|
228
|
], |
|
229
|
{ |
|
230
|
internal: true, |
|
231
|
} |
|
232
|
) |
|
233
|
}) |
|
234
|
}) |
|
235
|
|
|
236
|
describe("create", () => { |
|
237
|
it("should create a user successfully", async () => { |
|
238
|
await service.createUsers(defaultUserData) |
|
239
|
|
|
240
|
const [User, count] = await service.listAndCountUsers({ |
|
241
|
id: ["1"], |
|
242
|
}) |
|
243
|
|
|
244
|
expect(count).toEqual(1) |
|
245
|
expect(User[0]).toEqual( |
|
246
|
expect.objectContaining({ |
|
247
|
id: "1", |
|
248
|
}) |
|
249
|
) |
|
250
|
}) |
|
251
|
|
|
252
|
it("should emit user created events", async () => { |
|
253
|
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit") |
|
254
|
await service.createUsers(defaultUserData) |
|
255
|
|
|
256
|
// 2 events: 2 user created |
|
257
|
expect(eventBusSpy.mock.calls[0][0]).toHaveLength(2) |
|
258
|
|
|
259
|
const events = eventBusSpy.mock.calls[0][0] |
|
260
|
expect(events).toHaveLength(2) |
|
261
|
expect(events).toEqual( |
|
262
|
expect.arrayContaining([ |
|
263
|
expect.objectContaining({ |
|
264
|
data: { id: "1" }, |
|
265
|
name: UserEvents.USER_CREATED, |
|
266
|
}), |
|
267
|
expect.objectContaining({ |
|
268
|
data: { id: "2" }, |
|
269
|
name: UserEvents.USER_CREATED, |
|
270
|
}), |
|
271
|
]) |
|
272
|
) |
|
273
|
}) |
|
274
|
}) |
|
275
|
}) |
|
276
|
}, |
|
277
|
}) |