|
| 1 | +import {usePrivacyLink} from 'frontend/usePrivacyLink'; |
| 2 | + |
| 3 | +import {renderHookInEntry} from 'support'; |
| 4 | + |
| 5 | +describe('usePrivacyLink', () => { |
| 6 | + it('returns url and label without modifications when called without args', () => { |
| 7 | + const {result} = renderHookInEntry( |
| 8 | + () => usePrivacyLink(), { |
| 9 | + seed: { |
| 10 | + legalInfo: { |
| 11 | + privacy: {url: 'https://example.com/privacy', label: 'Privacy'} |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | + ); |
| 16 | + |
| 17 | + expect(result.current.url).toEqual('https://example.com/privacy'); |
| 18 | + expect(result.current.label).toEqual('Privacy'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns empty string when privacy url is empty', () => { |
| 22 | + const {result} = renderHookInEntry( |
| 23 | + () => usePrivacyLink({vendors: 'spotify'}), { |
| 24 | + seed: { |
| 25 | + legalInfo: { |
| 26 | + privacy: {url: ''} |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + ); |
| 31 | + |
| 32 | + expect(result.current.url).toEqual(''); |
| 33 | + }); |
| 34 | + |
| 35 | + it('appends vendors param and consent hash to absolute url', () => { |
| 36 | + const {result} = renderHookInEntry( |
| 37 | + () => usePrivacyLink({vendors: 'spotify'}), { |
| 38 | + seed: { |
| 39 | + legalInfo: { |
| 40 | + privacy: {url: 'https://example.com/privacy'} |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + ); |
| 45 | + |
| 46 | + expect(result.current.url) |
| 47 | + .toEqual('https://example.com/privacy?vendors=spotify#consent'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('appends vendors param to url with existing params', () => { |
| 51 | + const {result} = renderHookInEntry( |
| 52 | + () => usePrivacyLink({vendors: 'spotify'}), { |
| 53 | + seed: { |
| 54 | + legalInfo: { |
| 55 | + privacy: {url: 'https://example.com/privacy?lang=de'} |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + expect(result.current.url) |
| 62 | + .toEqual('https://example.com/privacy?lang=de&vendors=spotify#consent'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('preserves protocol-relative url format', () => { |
| 66 | + const {result} = renderHookInEntry( |
| 67 | + () => usePrivacyLink({vendors: 'spotify'}), { |
| 68 | + seed: { |
| 69 | + legalInfo: { |
| 70 | + privacy: {url: '//example.com/privacy?lang=de'} |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + expect(result.current.url) |
| 77 | + .toEqual('//example.com/privacy?lang=de&vendors=spotify#consent'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('handles relative path', () => { |
| 81 | + const {result} = renderHookInEntry( |
| 82 | + () => usePrivacyLink({vendors: 'spotify'}), { |
| 83 | + seed: { |
| 84 | + legalInfo: { |
| 85 | + privacy: {url: '/privacy?lang=de'} |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + ); |
| 90 | + |
| 91 | + expect(result.current.url) |
| 92 | + .toEqual('/privacy?lang=de&vendors=spotify#consent'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('supports comma-separated vendor names', () => { |
| 96 | + const {result} = renderHookInEntry( |
| 97 | + () => usePrivacyLink({vendors: 'spotify,youtube'}), { |
| 98 | + seed: { |
| 99 | + legalInfo: { |
| 100 | + privacy: {url: 'https://example.com/privacy'} |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + ); |
| 105 | + |
| 106 | + expect(result.current.url) |
| 107 | + .toEqual('https://example.com/privacy?vendors=spotify%2Cyoutube#consent'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('returns link props with href and target', () => { |
| 111 | + const {result} = renderHookInEntry( |
| 112 | + () => usePrivacyLink(), { |
| 113 | + seed: { |
| 114 | + legalInfo: { |
| 115 | + privacy: {url: 'https://example.com/privacy'} |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + ); |
| 120 | + |
| 121 | + expect(result.current.props).toEqual({ |
| 122 | + href: 'https://example.com/privacy', |
| 123 | + target: '_blank', |
| 124 | + rel: 'noreferrer noopener' |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns props with onClick for javascript: privacy settings url', () => { |
| 129 | + const {result} = renderHookInEntry( |
| 130 | + // eslint-disable-next-line no-script-url |
| 131 | + () => usePrivacyLink(), { |
| 132 | + seed: { |
| 133 | + legalInfo: { |
| 134 | + privacy: {url: 'javascript:pageflowDisplayPrivacySettings()'} |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + ); |
| 139 | + |
| 140 | + expect(result.current.props.href).toEqual('#privacySettings'); |
| 141 | + expect(result.current.props.onClick).toBeInstanceOf(Function); |
| 142 | + expect(result.current.props.target).toBeUndefined(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns link props with vendors param in href', () => { |
| 146 | + const {result} = renderHookInEntry( |
| 147 | + () => usePrivacyLink({vendors: 'spotify'}), { |
| 148 | + seed: { |
| 149 | + legalInfo: { |
| 150 | + privacy: {url: 'https://example.com/privacy'} |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + ); |
| 155 | + |
| 156 | + expect(result.current.props).toEqual({ |
| 157 | + href: 'https://example.com/privacy?vendors=spotify#consent', |
| 158 | + target: '_blank', |
| 159 | + rel: 'noreferrer noopener' |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments