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