Skip to content

Commit 9e27c06

Browse files
authored
chore: add missing tests for accounts service (#65)
1 parent 54bed48 commit 9e27c06

File tree

1 file changed

+230
-1
lines changed

1 file changed

+230
-1
lines changed

examples/http-server/test/Accounts.test.ts

Lines changed: 230 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { Email } from "app/Domain/Email"
77
import { withSystemActor } from "app/Domain/Policy"
88
import { User, UserId } from "app/Domain/User"
99
import { makeTestLayer } from "app/lib/Layer"
10-
import { DateTime, Effect, Layer, pipe, Redacted } from "effect"
10+
import { DateTime, Effect, Layer, Option, pipe, Redacted } from "effect"
11+
import { accessTokenFromRedacted } from "../src/Domain/AccessToken.js"
1112

1213
describe("Accounts", () => {
1314
it.effect("createUser", () =>
@@ -57,4 +58,232 @@ describe("Accounts", () => {
5758
)
5859
)
5960
))
61+
it.effect("updateUser", () =>
62+
Effect.gen(function*() {
63+
const accounts = yield* Accounts
64+
const userId = UserId.make(1)
65+
const updatedUser = { email: Email.make("[email protected]") }
66+
67+
const updatedUserResult = yield* pipe(
68+
accounts.updateUser(userId, updatedUser),
69+
withSystemActor
70+
)
71+
72+
assert.strictEqual(updatedUserResult.id, 1)
73+
assert.strictEqual(updatedUserResult.email, updatedUser.email)
74+
assert.strictEqual(updatedUserResult.accountId, 123)
75+
}).pipe(
76+
Effect.provide(
77+
Accounts.Test.pipe(
78+
Layer.provide(
79+
makeTestLayer(AccountsRepo)({
80+
insert: (account) =>
81+
Effect.map(
82+
DateTime.now,
83+
(now) =>
84+
new Account({
85+
...account,
86+
id: AccountId.make(123),
87+
createdAt: now,
88+
updatedAt: now
89+
})
90+
)
91+
})
92+
),
93+
Layer.provide(
94+
makeTestLayer(UsersRepo)({
95+
findById: (id: UserId) =>
96+
Effect.succeed(
97+
Option.some(
98+
new User({
99+
id,
100+
email: Email.make("[email protected]"),
101+
accountId: AccountId.make(123),
102+
createdAt: Effect.runSync(DateTime.now),
103+
updatedAt: Effect.runSync(DateTime.now),
104+
accessToken: accessTokenFromRedacted(
105+
Redacted.make("test-uuid")
106+
)
107+
})
108+
)
109+
),
110+
update: (user) =>
111+
Effect.map(
112+
DateTime.now,
113+
(now) =>
114+
new User({
115+
...user,
116+
updatedAt: now,
117+
createdAt: now
118+
})
119+
)
120+
})
121+
)
122+
)
123+
)
124+
))
125+
it.effect("findUserByAccessToken", () =>
126+
Effect.gen(function*() {
127+
const accounts = yield* Accounts
128+
const apiKey = accessTokenFromRedacted(
129+
Redacted.make("test-uuid")
130+
)
131+
const user = yield* pipe(
132+
accounts.findUserByAccessToken(apiKey),
133+
withSystemActor
134+
)
135+
if (Option.isSome(user)) {
136+
const userValue = user.value
137+
assert.strictEqual(userValue.id, 1)
138+
assert.strictEqual(userValue.accountId, 123)
139+
assert.strictEqual(userValue.email, Email.make("[email protected]"))
140+
}
141+
assert.strictEqual(Option.isSome(user), true)
142+
}).pipe(
143+
Effect.provide(
144+
Accounts.Test.pipe(
145+
Layer.provide(
146+
makeTestLayer(AccountsRepo)({
147+
insert: (account) =>
148+
Effect.map(
149+
DateTime.now,
150+
(now) =>
151+
new Account({
152+
...account,
153+
id: AccountId.make(123),
154+
createdAt: now,
155+
updatedAt: now
156+
})
157+
)
158+
})
159+
),
160+
Layer.provide(
161+
makeTestLayer(UsersRepo)({
162+
findByAccessToken: (apiKey) =>
163+
Effect.succeed(
164+
Option.some(
165+
new User({
166+
id: UserId.make(1),
167+
email: Email.make("[email protected]"),
168+
accountId: AccountId.make(123),
169+
createdAt: Effect.runSync(DateTime.now),
170+
updatedAt: Effect.runSync(DateTime.now),
171+
accessToken: apiKey
172+
})
173+
)
174+
)
175+
})
176+
)
177+
)
178+
)
179+
))
180+
181+
it.effect("findUserById", () =>
182+
Effect.gen(function*() {
183+
const accounts = yield* Accounts
184+
const userId = UserId.make(1)
185+
const user = yield* pipe(
186+
accounts.findUserById(userId),
187+
withSystemActor
188+
)
189+
if (Option.isSome(user)) {
190+
const userValue = user.value
191+
assert.strictEqual(userValue.id, 1)
192+
assert.strictEqual(userValue.accountId, 123)
193+
assert.strictEqual(userValue.email, Email.make("[email protected]"))
194+
}
195+
assert.strictEqual(Option.isSome(user), true)
196+
}).pipe(
197+
Effect.provide(
198+
Accounts.Test.pipe(
199+
Layer.provide(
200+
makeTestLayer(AccountsRepo)({
201+
insert: (account) =>
202+
Effect.map(
203+
DateTime.now,
204+
(now) =>
205+
new Account({
206+
...account,
207+
id: AccountId.make(123),
208+
createdAt: now,
209+
updatedAt: now
210+
})
211+
)
212+
})
213+
),
214+
Layer.provide(
215+
makeTestLayer(UsersRepo)({
216+
findById: (id: UserId) =>
217+
Effect.succeed(
218+
Option.some(
219+
new User({
220+
id,
221+
email: Email.make("[email protected]"),
222+
accountId: AccountId.make(123),
223+
createdAt: Effect.runSync(DateTime.now),
224+
updatedAt: Effect.runSync(DateTime.now),
225+
accessToken: accessTokenFromRedacted(Redacted.make("test-uuid"))
226+
})
227+
)
228+
)
229+
})
230+
)
231+
)
232+
)
233+
))
234+
it.effect("embellishUser", () =>
235+
Effect.gen(function*() {
236+
const accounts = yield* Accounts
237+
const user = new User({
238+
id: UserId.make(1),
239+
email: Email.make("[email protected]"),
240+
accountId: AccountId.make(123),
241+
createdAt: Effect.runSync(DateTime.now),
242+
updatedAt: Effect.runSync(DateTime.now),
243+
accessToken: accessTokenFromRedacted(Redacted.make("test-uuid"))
244+
})
245+
const embellishedUser = yield* pipe(
246+
accounts.embellishUser(user),
247+
withSystemActor
248+
)
249+
assert.strictEqual(embellishedUser.id, 1)
250+
assert.strictEqual(embellishedUser.account.id, 123)
251+
assert.strictEqual(embellishedUser.email, "[email protected]")
252+
}).pipe(
253+
Effect.provide(
254+
Accounts.Test.pipe(
255+
Layer.provide(
256+
makeTestLayer(AccountsRepo)({
257+
findById: (accountId: AccountId) =>
258+
Effect.succeed(
259+
Option.some(
260+
new Account({
261+
id: accountId,
262+
createdAt: Effect.runSync(DateTime.now),
263+
updatedAt: Effect.runSync(DateTime.now)
264+
})
265+
)
266+
)
267+
})
268+
),
269+
Layer.provide(
270+
makeTestLayer(UsersRepo)({
271+
findById: (id: UserId) =>
272+
Effect.succeed(
273+
Option.some(
274+
new User({
275+
id,
276+
email: Email.make("[email protected]"),
277+
accountId: AccountId.make(123),
278+
createdAt: Effect.runSync(DateTime.now),
279+
updatedAt: Effect.runSync(DateTime.now),
280+
accessToken: accessTokenFromRedacted(Redacted.make("test-uuid"))
281+
})
282+
)
283+
)
284+
})
285+
)
286+
)
287+
)
288+
))
60289
})

0 commit comments

Comments
 (0)