-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
create-user.e2e-spec.ts
66 lines (51 loc) · 1.98 KB
/
create-user.e2e-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { defineFeature, loadFeature } from 'jest-cucumber';
import { getConnectionPool } from '../../setup/jestSetupAfterEnv';
import { UserResponseDto } from '@modules/user/dtos/user.response.dto';
import { DatabasePool, sql } from 'slonik';
import { TestContext } from '@tests/test-utils/TestContext';
import { IdResponse } from '@src/libs/api/id.response.dto';
import {
CreateUserTestContext,
givenUserProfileData,
iSendARequestToCreateAUser,
} from '../user-shared-steps';
import { ApiClient } from '@tests/test-utils/ApiClient';
import { iReceiveAnErrorWithStatusCode } from '@tests/shared/shared-steps';
const feature = loadFeature('tests/user/create-user/create-user.feature');
/**
* e2e test implementing a Gherkin feature file
* https://github.com/Sairyss/backend-best-practices#testing
*/
defineFeature(feature, (test) => {
let pool: DatabasePool;
const apiClient = new ApiClient();
beforeAll(() => {
pool = getConnectionPool();
});
afterEach(async () => {
await pool.query(sql`TRUNCATE "users"`);
await pool.query(sql`TRUNCATE "wallets"`);
});
test('I can create a user', ({ given, when, then, and }) => {
const ctx = new TestContext<CreateUserTestContext>();
givenUserProfileData(given, ctx);
iSendARequestToCreateAUser(when, ctx);
then('I receive my user ID', () => {
const response = ctx.latestResponse as IdResponse;
expect(typeof response.id).toBe('string');
});
and('I can see my user in a list of all users', async () => {
const res = await apiClient.findAllUsers();
const response = ctx.latestResponse as IdResponse;
expect(
res.data.some((item: UserResponseDto) => item.id === response.id),
).toBe(true);
});
});
test('I try to create a user with invalid data', ({ given, when, then }) => {
const ctx = new TestContext<CreateUserTestContext>();
givenUserProfileData(given, ctx);
iSendARequestToCreateAUser(when, ctx);
iReceiveAnErrorWithStatusCode(then, ctx);
});
});