-
Notifications
You must be signed in to change notification settings - Fork 217
/
data-isolation.test.js
138 lines (119 loc) · 4.26 KB
/
data-isolation.test.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const axios = require('axios');
const sinon = require('sinon');
const nock = require('nock');
const {
initializeWebServer,
stopWebServer,
} = require('../../example-application/entry-points/api');
const OrderRepository = require('../../example-application/data-access/order-repository');
const { getShortUnique } = require('./test-helper');
let axiosAPIClient;
beforeAll(async () => {
// ️️️✅ Best Practice: Place the backend under test within the same process
const apiConnection = await initializeWebServer();
const axiosConfig = {
baseURL: `http://127.0.0.1:${apiConnection.port}`,
validateStatus: () => true, //Don't throw HTTP exceptions. Delegate to the tests to decide which error is acceptable
};
axiosAPIClient = axios.create(axiosConfig);
});
beforeEach(() => {
nock('http://localhost/user/').get(`/1`).reply(200, {
id: 1,
name: 'John',
}).persist();
});
afterEach(() => {
nock.cleanAll();
sinon.restore();
});
afterAll(async () => {
await stopWebServer();
nock.enableNetConnect();
// ️️️✅ Best Practice: Avoid cleaning-up the database after each test or afterAll
// This will interfere with other tests that run in different processes
});
describe('/api', () => {
describe('POST /orders', () => {
test('When adding a new valid order, Then should get back 200 response', async () => {
//Arrange
const orderToAdd = {
userId: 1,
productId: 2,
mode: 'approved',
// ️️️✅ Best Practice: Set unique value to unique fields so that tests writer wouldn't have to
// read previous tests before adding a new one
externalIdentifier: `100-${getShortUnique()}`, //unique value;
};
//Act
const receivedAPIResponse = await axiosAPIClient.post(
'/order',
orderToAdd
);
//Assert
expect(receivedAPIResponse.status).toBe(200);
});
test('When adding a new valid order, Then it should be approved', async () => {
//Arrange
const orderToAdd = {
userId: 1,
productId: 2,
mode: 'approved',
externalIdentifier: `100-${getShortUnique()}`, //unique value
};
//Act
const receivedAPIResponse = await axiosAPIClient.post(
'/order',
orderToAdd
);
//Assert
expect(receivedAPIResponse.data.mode).toBe('approved');
});
});
describe('GET /order:/id', () => {
test('When asked for an existing order, Then should retrieve it and receive 200 response', async () => {
// Arrange
const orderToAdd = {
userId: 1,
productId: 2,
externalIdentifier: `id-${getShortUnique()}`, //unique value
};
const existingOrder = await axiosAPIClient.post('/order', orderToAdd);
// Act
const receivedResponse = await axiosAPIClient.get(
`/order/${existingOrder.data.id}`
);
// Assert
expect(receivedResponse.status).toBe(200);
});
});
describe('Get /order', () => {
// ️️️✅ Best Practice: Acknowledge that other unknown records might exist, find your expectations within
// the result
test.todo(
'When adding 2 orders, then these orders exist in result when querying for all'
);
});
describe('DELETE /order', () => {
test('When deleting an existing order, Then it should NOT be retrievable', async () => {
// Arrange
const orderToDelete = {
userId: 1,
productId: 2,
externalIdentifier: `id-${getShortUnique()}`,
};
const deletedOrder = (await axiosAPIClient.post('/order', orderToDelete)).data.id;
const orderNotToBeDeleted = orderToDelete;
orderNotToBeDeleted.externalIdentifier = `id-${getShortUnique()}`;
const notDeletedOrder = (await axiosAPIClient.post('/order', orderNotToBeDeleted)).data.id;
// Act
const deleteRequestResponse = await axiosAPIClient.delete(`/order/${deletedOrder}`);
// Assert
const getDeletedOrderStatus = (await axiosAPIClient.get(`/order/${deletedOrder}`)).status;
const getNotDeletedOrderStatus = (await axiosAPIClient.get(`/order/${notDeletedOrder}`)).status;
expect(getNotDeletedOrderStatus).toBe(200);
expect(getDeletedOrderStatus).toBe(404);
expect(deleteRequestResponse.status).toBe(204);
});
});
});