|
| 1 | +/** |
| 2 | + * Tests for elementDestinationUrl utils (hasValidMustacheOnly, isValidElementDestinationURL). |
| 3 | + * Written for 100% line and branch coverage of src/utils/elementDestinationUrl.js. |
| 4 | + */ |
| 5 | +import { |
| 6 | + isValidElementDestinationURL, |
| 7 | + hasValidMustacheOnly, |
| 8 | +} from '@/utils/elementDestinationUrl'; |
| 9 | + |
| 10 | +describe('elementDestinationUrl', () => { |
| 11 | + describe('hasValidMustacheOnly', () => { |
| 12 | + it('returns false when string does not contain {{', () => { |
| 13 | + expect(hasValidMustacheOnly('no mustache here')).toBe(false); |
| 14 | + expect(hasValidMustacheOnly('https://example.com')).toBe(false); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns true when string has only valid placeholders', () => { |
| 18 | + expect(hasValidMustacheOnly('{{var}}')).toBe(true); |
| 19 | + expect(hasValidMustacheOnly('{{a}}{{b}}')).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns true for placeholder with spaces around variable name', () => { |
| 23 | + expect(hasValidMustacheOnly('{{ x }}')).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns false when string contains empty mustache {{}} or {{ }}', () => { |
| 27 | + expect(hasValidMustacheOnly('{{}}')).toBe(false); |
| 28 | + expect(hasValidMustacheOnly('{{ }}')).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns false when string has stray {{ (unclosed)', () => { |
| 32 | + expect(hasValidMustacheOnly('{{unclosed')).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns false when string has stray }} after valid placeholder', () => { |
| 36 | + expect(hasValidMustacheOnly('{{a}} }}')).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns false when valid placeholders are followed by empty {{}}', () => { |
| 40 | + expect(hasValidMustacheOnly('{{server}}/{{_request.id}}{{}}')).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns false when skeleton has stray single { or }', () => { |
| 44 | + expect(hasValidMustacheOnly('{{v}} {')).toBe(false); |
| 45 | + expect(hasValidMustacheOnly('https://127.0.0.5:8092/admin/users/12/edit{{v}} {{v}} {')).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns false when literal parts form invalid URL', () => { |
| 49 | + expect(hasValidMustacheOnly('{{variable}} / `[[[][∫ad')).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns true when URL has scheme (HAS_SCHEME branch)', () => { |
| 53 | + expect(hasValidMustacheOnly('https://host/{{path}}')).toBe(true); |
| 54 | + expect(hasValidMustacheOnly('http://example.com/{{id}}/view')).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns true when no scheme so http:// is prepended', () => { |
| 58 | + expect(hasValidMustacheOnly('{{host}}/path')).toBe(true); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('isValidElementDestinationURL', () => { |
| 63 | + describe('non-string or empty', () => { |
| 64 | + it('returns false for non-string values', () => { |
| 65 | + expect(isValidElementDestinationURL(null)).toBe(false); |
| 66 | + expect(isValidElementDestinationURL(undefined)).toBe(false); |
| 67 | + expect(isValidElementDestinationURL(123)).toBe(false); |
| 68 | + expect(isValidElementDestinationURL(false)).toBe(false); |
| 69 | + expect(isValidElementDestinationURL({})).toBe(false); |
| 70 | + expect(isValidElementDestinationURL([])).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns false for empty string', () => { |
| 74 | + expect(isValidElementDestinationURL('')).toBe(false); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns false for whitespace-only string', () => { |
| 78 | + expect(isValidElementDestinationURL(' ')).toBe(false); |
| 79 | + expect(isValidElementDestinationURL('\t\n')).toBe(false); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('Mustache template (contains {{)', () => { |
| 84 | + it('returns true for valid single placeholder', () => { |
| 85 | + expect(isValidElementDestinationURL('{{var}}')).toBe(true); |
| 86 | + expect(isValidElementDestinationURL('{{ APP_URL }}')).toBe(true); |
| 87 | + expect(isValidElementDestinationURL(' {{path}} ')).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns true for valid multiple placeholders', () => { |
| 91 | + expect(isValidElementDestinationURL('{{a}}{{b}}')).toBe(true); |
| 92 | + expect(isValidElementDestinationURL('{{x}}/{{y}}')).toBe(true); |
| 93 | + }); |
| 94 | + |
| 95 | + it('returns true for URL with valid Mustache placeholders', () => { |
| 96 | + expect(isValidElementDestinationURL('https://host/{{path}}')).toBe(true); |
| 97 | + expect(isValidElementDestinationURL('http://example.com/{{id}}/view')).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it('returns false for empty placeholder {{}}', () => { |
| 101 | + expect(isValidElementDestinationURL('{{}}')).toBe(false); |
| 102 | + }); |
| 103 | + |
| 104 | + it('returns false for placeholder with only spaces {{ }}', () => { |
| 105 | + expect(isValidElementDestinationURL('{{ }}')).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it('returns false for unclosed Mustache (stray {{)', () => { |
| 109 | + expect(isValidElementDestinationURL('{{unclosed')).toBe(false); |
| 110 | + expect(isValidElementDestinationURL('{{a}} {{')).toBe(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns false for stray closing braces }}', () => { |
| 114 | + expect(isValidElementDestinationURL('{{a}} }}')).toBe(false); |
| 115 | + expect(isValidElementDestinationURL('}}solo')).toBe(false); |
| 116 | + }); |
| 117 | + |
| 118 | + it('returns false for placeholder with space inside variable name', () => { |
| 119 | + expect(isValidElementDestinationURL('{{var2 var2}}')).toBe(false); |
| 120 | + }); |
| 121 | + |
| 122 | + it('returns false for URL with valid placeholders plus empty {{}}', () => { |
| 123 | + expect(isValidElementDestinationURL('{{server}}/{{_request.id}}{{}}')).toBe(false); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns false when literal parts form invalid URL', () => { |
| 127 | + expect(isValidElementDestinationURL('https://127.0.0.5:8092/admin/users/12/edit{{v}} {{v}} {')).toBe(false); |
| 128 | + expect(isValidElementDestinationURL('{{variable}} / `[[[][∫ad')).toBe(false); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('plain URL (no Mustache)', () => { |
| 133 | + it('returns true for valid HTTP URL', () => { |
| 134 | + expect(isValidElementDestinationURL('http://example.com')).toBe(true); |
| 135 | + expect(isValidElementDestinationURL('http://a.b')).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + it('returns true for valid HTTPS URL', () => { |
| 139 | + expect(isValidElementDestinationURL('https://example.com')).toBe(true); |
| 140 | + expect(isValidElementDestinationURL('https://example.com/path?q=1')).toBe(true); |
| 141 | + }); |
| 142 | + |
| 143 | + it('returns true for URL with trimmed whitespace', () => { |
| 144 | + expect(isValidElementDestinationURL(' https://example.com ')).toBe(true); |
| 145 | + }); |
| 146 | + |
| 147 | + it('returns false for invalid URL (exercises catch branch)', () => { |
| 148 | + expect(isValidElementDestinationURL('not a url')).toBe(false); |
| 149 | + expect(isValidElementDestinationURL('://bad')).toBe(false); |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments