|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { translateUrlForDatadog } from './translateUrlForDatadog'; |
| 3 | + |
| 4 | +describe('translateUrlForDatadog', () => { |
| 5 | + it('removes query string parameters and ensures trailing slash', () => { |
| 6 | + const href = '/orgs/acme/clu-123/browse/data/Users?like=this&and=that'; |
| 7 | + const params = { |
| 8 | + organizationId: 'acme', |
| 9 | + clusterId: 'clu-123', |
| 10 | + databaseName: 'data', |
| 11 | + tableName: 'Users', |
| 12 | + }; |
| 13 | + const result = translateUrlForDatadog(href, params); |
| 14 | + expect(result).toBe('/orgs/$organizationId/$clusterId/browse/$databaseName/$tableName/'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('adds a trailing slash if missing', () => { |
| 18 | + const href = '/orgs/acme/'; |
| 19 | + const result = translateUrlForDatadog(href, {}); |
| 20 | + expect(result).toBe('/orgs/acme/'); |
| 21 | + |
| 22 | + const href2 = '/orgs/acme'; |
| 23 | + const result2 = translateUrlForDatadog(href2, {}); |
| 24 | + expect(result2).toBe('/orgs/acme/'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('replaces multiple parameters in the path', () => { |
| 28 | + const href = '/orgs/org-1/clu-2/instances/ins-3/'; |
| 29 | + const params = { |
| 30 | + organizationId: 'org-1', |
| 31 | + clusterId: 'clu-2', |
| 32 | + instanceId: 'ins-3', |
| 33 | + }; |
| 34 | + const result = translateUrlForDatadog(href, params); |
| 35 | + expect(result).toBe('/orgs/$organizationId/$clusterId/instances/$instanceId/'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('does not replace when param value not present', () => { |
| 39 | + const href = '/orgs/org-1/other/'; |
| 40 | + const params = { |
| 41 | + organizationId: 'org-2', |
| 42 | + }; |
| 43 | + const result = translateUrlForDatadog(href, params); |
| 44 | + expect(result).toBe('/orgs/org-1/other/'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('only replaces whole path segments bounded by slashes', () => { |
| 48 | + const href = '/files/my-org-1-file/'; |
| 49 | + const params = { |
| 50 | + organizationId: 'org-1', |
| 51 | + }; |
| 52 | + const result = translateUrlForDatadog(href, params); |
| 53 | + expect(result).toBe('/files/my-org-1-file/'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('handles URL without trailing slash but parameters at end', () => { |
| 57 | + const href = '/dbs/data/tables/Users'; |
| 58 | + const params = { |
| 59 | + databaseName: 'data', |
| 60 | + tableName: 'Users', |
| 61 | + }; |
| 62 | + const result = translateUrlForDatadog(href, params); |
| 63 | + expect(result).toBe('/dbs/$databaseName/tables/$tableName/'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('works with hash fragments and query strings in full href', () => { |
| 67 | + const href = 'https://example.com/app#/orgs/org-1/clu-2?x=1&y=2'; |
| 68 | + const params = { |
| 69 | + organizationId: 'org-1', |
| 70 | + clusterId: 'clu-2', |
| 71 | + }; |
| 72 | + // The function simply splits on '?', so the query is dropped; the rest remains intact, including protocol and host. |
| 73 | + const result = translateUrlForDatadog(href, params); |
| 74 | + expect(result).toBe('https://example.com/app#/orgs/$organizationId/$clusterId/'); |
| 75 | + }); |
| 76 | +}); |
0 commit comments