Skip to content

Commit 8833f39

Browse files
authored
Merge pull request #5 from hungdv136/ci
Add badge
2 parents d40a925 + 73b46ae commit 8833f39

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Use Rio as a mock server to test Rest API
22

3+
[![sdk-ci](https://github.com/hungdv136/rio-js/actions/workflows/ci.yml/badge.svg)](https://github.com/hungdv136/rio-js/actions/workflows/ci.yml)
4+
35
- [Use Rio as a mock server to test Rest API](#use-rio-as-a-mock-server-to-test-rest-api)
46
- [Introduction](#introduction)
57
- [Why mock?](#why-mock)

example/checkout.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import axios from 'axios'
22
import { v4 as uuidv4 } from 'uuid';
33
import { Server } from '../sdk/server';
4+
import { Stub } from '../sdk/stub';
5+
import { Rule } from '../sdk/matching-rule';
6+
import { JSONResponse } from '../sdk/response';
47

58
// Your checkout API
69
const checkoutURL = 'http://localhost:8808/checkout';
710
const mockServer = new Server('http://localhost:8896');
811

9-
describe('POST /checkout', () => {
12+
describe('test /checkout using raw JSON to create stub', () => {
1013
// Create random data
1114
const validCardNumber = uuidv4();
1215
const invalidCardNumber = uuidv4();
@@ -61,6 +64,46 @@ describe('POST /checkout', () => {
6164
expect(data.response.id).toBe(expectedId)
6265
});
6366

67+
it('card number is not matched, no response from 3rd party', async () => {
68+
const params = { cardNumber: invalidCardNumber, amount: 30000}
69+
const { data, status } = await axios.post(checkoutURL, params);
70+
expect(status).toBe(200)
71+
expect(data.response).toBeUndefined()
72+
});
73+
});
74+
75+
describe('test /checkout using SDK to create stub', () => {
76+
// Create random data
77+
const validCardNumber = uuidv4();
78+
const invalidCardNumber = uuidv4();
79+
const expectedId = uuidv4();
80+
81+
82+
beforeAll(async () => {
83+
try {
84+
await mockServer.clearStubs();
85+
86+
// Create a stub which matches method, url and request body (stubs -> request)
87+
const stub = new Stub('POST', Rule.contains('/pay'))
88+
.withRequestBodyJSONPath('$.cardNumber', Rule.equalsTo(validCardNumber))
89+
.withRequestBodyJSONPath('$.amount', Rule.equalsTo(30000))
90+
.willReturn(JSONResponse({id: expectedId}))
91+
.send(mockServer)
92+
93+
await mockServer.createStub(stub);
94+
} catch(error) {
95+
console.log('setup error: ', error);
96+
}
97+
})
98+
99+
it('card number is matched, return predefined body in stub', async () => {
100+
const params = { cardNumber: validCardNumber, amount: 30000}
101+
const { data, status } = await axios.post(checkoutURL, params);
102+
expect(status).toBe(200)
103+
expect(data.response.id.length).not.toBe(0);
104+
expect(data.response.id).toBe(expectedId)
105+
});
106+
64107
it('card number is not matched, no response from 3rd party', async () => {
65108
const params = { cardNumber: invalidCardNumber, amount: 30000}
66109
const { data, status } = await axios.post(checkoutURL, params);

example/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"esModuleInterop": true,
66
"forceConsistentCasingInFileNames": true,
77
"strict": true,
8-
"skipLibCheck": true
8+
"skipLibCheck": true,
9+
"strictPropertyInitialization": false
910
}
1011
}

0 commit comments

Comments
 (0)