import { describe, test, it, expect } from 'vitest' import UrlUtils from '~/services/utils/urlUtils' describe('join', () => { test('simple cases', () => { expect(UrlUtils.join('https://mydomain.com', 'foo')).toEqual( 'https://mydomain.com/foo', ) expect(UrlUtils.join('https://mydomain.com/', '/foo')).toEqual( 'https://mydomain.com/foo', ) expect(UrlUtils.join('https://mydomain.com/', 'foo/')).toEqual( 'https://mydomain.com/foo/', ) }) test('more parts and various types', () => { expect(UrlUtils.join('https://hoy.com', 'foo', 'bar', 10)).toEqual( 'https://hoy.com/foo/bar/10', ) }) test('tail is empty', () => { expect(UrlUtils.join('https://hoy.com')).toEqual('https://hoy.com') }) test('relative uri', () => { expect(UrlUtils.join('foo', 'bar')).toEqual('foo/bar') expect(UrlUtils.join('/foo/', '/bar/')).toEqual('/foo/bar/') }) test('empty base', () => { expect(UrlUtils.join('', 'foo')).toEqual('/foo') expect(UrlUtils.join('')).toEqual('') }) }) describe('prependHttps', () => { test('uri with no prefix', () => { expect(UrlUtils.prependHttps('opentalent.fr')).toEqual( 'https://opentalent.fr', ) }) test('uri with http prefix', () => { expect(UrlUtils.prependHttps('http://opentalent.fr')).toEqual( 'http://opentalent.fr', ) }) test('uri with https prefix', () => { expect(UrlUtils.prependHttps('https://opentalent.fr')).toEqual( 'https://opentalent.fr', ) }) test('uri with empty string', () => { expect(UrlUtils.prependHttps('')).toEqual('https://') }) }) describe('getParameter', () => { test('get param from a simple url', () => { expect( UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'foo'), ).toEqual('abc') }) test('get a numeric param (no default value)', () => { expect( UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo'), ).toEqual(123) }) test('get a numeric param (default value is also numeric)', () => { expect( UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 1), ).toEqual(123) }) test('get a numeric param (default value is not numeric)', () => { expect( UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 'hello'), ).toEqual('123') }) test('try to get non-existing param (no default value)', () => { expect( UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar'), ).toEqual(null) }) test('try to get non-existing param, providing a default value', () => { expect( UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar', 'def'), ).toEqual('def') }) test('get param from a relative url', () => { expect(UrlUtils.getParameter('/hello?foo=123', 'foo')).toEqual(123) }) }) describe('extractIdFromUri', () => { test('extract id from uri', () => { expect(UrlUtils.extractIdFromUri('/api/organizations/1000')).toEqual(1000) }) test('try to literal id', () => { expect(UrlUtils.extractIdFromUri('/api/organizations/abc', true)).toEqual( 'abc', ) }) test('try to extract missing id', () => { expect(() => UrlUtils.extractIdFromUri('/api/organizations/first'), ).toThrowError('no id found') }) test('try to extract id from empty string', () => { expect(() => UrlUtils.extractIdFromUri('')).toThrowError('no id found') }) }) describe('split', () => { test('split relative url', () => { expect(UrlUtils.split('foo/bar/1')).toEqual(['foo', 'bar', '1']) }) test('split relative url with prepended slash', () => { expect(UrlUtils.split('/foo/bar/1')).toEqual(['foo', 'bar', '1']) }) test('split complete url', () => { expect(UrlUtils.split('https://test.com/api/access/123')).toEqual([ 'https:', 'test.com', 'api', 'access', '123', ]) }) test('split empty string', () => { expect(UrlUtils.split('')).toEqual([]) }) }) describe('addQuery', () => { test('with empty query', () => { expect(UrlUtils.addQuery('foo/bar', {})).toEqual('/foo/bar') }) test('with simple query', () => { expect(UrlUtils.addQuery('foo/bar', { a: 1 })).toEqual('/foo/bar?a=1') }) test('with longer query', () => { expect(UrlUtils.addQuery('foo/bar', { a: 1, b: 2 })).toEqual( '/foo/bar?a=1&b=2', ) }) test('with existing query', () => { expect(UrlUtils.addQuery('foo/bar?a=1', { b: 2 })).toEqual( '/foo/bar?a=1&b=2', ) }) test('with empty url', () => { expect(UrlUtils.addQuery('', { a: 1 })).toEqual('/?a=1') }) test('with empty url and empty query', () => { expect(UrlUtils.addQuery('', {})).toEqual('/') }) test('with absolute url', () => { expect(UrlUtils.addQuery('https://foo.com/bar/', { a: 1 })).toEqual( 'https://foo.com/bar/?a=1', ) }) test('with number sign in path name', () => { expect(UrlUtils.addQuery('https://foo.com/#/bar/', { a: 1 })).toEqual( 'https://foo.com/#/bar/?a=1', ) }) }) describe('makeIRI', () => { test('valid parameters', () => { expect(UrlUtils.makeIRI('someEntity', 123)).toEqual('/api/someEntity/123') }) test('invalid id', () => { // @ts-ignore expect(() => UrlUtils.makeIRI('someEntity', 'abc')).toThrowError( 'Invalid id : abc', ) }) })