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