Skip to content

Commit 6ca6a75

Browse files
author
Weffe
committed
test: add integration mock tests
1 parent 605f60a commit 6ca6a75

File tree

2 files changed

+82
-32
lines changed

2 files changed

+82
-32
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import axios from 'axios';
2+
import MockAdapter from 'axios-mock-adapter';
3+
import * as status from 'http-status-codes';
4+
import { withVersioning } from '../axios-api-versioning';
5+
import { IWithVersioningConfig, VersioningStrategy } from '../types';
6+
import { AxiosInstanceWithVersioning } from '../types/axios';
7+
8+
const test_url = 'http://localhost:3000';
9+
const MOCK_RES = 'hello_world';
10+
let mock: MockAdapter;
11+
let instance: AxiosInstanceWithVersioning;
12+
13+
describe('Testing correct response config of "QueryString" strategy', () => {
14+
let versioningConfig: IWithVersioningConfig = {
15+
apiVersion: '1.0',
16+
versioningStrategy: VersioningStrategy.QueryString
17+
};
18+
19+
beforeAll(() => {
20+
instance = withVersioning(axios, versioningConfig);
21+
mock = new MockAdapter(instance);
22+
})
23+
24+
test('it should have the "apiVersion" as a query param in the response config', async () => {
25+
mock.onGet(test_url)
26+
.reply(status.OK, MOCK_RES);
27+
28+
const res = await instance.get(test_url);
29+
const { params } = res.config;
30+
31+
expect(params).toHaveProperty('api-version');
32+
expect(params['api-version']).toBe(versioningConfig.apiVersion);
33+
})
34+
})
35+
36+
describe('Testing correct response config of "MediaType" strategy', () => {
37+
let versioningConfig: IWithVersioningConfig = {
38+
apiVersion: '1.0',
39+
versioningStrategy: VersioningStrategy.MediaType
40+
};
41+
42+
beforeAll(() => {
43+
instance = withVersioning(axios, versioningConfig);
44+
mock = new MockAdapter(instance);
45+
})
46+
47+
test('it should have the "apiVersion" as an accept-param in the Accept header', async () => {
48+
mock.onGet(test_url)
49+
.reply(status.OK, MOCK_RES);
50+
51+
const res = await instance.get(test_url);
52+
const { headers } = res.config;
53+
54+
expect(headers).toHaveProperty('Accept');
55+
expect(headers['Accept']).toMatch(`v=${versioningConfig.apiVersion}`)
56+
})
57+
})
58+
59+
describe('Testing correct response config of "UrlPath" strategy', () => {
60+
let versioningConfig: IWithVersioningConfig = {
61+
apiVersion: '1',
62+
versioningStrategy: VersioningStrategy.UrlPath
63+
};
64+
65+
const blank_test_url = test_url + "/v{apiVersion}";
66+
const versioned_test_url = test_url + `/v${versioningConfig.apiVersion}`;
67+
68+
beforeAll(() => {
69+
instance = withVersioning(axios, versioningConfig);
70+
mock = new MockAdapter(instance);
71+
})
72+
73+
test('it should have the "apiVersion" as a url param in the url', async () => {
74+
mock.onGet(versioned_test_url)
75+
.reply(status.OK, MOCK_RES);
76+
77+
const res = await instance.get(blank_test_url);
78+
const { url } = res.config;
79+
80+
expect(url).toBe(versioned_test_url);
81+
})
82+
})

src/__tests__/axios-api-versioning.mocks.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)