routeSlug.utils.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, test } from 'vitest'
  2. import { buildOrgUrl } from '@/components/interfaces/Organization/Organization.utils'
  3. describe('buildOrgUrl', () => {
  4. describe('when slug is undefined (bare /org/_ route)', () => {
  5. test('returns the org project list url', () => {
  6. expect(buildOrgUrl({ slug: undefined, orgSlug: 'my-org', queryString: '' })).toBe(
  7. '/org/my-org'
  8. )
  9. })
  10. test('appends query string when present', () => {
  11. expect(buildOrgUrl({ slug: undefined, orgSlug: 'my-org', queryString: 'foo=bar' })).toBe(
  12. '/org/my-org?foo=bar'
  13. )
  14. })
  15. test('does not append a bare ? when query string is empty', () => {
  16. expect(buildOrgUrl({ slug: undefined, orgSlug: 'my-org', queryString: '' })).not.toContain(
  17. '?'
  18. )
  19. })
  20. })
  21. describe('when slug is a string (next.js router quirk — single segment)', () => {
  22. test('returns the org project list url, ignoring the string slug', () => {
  23. expect(buildOrgUrl({ slug: 'general', orgSlug: 'my-org', queryString: '' })).toBe(
  24. '/org/my-org'
  25. )
  26. })
  27. test('appends query string when present', () => {
  28. expect(buildOrgUrl({ slug: 'general', orgSlug: 'my-org', queryString: 'ref=abc' })).toBe(
  29. '/org/my-org?ref=abc'
  30. )
  31. })
  32. })
  33. describe('when slug is an array (sub-path route)', () => {
  34. test('preserves a single-segment sub-path', () => {
  35. expect(buildOrgUrl({ slug: ['general'], orgSlug: 'my-org', queryString: '' })).toBe(
  36. '/org/my-org/general'
  37. )
  38. })
  39. test('preserves a multi-segment sub-path', () => {
  40. expect(
  41. buildOrgUrl({ slug: ['settings', 'billing'], orgSlug: 'my-org', queryString: '' })
  42. ).toBe('/org/my-org/settings/billing')
  43. })
  44. test('appends query string when present', () => {
  45. expect(
  46. buildOrgUrl({ slug: ['general'], orgSlug: 'my-org', queryString: 'foo=1&bar=2' })
  47. ).toBe('/org/my-org/general?foo=1&bar=2')
  48. })
  49. test('does not append a bare ? when query string is empty', () => {
  50. expect(buildOrgUrl({ slug: ['general'], orgSlug: 'my-org', queryString: '' })).not.toContain(
  51. '?'
  52. )
  53. })
  54. })
  55. })