XMLHttpRequest.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import XMLHttpRequest from '../src/XMLHttpRequest'
  2. let request
  3. const testURL = 'http://qq.com'
  4. const testMethod = 'GET'
  5. const defaultRequsetArguments = {
  6. data: expect.any(String),
  7. header: expect.any(Object),
  8. method: expect.any(String),
  9. url: expect.any(String),
  10. success: expect.any(Function),
  11. fail: expect.any(Function),
  12. responseType: expect.any(String)
  13. }
  14. beforeEach(() => {
  15. request = jest.fn()
  16. global.wx = {
  17. request
  18. }
  19. })
  20. describe('XMLHttpRequest', () => {
  21. it('constructor return default properties', () => {
  22. const xhr = new XMLHttpRequest()
  23. expect(xhr.onreadystatechange).toBeNull()
  24. expect(xhr.readyState).toEqual(0)
  25. expect(xhr.response).toBeNull()
  26. expect(xhr.responseType).toEqual('')
  27. expect(xhr.responseXML).toBeNull()
  28. expect(xhr.status).toEqual(0)
  29. expect(xhr.statusText).toEqual('')
  30. expect(xhr.upload).toEqual(expect.any(Object))
  31. expect(xhr.withCredentials).toEqual(false)
  32. expect(request).toHaveBeenCalledTimes(0)
  33. })
  34. it('open() set "method" and "url"', () => {
  35. const xhr = new XMLHttpRequest()
  36. xhr.open(testMethod, testURL)
  37. xhr.send()
  38. expect(request).toHaveBeenCalledWith(Object.assign({}, defaultRequsetArguments, {
  39. method: testMethod,
  40. url: testURL
  41. }))
  42. })
  43. it('setRequestHeader() set header', () => {
  44. const xhr = new XMLHttpRequest()
  45. xhr.setRequestHeader('foo', 'bar')
  46. xhr.open(testMethod, testURL)
  47. xhr.send()
  48. expect(request).toHaveBeenCalledWith(Object.assign({}, defaultRequsetArguments, {
  49. header: {
  50. 'content-type': 'application/x-www-form-urlencoded', // 默认加上的 header
  51. foo: 'bar'
  52. }
  53. }))
  54. })
  55. it('send() with POST data', () => {
  56. const xhr = new XMLHttpRequest()
  57. xhr.open('POST', testURL)
  58. xhr.send('hello, world')
  59. expect(request).toHaveBeenCalledWith(Object.assign({}, defaultRequsetArguments, {
  60. method: 'POST',
  61. data: 'hello, world'
  62. }))
  63. })
  64. it('getResponseHeader() and getAllResponseHeaders()', (done) => {
  65. wx.request = jest.fn((args) => {
  66. args.success({
  67. header: {
  68. header1: 'value1',
  69. header2: 'value2'
  70. }
  71. })
  72. })
  73. const xhr = new XMLHttpRequest()
  74. xhr.onreadystatechange = () => {
  75. if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
  76. expect(xhr.getAllResponseHeaders()).toEqual('header1: value1\nheader2: value2')
  77. expect(xhr.getResponseHeader('header1')).toEqual('value1')
  78. expect(xhr.getResponseHeader('header2')).toEqual('value2')
  79. done()
  80. }
  81. }
  82. xhr.open(testMethod, testURL)
  83. xhr.send()
  84. })
  85. it('get response', (done) => {
  86. wx.request = jest.fn((args) => {
  87. args.success({
  88. data: 'hello, world',
  89. statusCode: 200
  90. })
  91. })
  92. const xhr = new XMLHttpRequest()
  93. xhr.onreadystatechange = () => {
  94. if (xhr.readyState === XMLHttpRequest.DONE) {
  95. expect(xhr.status).toEqual(200)
  96. expect(xhr.response).toEqual('hello, world')
  97. expect(xhr.responseText).toEqual('hello, world')
  98. done()
  99. }
  100. }
  101. xhr.open(testMethod, testURL)
  102. xhr.send()
  103. })
  104. })