Skip to content

Commit a79b2ae

Browse files
committed
Added e2e tests
1 parent 6c3d4bb commit a79b2ae

File tree

2 files changed

+173
-81
lines changed

2 files changed

+173
-81
lines changed
Lines changed: 165 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
2-
import { type EventStore } from '@event-driven-io/emmett';
2+
import {
3+
formatDateToUtcYYYYMMDD,
4+
type EventStore,
5+
} from '@event-driven-io/emmett';
36
import { getEventStoreDBEventStore } from '@event-driven-io/emmett-esdb';
47
import {
58
ApiE2ESpecification,
9+
expectError,
610
expectResponse,
711
getApplication,
812
type TestRequest,
@@ -19,14 +23,12 @@ const doesGuestStayExist = (_guestId: string, _roomId: string, _day: Date) =>
1923
Promise.resolve(true);
2024

2125
describe('guestStayAccount E2E', () => {
22-
// const oldTime = new Date();
2326
const now = new Date();
24-
// const formattedNow = formatDateToUtcYYYYMMDD(now);
27+
const formattedNow = formatDateToUtcYYYYMMDD(now);
2528

2629
let guestId: string;
2730
let roomId: string;
28-
// let guestStayAccountId: string;
29-
// const amount = Math.random() * 100;
31+
const amount = Math.random() * 100;
3032
const transactionId = randomUUID();
3133

3234
let esdbContainer: StartedEventStoreDBContainer;
@@ -51,51 +53,173 @@ describe('guestStayAccount E2E', () => {
5153
);
5254
});
5355

56+
after(() => {
57+
return esdbContainer.stop();
58+
});
59+
5460
beforeEach(() => {
5561
guestId = randomUUID();
5662
roomId = randomUUID();
57-
// guestStayAccountId = toGuestStayAccountId(guestId, roomId, now);
5863
});
5964

60-
after(() => {
61-
return esdbContainer.stop();
62-
});
65+
const checkIn: TestRequest = (request) =>
66+
request.post(`/guests/${guestId}/stays/${roomId}`);
67+
68+
const recordCharge: TestRequest = (request) =>
69+
request
70+
.post(
71+
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}/charges`,
72+
)
73+
.send({ amount });
6374

64-
describe('When empty', () => {
75+
const recordPayment: TestRequest = (request) =>
76+
request
77+
.post(
78+
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}/payments`,
79+
)
80+
.send({ amount });
81+
82+
const checkOut: TestRequest = (request) =>
83+
request.delete(
84+
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}`,
85+
);
86+
87+
void describe('When not existing', () => {
6588
const notExistingAccount: TestRequest[] = [];
6689

67-
it('should add product item', () => {
68-
return given(...notExistingAccount)
69-
.when((request) => request.post(`/guests/${guestId}/stays/${roomId}`))
70-
.then([expectResponse(201)]);
90+
void it('checks in', () =>
91+
given(...notExistingAccount)
92+
.when(checkIn)
93+
.then([expectResponse(201)]));
94+
95+
void it(`doesn't record charge`, () =>
96+
given(...notExistingAccount)
97+
.when(recordCharge)
98+
.then([
99+
expectError(403, {
100+
detail: `Guest account doesn't exist!`,
101+
}),
102+
]));
103+
104+
void it(`doesn't record payment`, () =>
105+
given(...notExistingAccount)
106+
.when(recordPayment)
107+
.then([
108+
expectError(403, {
109+
detail: `Guest account doesn't exist!`,
110+
}),
111+
]));
112+
113+
void it(`doesn't checkout`, () =>
114+
given(...notExistingAccount)
115+
.when(checkOut)
116+
.then([expectError(403)]));
117+
});
118+
119+
void describe('When checked in', () => {
120+
const checkedInAccount: TestRequest = checkIn;
121+
122+
void it(`doesn't check in`, () =>
123+
given(checkedInAccount)
124+
.when(checkIn)
125+
.then([expectError(403, { detail: `Guest is already checked-in!` })]));
126+
127+
void it('records charge', () =>
128+
given(checkedInAccount)
129+
.when(recordCharge)
130+
.then([expectResponse(204)]));
131+
132+
void it('records payment', () =>
133+
given(checkedInAccount)
134+
.when(recordPayment)
135+
.then([expectResponse(204)]));
136+
137+
void it('checks out', () =>
138+
given(checkedInAccount)
139+
.when(checkOut)
140+
.then([expectResponse(204)]));
141+
142+
void describe('with unsettled balance', () => {
143+
const unsettledAccount: TestRequest[] = [checkIn, recordCharge];
144+
145+
void it('records charge', () =>
146+
given(...unsettledAccount)
147+
.when((request) =>
148+
request
149+
.post(
150+
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}/charges`,
151+
)
152+
.send({ amount }),
153+
)
154+
.then([expectResponse(204)]));
155+
156+
void it('records payment', () =>
157+
given(...unsettledAccount)
158+
.when(recordPayment)
159+
.then([expectResponse(204)]));
160+
161+
void it(`doesn't check out`, () =>
162+
given(...unsettledAccount)
163+
.when(checkOut)
164+
.then([expectError(403)]));
165+
});
166+
167+
void describe('with settled balance', () => {
168+
const settledAccount: TestRequest[] = [
169+
checkIn,
170+
recordCharge,
171+
recordPayment,
172+
];
173+
174+
void it('records charge', () =>
175+
given(...settledAccount)
176+
.when(recordCharge)
177+
.then([expectResponse(204)]));
178+
179+
void it('records payment', () =>
180+
given(...settledAccount)
181+
.when(recordPayment)
182+
.then([expectResponse(204)]));
183+
184+
void it(`checks out`, () =>
185+
given(...settledAccount)
186+
.when(checkOut)
187+
.then([expectResponse(204)]));
71188
});
72189
});
73190

74-
// describe('When empty', () => {
75-
// it('should add product item', () => {
76-
// return given((request) =>
77-
// request
78-
// .post(`/clients/${clientId}/shopping-carts/current/product-items`)
79-
// .send(productItem),
80-
// )
81-
// .when((request) =>
82-
// request.get(`/clients/${clientId}/shopping-carts/current`).send(),
83-
// )
84-
// .then([
85-
// expectResponse(200, {
86-
// body: {
87-
// clientId,
88-
// id: shoppingCartId,
89-
// productItems: [
90-
// {
91-
// quantity: productItem.quantity,
92-
// productId: productItem.productId,
93-
// },
94-
// ],
95-
// status: 'Opened',
96-
// },
97-
// }),
98-
// ]);
99-
// });
100-
// });
191+
void describe('When checked out', () => {
192+
const checkedOutAccount: TestRequest[] = [
193+
checkIn,
194+
recordCharge,
195+
recordPayment,
196+
checkOut,
197+
];
198+
199+
void it(`doesn't check in`, () =>
200+
given(...checkedOutAccount)
201+
.when(checkIn)
202+
.then([
203+
expectError(403, { detail: `Guest account is already checked out` }),
204+
]));
205+
206+
void it(`doesn't record charge`, () =>
207+
given(...checkedOutAccount)
208+
.when(recordCharge)
209+
.then([
210+
expectError(403, { detail: `Guest account is already checked out` }),
211+
]));
212+
213+
void it(`doesn't record payment`, () =>
214+
given(...checkedOutAccount)
215+
.when(recordPayment)
216+
.then([
217+
expectError(403, { detail: `Guest account is already checked out` }),
218+
]));
219+
220+
void it(`doesn't checkout`, () =>
221+
given(...checkedOutAccount)
222+
.when(checkOut)
223+
.then([expectError(403, { detail: `NotOpened` })]));
224+
});
101225
});

src/guestStayAccounts/api/api.int.spec.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,7 @@ void describe('Guest stay account', () => {
213213

214214
void it('records charge', () =>
215215
given(unsettledAccount)
216-
.when((request) =>
217-
request
218-
.post(
219-
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}/charges`,
220-
)
221-
.send({ amount }),
222-
)
216+
.when(recordCharge)
223217
.then([
224218
expectResponse(204),
225219
expectNewEvents(guestStayAccountId, [
@@ -237,13 +231,7 @@ void describe('Guest stay account', () => {
237231

238232
void it('records payment', () =>
239233
given(unsettledAccount)
240-
.when((request) =>
241-
request
242-
.post(
243-
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}/payments`,
244-
)
245-
.send({ amount }),
246-
)
234+
.when(recordPayment)
247235
.then([
248236
expectResponse(204),
249237
expectNewEvents(guestStayAccountId, [
@@ -261,11 +249,7 @@ void describe('Guest stay account', () => {
261249

262250
void it(`doesn't check out`, () =>
263251
given(unsettledAccount)
264-
.when((request) =>
265-
request.delete(
266-
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}`,
267-
),
268-
)
252+
.when(checkOut)
269253
.then([
270254
expectError(403),
271255
expectNewEvents(guestStayAccountId, [
@@ -320,13 +304,7 @@ void describe('Guest stay account', () => {
320304

321305
void it('records charge', () =>
322306
given(settledAccount)
323-
.when((request) =>
324-
request
325-
.post(
326-
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}/charges`,
327-
)
328-
.send({ amount }),
329-
)
307+
.when(recordCharge)
330308
.then([
331309
expectResponse(204),
332310
expectNewEvents(guestStayAccountId, [
@@ -344,20 +322,14 @@ void describe('Guest stay account', () => {
344322

345323
void it('records payment', () =>
346324
given(settledAccount)
347-
.when((request) =>
348-
request
349-
.post(
350-
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}/charges`,
351-
)
352-
.send({ amount }),
353-
)
325+
.when(recordPayment)
354326
.then([
355327
expectResponse(204),
356328
expectNewEvents(guestStayAccountId, [
357329
{
358-
type: 'ChargeRecorded',
330+
type: 'PaymentRecorded',
359331
data: {
360-
chargeId: `charge-${transactionId}`,
332+
paymentId: `payment-${transactionId}`,
361333
guestStayAccountId,
362334
amount,
363335
recordedAt: now,
@@ -368,11 +340,7 @@ void describe('Guest stay account', () => {
368340

369341
void it(`checks out`, () =>
370342
given(settledAccount)
371-
.when((request) =>
372-
request.delete(
373-
`/guests/${guestId}/stays/${roomId}/periods/${formattedNow}`,
374-
),
375-
)
343+
.when(checkOut)
376344
.then([
377345
expectResponse(204),
378346
expectNewEvents(guestStayAccountId, [

0 commit comments

Comments
 (0)