urlUtils.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, test, it, expect } from 'vitest'
  2. import UrlUtils from "~/services/utils/urlUtils";
  3. describe('join', () => {
  4. test('simple cases', () => {
  5. expect(UrlUtils.join('https://mydomain.com', 'foo')).toEqual('https://mydomain.com/foo')
  6. expect(UrlUtils.join('https://mydomain.com/', '/foo')).toEqual('https://mydomain.com/foo')
  7. expect(UrlUtils.join('https://mydomain.com/', 'foo/')).toEqual('https://mydomain.com/foo/')
  8. })
  9. test('more parts and various types', () => {
  10. expect(UrlUtils.join('https://hoy.com', 'foo', 'bar', 10)).toEqual('https://hoy.com/foo/bar/10')
  11. })
  12. test('tail is empty', () => {
  13. expect(UrlUtils.join('https://hoy.com')).toEqual('https://hoy.com')
  14. })
  15. test('relative uri', () => {
  16. expect(UrlUtils.join('foo', 'bar')).toEqual('foo/bar')
  17. expect(UrlUtils.join('/foo/', '/bar/')).toEqual('/foo/bar/')
  18. })
  19. test('empty base', () => {
  20. expect(UrlUtils.join('', 'foo')).toEqual('/foo')
  21. expect(UrlUtils.join('')).toEqual('')
  22. })
  23. });
  24. describe('prependHttps', () => {
  25. test('uri with no prefix', () => {
  26. expect(UrlUtils.prependHttps('opentalent.fr')).toEqual('https://opentalent.fr')
  27. })
  28. test('uri with http prefix', () => {
  29. expect(UrlUtils.prependHttps('http://opentalent.fr')).toEqual('http://opentalent.fr')
  30. })
  31. test('uri with https prefix', () => {
  32. expect(UrlUtils.prependHttps('https://opentalent.fr')).toEqual('https://opentalent.fr')
  33. })
  34. test('uri with empty string', () => {
  35. expect(UrlUtils.prependHttps('')).toEqual('https://')
  36. })
  37. })
  38. describe('getParameter', () => {
  39. test('get param from a simple url', () => {
  40. expect(UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'foo')).toEqual('abc')
  41. })
  42. test('get a numeric param (no default value)', () => {
  43. expect(UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo')).toEqual(123)
  44. })
  45. test('get a numeric param (default value is also numeric)', () => {
  46. expect(UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 1)).toEqual(123)
  47. })
  48. test('get a numeric param (default value is not numeric)', () => {
  49. expect(UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 'hello')).toEqual('123')
  50. })
  51. test('try to get non-existing param (no default value)', () => {
  52. expect(UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar')).toEqual(null)
  53. })
  54. test('try to get non-existing param, providing a default value', () => {
  55. expect(UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar', 'def')).toEqual('def')
  56. })
  57. test('get param from a relative url', () => {
  58. expect(UrlUtils.getParameter('/hello?foo=123', 'foo')).toEqual(123)
  59. })
  60. })
  61. describe('extractIdFromUri', () => {
  62. test('extract id from uri', () => {
  63. expect(UrlUtils.extractIdFromUri('/api/organizations/1000')).toEqual(1000)
  64. })
  65. test('try to extract missing id', () => {
  66. expect(() => UrlUtils.extractIdFromUri('/api/organizations/first')).toThrowError('no id found')
  67. })
  68. test('try to extract id from empty string', () => {
  69. expect(() => UrlUtils.extractIdFromUri('')).toThrowError('no id found')
  70. })
  71. })
  72. describe('split', () => {
  73. test('split relative url', () => {
  74. expect(UrlUtils.split('foo/bar/1')).toEqual(['foo', 'bar', '1'])
  75. })
  76. test('split relative url with prepended slash', () => {
  77. expect(UrlUtils.split('/foo/bar/1')).toEqual(['foo', 'bar', '1'])
  78. })
  79. test('split complete url', () => {
  80. expect(UrlUtils.split('https://test.com/api/access/123')).toEqual(['https:', 'test.com', 'api', 'access', '123'])
  81. })
  82. test('split empty string', () => {
  83. expect(UrlUtils.split('')).toEqual([])
  84. })
  85. })