url.test.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { isURL, cleanPath, generateUrl, isNextInternalUrl, createDefaultLocaleReplace, } from '../url.js';
  2. describe('next-sitemap/url', () => {
  3. test('isURL : Valid', () => {
  4. expect(isURL('https://example.com')).toBeTruthy();
  5. });
  6. test('isURL : Invalid', () => {
  7. expect(isURL('/someone-relative/path/item.jpg')).toBeFalsy();
  8. });
  9. test('cleanPath : Relative Path', () => {
  10. expect(cleanPath('./epic///awesome///path')).toBe('./epic/awesome/path');
  11. });
  12. test('cleanPath: Public Url', () => {
  13. expect(cleanPath('https://www.example.com//epic///awesome///path')).toBe('https://www.example.com/epic/awesome/path');
  14. });
  15. test('generateUrl: with relative slug', () => {
  16. const url = generateUrl('https://base.example.com', '//awesome/path');
  17. expect(url).toBe('https://base.example.com/awesome/path');
  18. });
  19. test('generateUrl: with external slug', () => {
  20. const url = generateUrl('https://base.example.com', 'https://cdn.another.site/new//path');
  21. expect(url).toBe('https://cdn.another.site/new/path');
  22. });
  23. test('isNextInternalUrl', () => {
  24. expect(isNextInternalUrl('/_app')).toBeTruthy();
  25. expect(isNextInternalUrl('/404')).toBeTruthy();
  26. expect(isNextInternalUrl('/500')).toBeTruthy();
  27. expect(isNextInternalUrl('/_random')).toBeTruthy();
  28. expect(isNextInternalUrl('/_middleware')).toBeTruthy();
  29. expect(isNextInternalUrl('/about/_middleware')).toBeTruthy();
  30. expect(isNextInternalUrl('/some_url/about/_middleware')).toBeTruthy();
  31. expect(isNextInternalUrl('/projects/[id]/_middleware')).toBeTruthy();
  32. });
  33. test('isNextInternalUrl: url params', () => {
  34. expect(isNextInternalUrl('/[id]')).toBeTruthy();
  35. expect(isNextInternalUrl('/blog/[id]')).toBeTruthy();
  36. });
  37. test('isNextInternalUrl: allow urls with underscore`', () => {
  38. expect(isNextInternalUrl('/_some_url')).toBeTruthy();
  39. expect(isNextInternalUrl('/some_url/[param]')).toBeTruthy();
  40. expect(isNextInternalUrl('/some_url')).toBeFalsy();
  41. expect(isNextInternalUrl('/some-404')).toBeFalsy();
  42. expect(isNextInternalUrl('/some-500')).toBeFalsy();
  43. });
  44. test('createDefaultLocaleReplace: replaces default locale within path`', () => {
  45. const replaceDefaultLocale = createDefaultLocaleReplace('en-US');
  46. expect(replaceDefaultLocale('/')).toBe('/');
  47. expect(replaceDefaultLocale('/en-US')).toBe('/');
  48. expect(replaceDefaultLocale('/en-US/')).toBe('/');
  49. expect(replaceDefaultLocale('/en-US/home')).toBe('/home');
  50. expect(replaceDefaultLocale('/en-US/home/')).toBe('/home/');
  51. expect(replaceDefaultLocale('/en-US-home')).toBe('/en-US-home');
  52. expect(replaceDefaultLocale('/en-USA/home')).toBe('/en-USA/home');
  53. expect(replaceDefaultLocale('/fr')).toBe('/fr');
  54. expect(replaceDefaultLocale('/fr/about')).toBe('/fr/about');
  55. });
  56. });