Axios 实例模块单元测试

ts-axios 提供了 axios.create 静态方法,返回一个 instance 实例,我们需要对这个模块做测试。

测试代码编写

test/instance.spec.ts

  1. import axios, { AxiosRequestConfig, AxiosResponse } from '../src/index'
  2. import { getAjaxRequest } from './helper'
  3. describe('instance', () => {
  4. beforeEach(() => {
  5. jasmine.Ajax.install()
  6. })
  7. afterEach(() => {
  8. jasmine.Ajax.uninstall()
  9. })
  10. test('should make a http request without verb helper', () => {
  11. const instance = axios.create()
  12. instance('/foo')
  13. return getAjaxRequest().then(request => {
  14. expect(request.url).toBe('/foo')
  15. })
  16. })
  17. test('should make a http request', () => {
  18. const instance = axios.create()
  19. instance.get('/foo')
  20. return getAjaxRequest().then(request => {
  21. expect(request.url).toBe('/foo')
  22. expect(request.method).toBe('GET')
  23. })
  24. })
  25. test('should make a post request', () => {
  26. const instance = axios.create()
  27. instance.post('/foo')
  28. return getAjaxRequest().then(request => {
  29. expect(request.method).toBe('POST')
  30. })
  31. })
  32. test('should make a put request', () => {
  33. const instance = axios.create()
  34. instance.put('/foo')
  35. return getAjaxRequest().then(request => {
  36. expect(request.method).toBe('PUT')
  37. })
  38. })
  39. test('should make a patch request', () => {
  40. const instance = axios.create()
  41. instance.patch('/foo')
  42. return getAjaxRequest().then(request => {
  43. expect(request.method).toBe('PATCH')
  44. })
  45. })
  46. test('should make a options request', () => {
  47. const instance = axios.create()
  48. instance.options('/foo')
  49. return getAjaxRequest().then(request => {
  50. expect(request.method).toBe('OPTIONS')
  51. })
  52. })
  53. test('should make a delete request', () => {
  54. const instance = axios.create()
  55. instance.delete('/foo')
  56. return getAjaxRequest().then(request => {
  57. expect(request.method).toBe('DELETE')
  58. })
  59. })
  60. test('should make a head request', () => {
  61. const instance = axios.create()
  62. instance.head('/foo')
  63. return getAjaxRequest().then(request => {
  64. expect(request.method).toBe('HEAD')
  65. })
  66. })
  67. test('should use instance options', () => {
  68. const instance = axios.create({ timeout: 1000 })
  69. instance.get('/foo')
  70. return getAjaxRequest().then(request => {
  71. expect(request.timeout).toBe(1000)
  72. })
  73. })
  74. test('should have defaults.headers', () => {
  75. const instance = axios.create({ baseURL: 'https://api.example.com' })
  76. expect(typeof instance.defaults.headers).toBe('object')
  77. expect(typeof instance.defaults.headers.common).toBe('object')
  78. })
  79. test('should have interceptors on the instance', done => {
  80. axios.interceptors.request.use(config => {
  81. config.timeout = 2000
  82. return config
  83. })
  84. const instance = axios.create()
  85. instance.interceptors.request.use(config => {
  86. config.withCredentials = true
  87. return config
  88. })
  89. let response: AxiosResponse
  90. instance.get('/foo').then(res => {
  91. response = res
  92. })
  93. getAjaxRequest().then(request => {
  94. request.respondWith({
  95. status: 200
  96. })
  97. setTimeout(() => {
  98. expect(response.config.timeout).toEqual(0)
  99. expect(response.config.withCredentials).toEqual(true)
  100. done()
  101. }, 100)
  102. })
  103. })
  104. test('should get the computed uri', () => {
  105. const fakeConfig: AxiosRequestConfig = {
  106. baseURL: 'https://www.baidu.com/',
  107. url: '/user/12345',
  108. params: {
  109. idClient: 1,
  110. idTest: 2,
  111. testString: 'thisIsATest'
  112. }
  113. }
  114. expect(axios.getUri(fakeConfig)).toBe(
  115. 'https://www.baidu.com/user/12345?idClient=1&idTest=2&testString=thisIsATest'
  116. )
  117. })
  118. })

至此我们完成了 ts-axiosAxios 实例模块相关业务逻辑的测试,下一节课我们会对拦截器模块做测试。