|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { addFeatureParamToHref } from '@/frame/components/lib/add-feature-param-to-href' |
| 4 | + |
| 5 | +const restLink = |
| 6 | + 'https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-a-repository' |
| 7 | + |
| 8 | +describe('addFeatureParamToHref', () => { |
| 9 | + test('keeps query and fragment when there is no feature param', () => { |
| 10 | + expect(addFeatureParamToHref(restLink, '')).toBe(restLink) |
| 11 | + expect(addFeatureParamToHref(restLink, '?foo=bar')).toBe(restLink) |
| 12 | + }) |
| 13 | + |
| 14 | + test('adds feature param after the existing query and before the fragment', () => { |
| 15 | + expect(addFeatureParamToHref(restLink, '?feature=foo')).toBe( |
| 16 | + 'https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28&feature=foo#create-a-repository', |
| 17 | + ) |
| 18 | + }) |
| 19 | + |
| 20 | + test('adds feature param before the fragment when there is no query', () => { |
| 21 | + expect(addFeatureParamToHref('https://docs.github.com/en/foo#section', '?feature=foo')).toBe( |
| 22 | + 'https://docs.github.com/en/foo?feature=foo#section', |
| 23 | + ) |
| 24 | + }) |
| 25 | + |
| 26 | + test('adds feature param to a plain link', () => { |
| 27 | + expect(addFeatureParamToHref('https://docs.github.com/en/foo', '?feature=foo')).toBe( |
| 28 | + 'https://docs.github.com/en/foo?feature=foo', |
| 29 | + ) |
| 30 | + }) |
| 31 | + |
| 32 | + test('keeps relative links relative', () => { |
| 33 | + expect(addFeatureParamToHref('/en/foo?tool=cli', '?feature=foo')).toBe( |
| 34 | + '/en/foo?tool=cli&feature=foo', |
| 35 | + ) |
| 36 | + }) |
| 37 | + |
| 38 | + test('replaces an existing feature param instead of duplicating it', () => { |
| 39 | + expect( |
| 40 | + addFeatureParamToHref('https://docs.github.com/en/foo?feature=old&tool=cli', '?feature=foo'), |
| 41 | + ).toBe('https://docs.github.com/en/foo?feature=foo&tool=cli') |
| 42 | + }) |
| 43 | + |
| 44 | + test('leaves in-page fragment links unchanged', () => { |
| 45 | + expect(addFeatureParamToHref('#section', '?feature=foo')).toBe('#section') |
| 46 | + }) |
| 47 | + |
| 48 | + test('leaves an empty href empty', () => { |
| 49 | + expect(addFeatureParamToHref('', '?feature=foo')).toBe('') |
| 50 | + }) |
| 51 | +}) |
0 commit comments