|
| 1 | +import { Matcher } from '../../src/goals/Goals'; |
| 2 | +import { matchesUrl } from '../../src/goals/GoalTracker'; |
| 3 | + |
| 4 | +it.each([ |
| 5 | + ['https://example.com', '', '', 'https://example.com'], |
| 6 | + [ |
| 7 | + 'https://example.com?potato=true#hash', |
| 8 | + '?potato=true', |
| 9 | + '#hash', |
| 10 | + 'https://example.com?potato=true#hash', |
| 11 | + ], |
| 12 | +])('returns true for exact match with "exact" matcher kind', (href, query, hash, matcherUrl) => { |
| 13 | + const matcher: Matcher = { kind: 'exact', url: matcherUrl }; |
| 14 | + const result = matchesUrl(matcher, href, query, hash); |
| 15 | + expect(result).toBe(true); |
| 16 | +}); |
| 17 | + |
| 18 | +it.each([ |
| 19 | + ['https://example.com/potato', '', '', 'https://example.com'], |
| 20 | + [ |
| 21 | + 'https://example.com?potato=true#hash', |
| 22 | + '?potato=true', |
| 23 | + '#hash', |
| 24 | + 'https://example.com?potato=true#brown', |
| 25 | + ], |
| 26 | +])('returns false for non-matching "exact" matcher kind', (href, query, hash, matcherUrl) => { |
| 27 | + const matcher: Matcher = { kind: 'exact', url: matcherUrl }; |
| 28 | + const result = matchesUrl(matcher, href, query, hash); |
| 29 | + expect(result).toBe(false); |
| 30 | +}); |
| 31 | + |
| 32 | +it('returns true for canonical match with "canonical" matcher kind', () => { |
| 33 | + // For this type of match the hash and query parameters are not included. |
| 34 | + const matcher: Matcher = { kind: 'canonical', url: 'https://example.com/some-path' }; |
| 35 | + const result = matchesUrl( |
| 36 | + matcher, |
| 37 | + 'https://example.com/some-path?query=1#hash', |
| 38 | + '?query=1', |
| 39 | + '#hash', |
| 40 | + ); |
| 41 | + expect(result).toBe(true); |
| 42 | +}); |
| 43 | + |
| 44 | +it('returns true for substring match with "substring" matcher kind', () => { |
| 45 | + const matcher: Matcher = { kind: 'substring', substring: 'example' }; |
| 46 | + const result = matchesUrl( |
| 47 | + matcher, |
| 48 | + 'https://example.com/some-path?query=1#hash', |
| 49 | + '?query=1', |
| 50 | + '#hash', |
| 51 | + ); |
| 52 | + expect(result).toBe(true); |
| 53 | +}); |
| 54 | + |
| 55 | +it('returns false for non-matching substring with "substring" matcher kind', () => { |
| 56 | + const matcher: Matcher = { kind: 'substring', substring: 'nonexistent' }; |
| 57 | + const result = matchesUrl( |
| 58 | + matcher, |
| 59 | + 'https://example.com/some-path?query=1#hash', |
| 60 | + '?query=1', |
| 61 | + '#hash', |
| 62 | + ); |
| 63 | + expect(result).toBe(false); |
| 64 | +}); |
| 65 | + |
| 66 | +it('returns true for regex match with "regex" matcher kind', () => { |
| 67 | + const matcher: Matcher = { kind: 'regex', pattern: 'example\\.com' }; |
| 68 | + const result = matchesUrl( |
| 69 | + matcher, |
| 70 | + 'https://example.com/some-path?query=1#hash', |
| 71 | + '?query=1', |
| 72 | + '#hash', |
| 73 | + ); |
| 74 | + expect(result).toBe(true); |
| 75 | +}); |
| 76 | + |
| 77 | +it('returns false for non-matching regex with "regex" matcher kind', () => { |
| 78 | + const matcher: Matcher = { kind: 'regex', pattern: 'nonexistent\\.com' }; |
| 79 | + const result = matchesUrl( |
| 80 | + matcher, |
| 81 | + 'https://example.com/some-path?query=1#hash', |
| 82 | + '?query=1', |
| 83 | + '#hash', |
| 84 | + ); |
| 85 | + expect(result).toBe(false); |
| 86 | +}); |
| 87 | + |
| 88 | +it('includes the hash for "path-like" hashes for "substring" matchers', () => { |
| 89 | + const matcher: Matcher = { kind: 'substring', substring: 'example' }; |
| 90 | + const result = matchesUrl( |
| 91 | + matcher, |
| 92 | + 'https://example.com/some-path?query=1#/hash/path', |
| 93 | + '?query=1', |
| 94 | + '#/hash/path', |
| 95 | + ); |
| 96 | + expect(result).toBe(true); |
| 97 | +}); |
| 98 | + |
| 99 | +it('includes the hash for "path-like" hashes for "regex" matchers', () => { |
| 100 | + const matcher: Matcher = { kind: 'regex', pattern: 'hash' }; |
| 101 | + const result = matchesUrl( |
| 102 | + matcher, |
| 103 | + 'https://example.com/some-path?query=1#/hash/path', |
| 104 | + '?query=1', |
| 105 | + '#/hash/path', |
| 106 | + ); |
| 107 | + expect(result).toBe(true); |
| 108 | +}); |
| 109 | + |
| 110 | +it('returns false for unsupported matcher kind', () => { |
| 111 | + // @ts-expect-error |
| 112 | + const matcher: Matcher = { kind: 'unsupported' }; |
| 113 | + const result = matchesUrl(matcher, 'https://example.com', '', ''); |
| 114 | + expect(result).toBe(false); |
| 115 | +}); |
0 commit comments