url.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { RouterURL } from '../src/url'
  2. describe('URL', () => {
  3. describe('web mode', () => {
  4. test('basic', () => {
  5. const u = new RouterURL('http://localhost/hello/world')
  6. expect(u.mode).toBe('web')
  7. expect(u.path).toBe('/hello/world')
  8. })
  9. test('with base', () => {
  10. const u = new RouterURL('http://localhost/base/hello/world', { base: '/base' })
  11. expect(u.mode).toBe('web')
  12. expect(u.base).toBe('/base')
  13. expect(u.path).toBe('/hello/world')
  14. })
  15. test('parse query', () => {
  16. const u = new RouterURL('http://localhost/hello/world?a=1&b=c')
  17. expect(u.query).toStrictEqual({a: '1', b: 'c'})
  18. })
  19. test('resolve url', () => {
  20. const u = new RouterURL('http://localhost/hello/world?a=1&b=c')
  21. expect(u.resolve('/xyz', {a: '123', d: 1}).url).toBe('http://localhost/xyz?a=123&b=c&d=1')
  22. expect(u.resolve('/abc', {a: '123', d: 1}, true).url).toBe('http://localhost/abc?a=123&d=1')
  23. expect(u.resolve('/def', {}, true).url).toBe('http://localhost/def')
  24. expect(u.resolve('/', {}, true).url).toBe('http://localhost/')
  25. expect(u.resolve('', {}, true).url).toBe('http://localhost/')
  26. })
  27. test('resolve url with bsae', () => {
  28. const u = new RouterURL('http://localhost/base/hello/world?a=1&b=c', { base: '/base' })
  29. expect(u.resolve('/xyz', {a: '123', d: 1}).url).toBe('http://localhost/base/xyz?a=123&b=c&d=1')
  30. expect(u.resolve('/abc', {a: '123', d: 1}, true).url).toBe('http://localhost/base/abc?a=123&d=1')
  31. expect(u.resolve('/def', {}, true).url).toBe('http://localhost/base/def')
  32. expect(u.resolve('/', {}, true).url).toBe('http://localhost/base/')
  33. expect(u.resolve('', {}, true).url).toBe('http://localhost/base')
  34. })
  35. })
  36. describe('hash mode', () => {
  37. test('basic', () => {
  38. const u = new RouterURL('http://localhost/#/hello/world', { mode: 'hash' })
  39. expect(u.mode).toBe('hash')
  40. expect(u.path).toBe('/hello/world')
  41. })
  42. test('with base', () => {
  43. const u = new RouterURL('http://localhost/base#/hello/world', { mode: 'hash', base: '/base' })
  44. expect(u.mode).toBe('hash')
  45. expect(u.base).toBe('/base')
  46. expect(u.path).toBe('/hello/world')
  47. })
  48. test('parse query', () => {
  49. const u = new RouterURL('http://localhost/#/hello/world?a=1&b=c', { mode: 'hash' })
  50. expect(u.query).toStrictEqual({a: '1', b: 'c'})
  51. })
  52. test('resolve url', () => {
  53. const u = new RouterURL('http://localhost/#/hello/world?a=1&b=c', { mode: 'hash' })
  54. expect(u.resolve('/xyz', {a: '123', d: 1}).url).toBe('http://localhost/#/xyz?a=123&b=c&d=1')
  55. expect(u.resolve('/abc', {a: '123', d: 1}, true).url).toBe('http://localhost/#/abc?a=123&d=1')
  56. expect(u.resolve('/def', {}, true).url).toBe('http://localhost/#/def')
  57. expect(u.resolve('/', {}, true).url).toBe('http://localhost/#/')
  58. expect(u.resolve('', {}, true).url).toBe('http://localhost/#')
  59. })
  60. test('resolve url with base', () => {
  61. const u = new RouterURL('http://localhost/base#/hello/world?a=1&b=c', { mode: 'hash', base: '/base' })
  62. expect(u.resolve('/xyz', {a: '123', d: 1}).url).toBe('http://localhost/base#/xyz?a=123&b=c&d=1')
  63. expect(u.resolve('/abc', {a: '123', d: 1}, true).url).toBe('http://localhost/base#/abc?a=123&d=1')
  64. expect(u.resolve('/def', {}, true).url).toBe('http://localhost/base#/def')
  65. expect(u.resolve('/', {}, true).url).toBe('http://localhost/base#/')
  66. expect(u.resolve('', {}, true).url).toBe('http://localhost/base#')
  67. })
  68. test('convert web to hash', () => {
  69. const u = new RouterURL('http://localhost/hello/world?a=1&b=c', { mode: 'hash' })
  70. expect(u.path).toBe('/hello/world')
  71. expect(u.resolve('/xyz', {a: '123', d: 1}).url).toBe('http://localhost/#/xyz?a=123&b=c&d=1')
  72. })
  73. test('resolve template path', () => {
  74. let pathname = new URL('http://localhost/hello/world').pathname
  75. expect(RouterURL.resolveTemplatePath(pathname, '/a.html')).toStrictEqual('/a.html')
  76. expect(RouterURL.resolveTemplatePath(pathname, './a.html')).toStrictEqual('/hello/a.html')
  77. expect(RouterURL.resolveTemplatePath(pathname, '../a.html')).toStrictEqual('/a.html')
  78. expect(RouterURL.resolveTemplatePath(pathname, '../../a.html')).toStrictEqual('/a.html')
  79. expect(RouterURL.resolveTemplatePath(pathname, '../../../a.html')).toStrictEqual('/a.html')
  80. pathname = new URL('http://localhost/hello/world/').pathname
  81. expect(RouterURL.resolveTemplatePath(pathname, '/a.html')).toStrictEqual('/a.html')
  82. expect(RouterURL.resolveTemplatePath(pathname, './a.html')).toStrictEqual('/hello/world/a.html')
  83. expect(RouterURL.resolveTemplatePath(pathname, '../a.html')).toStrictEqual('/hello/a.html')
  84. expect(RouterURL.resolveTemplatePath(pathname, '../../a.html')).toStrictEqual('/a.html')
  85. expect(RouterURL.resolveTemplatePath(pathname, '../../../a.html')).toStrictEqual('/a.html')
  86. pathname = new URL('http://localhost/').pathname
  87. expect(RouterURL.resolveTemplatePath(pathname, '/a.html')).toStrictEqual('/a.html')
  88. expect(RouterURL.resolveTemplatePath(pathname, './a.html')).toStrictEqual('/a.html')
  89. expect(RouterURL.resolveTemplatePath(pathname, '../a.html')).toStrictEqual('/a.html')
  90. expect(RouterURL.resolveTemplatePath(pathname, '../../a.html')).toStrictEqual('/a.html')
  91. expect(RouterURL.resolveTemplatePath(pathname, '../../../a.html')).toStrictEqual('/a.html')
  92. expect(RouterURL.resolveTemplatePath(pathname)).toStrictEqual()
  93. })
  94. })
  95. })