|
| 1 | +/* eslint-disable no-underscore-dangle, no-console */ |
| 2 | +import { runScriptlet, clearGlobalProps } from '../helpers'; |
| 3 | + |
| 4 | +const { test, module } = QUnit; |
| 5 | +const name = 'prevent-navigation'; |
| 6 | + |
| 7 | +const nativeConsoleLog = window.console.log; |
| 8 | + |
| 9 | +const beforeEach = () => { |
| 10 | + window.__debug = () => { |
| 11 | + window.hit = 'FIRED'; |
| 12 | + }; |
| 13 | +}; |
| 14 | + |
| 15 | +const afterEach = () => { |
| 16 | + clearGlobalProps('hit', '__debug'); |
| 17 | + window.console.log = nativeConsoleLog; |
| 18 | +}; |
| 19 | + |
| 20 | +module(name, { beforeEach, afterEach }); |
| 21 | + |
| 22 | +const isSupported = typeof navigation !== 'undefined'; |
| 23 | + |
| 24 | +if (!isSupported) { |
| 25 | + test('unsupported', (assert) => { |
| 26 | + assert.ok(true, 'Browser does not support it'); |
| 27 | + }); |
| 28 | +} else { |
| 29 | + test('Prevent website reload', (assert) => { |
| 30 | + assert.expect(1); |
| 31 | + const url = 'location.href'; |
| 32 | + |
| 33 | + const scriptletArgs = [url]; |
| 34 | + runScriptlet(name, scriptletArgs); |
| 35 | + |
| 36 | + try { |
| 37 | + window.location.reload(); |
| 38 | + assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); |
| 39 | + } catch (error) { |
| 40 | + assert.ok(false, `Navigation should be prevented, but an error was thrown: ${error}`); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + test('Prevent navigation to "advert" URL', (assert) => { |
| 45 | + assert.expect(1); |
| 46 | + const url = 'advert'; |
| 47 | + |
| 48 | + const scriptletArgs = [url]; |
| 49 | + runScriptlet(name, scriptletArgs); |
| 50 | + |
| 51 | + try { |
| 52 | + window.location.href = '/advert'; |
| 53 | + assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); |
| 54 | + } catch (error) { |
| 55 | + assert.ok(false, `Navigation should be prevented, but an error was thrown: ${error}`); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + test('Prevent navigation - regex', (assert) => { |
| 60 | + const url = '/adblock.*enabled/'; |
| 61 | + |
| 62 | + const scriptletArgs = [url]; |
| 63 | + runScriptlet(name, scriptletArgs); |
| 64 | + |
| 65 | + window.location.href = '/adblock-test-foo-bar-enabled'; |
| 66 | + |
| 67 | + assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); |
| 68 | + }); |
| 69 | + |
| 70 | + test('Prevent navigation - URL does not match', (assert) => { |
| 71 | + assert.expect(1); |
| 72 | + const url = 'SHOULD_NOT_MATCH'; |
| 73 | + const redirectUrl = '/test-foo'; |
| 74 | + |
| 75 | + const scriptletArgs = [url]; |
| 76 | + runScriptlet(name, scriptletArgs); |
| 77 | + |
| 78 | + // Prevent navigation to "redirectUrl", if it's not blocked by the scriptlet |
| 79 | + // When it's blocked by the scriptlet, the test will fail as "hit" will be set to "FIRED" |
| 80 | + window.navigation.addEventListener('navigate', (event) => { |
| 81 | + const { url } = event.destination; |
| 82 | + if (url.includes(redirectUrl)) { |
| 83 | + event.preventDefault(); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + try { |
| 88 | + window.location.href = redirectUrl; |
| 89 | + assert.strictEqual(window.hit, undefined, 'hit should NOT fire'); |
| 90 | + } catch (error) { |
| 91 | + assert.ok(false, `Navigation should be prevented, but an error was thrown: ${error}`); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + test('Log navigation URL to console', (assert) => { |
| 96 | + assert.expect(2); |
| 97 | + const redirectUrl = '/log-navigation-test'; |
| 98 | + const expectedMessage = 'log-navigation-test'; |
| 99 | + let testPassed = false; |
| 100 | + |
| 101 | + runScriptlet(name); |
| 102 | + |
| 103 | + // Need to prevent navigation to "redirectUrl", because it's not blocked by the scriptlet |
| 104 | + window.navigation.addEventListener('navigate', (event) => { |
| 105 | + const { url } = event.destination; |
| 106 | + if (url.includes(redirectUrl)) { |
| 107 | + event.preventDefault(); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + // Override "console.log" to check if the log contains the expected messages |
| 112 | + const wrapperLog = (target, thisArg, args) => { |
| 113 | + const logContent = args[0]; |
| 114 | + if (logContent.includes(expectedMessage)) { |
| 115 | + testPassed = true; |
| 116 | + } |
| 117 | + return Reflect.apply(target, thisArg, args); |
| 118 | + }; |
| 119 | + const handlerLog = { |
| 120 | + apply: wrapperLog, |
| 121 | + }; |
| 122 | + window.console.log = new Proxy(window.console.log, handlerLog); |
| 123 | + |
| 124 | + try { |
| 125 | + window.location.href = redirectUrl; |
| 126 | + |
| 127 | + assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); |
| 128 | + assert.ok(testPassed, 'log should contain the expected message'); |
| 129 | + } catch (error) { |
| 130 | + assert.ok(false, `Navigation should be prevented, but an error was thrown: ${error}`); |
| 131 | + } |
| 132 | + }); |
| 133 | +} |
0 commit comments