|
| 1 | +import { |
| 2 | + getBarePathFromPath, |
| 3 | + isExternalUrl, |
| 4 | + isRelativeUrl, |
| 5 | + isSubrouteOf, |
| 6 | + removeTrailingSlashes, |
| 7 | + toHtmlId, |
| 8 | +} from './urls' |
| 9 | + |
| 10 | +const relativeUrls = [ |
| 11 | + '', |
| 12 | + 'a', |
| 13 | + 'a/', |
| 14 | + 'abcd', |
| 15 | + 'abcd/', |
| 16 | + 'abcd#something-something', |
| 17 | + 'abcd/#hash.hash', |
| 18 | + 'deep/path/to/page.html', |
| 19 | + 'deep/path/to/page.html#hash', |
| 20 | + ':something', |
| 21 | + '#hash-link', |
| 22 | +] |
| 23 | + |
| 24 | +const absoluteUrls = [ |
| 25 | + '/a', |
| 26 | + '/a/', |
| 27 | + '/abcd', |
| 28 | + '/abcd/', |
| 29 | + '/abcd#something-something', |
| 30 | + '/abcd/#hash.hash', |
| 31 | + '/deep/path/to/page.html', |
| 32 | + '/deep/path/to/page.html#hash', |
| 33 | +] |
| 34 | + |
| 35 | +const externalUrls = [ |
| 36 | + // Links with protocols |
| 37 | + '//google.com', |
| 38 | + 'http://google.com', |
| 39 | + 'https://google.com', |
| 40 | + 'ftp://google.com', |
| 41 | + 'gopher://google.com', |
| 42 | + 'HTTP://google.com', |
| 43 | + 'HTTPS://google.com', |
| 44 | + 'FTP://google.com', |
| 45 | + 'GOPHER://google.com', |
| 46 | + '234h+-.:something', // Weird, but valid protocol |
| 47 | + |
| 48 | + // Alternative links |
| 49 | + 'mailto:', |
| 50 | + |
| 51 | + 'tel:', |
| 52 | + 'sms:', |
| 53 | + 'callto:', |
| 54 | + 'tel:+1.123.345.6342', |
| 55 | + 'sms:+1.123.345.6342', |
| 56 | + 'callto:+1.123.345.6342', |
| 57 | +] |
| 58 | + |
| 59 | +describe('URL utils', () => { |
| 60 | + it('should detect relative urls', () => { |
| 61 | + relativeUrls.forEach((url) => { |
| 62 | + expect(isRelativeUrl(url)).toBeTruthy() |
| 63 | + }) |
| 64 | + absoluteUrls.forEach((url) => { |
| 65 | + expect(isRelativeUrl(url)).toBeFalsy() |
| 66 | + }) |
| 67 | + externalUrls.forEach((url) => { |
| 68 | + expect(isRelativeUrl(url)).toBeFalsy() |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should detect external urls', () => { |
| 73 | + relativeUrls.forEach((url) => { |
| 74 | + expect(isExternalUrl(url)).toBeFalsy() |
| 75 | + }) |
| 76 | + absoluteUrls.forEach((url) => { |
| 77 | + expect(isExternalUrl(url)).toBeFalsy() |
| 78 | + }) |
| 79 | + externalUrls.forEach((url) => { |
| 80 | + expect(isExternalUrl(url)).toBeTruthy() |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should remove trailing slashes', () => { |
| 85 | + expect(removeTrailingSlashes(null)).toBe(null) |
| 86 | + expect(removeTrailingSlashes(undefined)).toBe(undefined) |
| 87 | + expect(removeTrailingSlashes('/')).toBe('') |
| 88 | + expect(removeTrailingSlashes('//')).toBe('') |
| 89 | + expect(removeTrailingSlashes('///////')).toBe('') |
| 90 | + expect(removeTrailingSlashes('/abc/a/')).toBe('/abc/a') |
| 91 | + expect(removeTrailingSlashes('/abc/a////')).toBe('/abc/a') |
| 92 | + expect(removeTrailingSlashes('/abc////a')).toBe('/abc////a') |
| 93 | + expect(removeTrailingSlashes('http://a.b.c/#d')).toBe('http://a.b.c/#d') |
| 94 | + }) |
| 95 | + |
| 96 | + it('should detect subroutes', () => { |
| 97 | + expect(isSubrouteOf('/', '')).toBeTruthy() |
| 98 | + expect(isSubrouteOf('/something', '/')).toBeTruthy() |
| 99 | + expect(isSubrouteOf('/a/b/cdefg/h/', '/a/b/cdefg/h/')).toBeTruthy() |
| 100 | + expect(isSubrouteOf('/a/b/cdefg/h/ijk', '/a/b/cdefg/h/')).toBeTruthy() |
| 101 | + expect( |
| 102 | + isSubrouteOf('http://google.com/?x=something', 'http://google.com') |
| 103 | + ).toBeTruthy() |
| 104 | + |
| 105 | + expect(isSubrouteOf('', '/')).toBeFalsy() |
| 106 | + expect(isSubrouteOf('https://google.com', 'http://google.com')).toBeFalsy() |
| 107 | + }) |
| 108 | + |
| 109 | + it('should create valid id attributes', () => { |
| 110 | + expect(toHtmlId('some%#$long9 string-with_chars')).toBe( |
| 111 | + 'some-long9-string-with_chars' |
| 112 | + ) |
| 113 | + expect(toHtmlId('123 numbers')).toBe('_123-numbers') |
| 114 | + expect(toHtmlId('')).toBe('') |
| 115 | + expect(toHtmlId(' ')).toBe('') |
| 116 | + expect(toHtmlId('a')).toBe('a') |
| 117 | + expect(toHtmlId(' abc')).toBe('abc') |
| 118 | + expect(toHtmlId('-things')).toBe('things') |
| 119 | + expect(toHtmlId('_things')).toBe('_things') |
| 120 | + expect(toHtmlId('_thiñgs')).toBe('_thi-gs') |
| 121 | + expect(toHtmlId('CAPITALS')).toBe('capitals') |
| 122 | + }) |
| 123 | + |
| 124 | + it('should get paths without url params or hashes', () => { |
| 125 | + expect(getBarePathFromPath('')).toBe('') |
| 126 | + expect(getBarePathFromPath('abc')).toBe('abc') |
| 127 | + expect(getBarePathFromPath('abc//')).toBe('abc//') |
| 128 | + expect(getBarePathFromPath('#hash?var=val')).toBe('') |
| 129 | + expect(getBarePathFromPath('path#hash?var=val')).toBe('path') |
| 130 | + expect(getBarePathFromPath('//path#hash?var=val')).toBe('//path') |
| 131 | + expect(getBarePathFromPath('path/#/morepath')).toBe('path/') |
| 132 | + expect(getBarePathFromPath('//path/#hash?var=val')).toBe('//path/') |
| 133 | + expect(getBarePathFromPath('http://path.com?var=val')).toBe( |
| 134 | + 'http://path.com' |
| 135 | + ) |
| 136 | + }) |
| 137 | +}) |
0 commit comments