-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acef717
commit 039c5b0
Showing
4 changed files
with
111 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* eslint-disable no-underscore-dangle, no-console */ | ||
import { runScriptlet, clearGlobalProps } from '../helpers'; | ||
import { startsWith } from '../../src/helpers/string-utils'; | ||
import { isEmptyObject } from '../../src/helpers/object-utils'; | ||
|
||
const { test, module } = QUnit; | ||
const name = 'trusted-replace-fetch-response'; | ||
|
||
const FETCH_OBJECTS_PATH = './test-files'; | ||
const nativeFetch = fetch; | ||
const nativeConsole = console.log; | ||
const nativeResponseJson = Response.prototype.json; | ||
|
||
const beforeEach = () => { | ||
window.__debug = () => { | ||
window.hit = 'FIRED'; | ||
}; | ||
}; | ||
|
||
const afterEach = () => { | ||
clearGlobalProps('hit', '__debug'); | ||
fetch = nativeFetch; // eslint-disable-line no-global-assign | ||
console.log = nativeConsole; | ||
Response.prototype.json = nativeResponseJson; | ||
}; | ||
|
||
module(name, { beforeEach, afterEach }); | ||
|
||
const isSupported = typeof fetch !== 'undefined' && typeof Proxy !== 'undefined' && typeof Response !== 'undefined'; | ||
|
||
if (!isSupported) { | ||
test('unsupported', (assert) => { | ||
assert.ok(true, 'Browser does not support it'); | ||
}); | ||
} else { | ||
// test('No arguments, logging', async (assert) => { | ||
// const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test01.json`; | ||
// const TEST_METHOD = 'POST'; | ||
// const init = { | ||
// method: TEST_METHOD, | ||
// }; | ||
// const expectedJson = { | ||
// a1: 1, | ||
// b2: 'test', | ||
// c3: 3, | ||
// }; | ||
// const done = assert.async(); | ||
|
||
// // mock console.log function for log checking | ||
// console.log = function log(input) { | ||
// if (input.indexOf('trace') > -1) { | ||
// return; | ||
// } | ||
// // eslint-disable-next-line max-len | ||
// const EXPECTED_LOG_STR_START = `fetch( url:"${INPUT_JSON_PATH}" method:"${TEST_METHOD}"`; | ||
// assert.ok(startsWith(input, EXPECTED_LOG_STR_START), 'console.hit input'); | ||
// }; | ||
|
||
// // no args -> just logging, no preventing | ||
// runScriptlet(name); | ||
|
||
// const response = await fetch(INPUT_JSON_PATH, init); | ||
// const actualJson = await response.json(); | ||
|
||
// assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); | ||
// assert.deepEqual(actualJson, expectedJson); | ||
// done(); | ||
// }); | ||
|
||
// test('Pattern(string) & replacement, match all requests', async (assert) => { | ||
// const INPUT_JSON_PATH_1 = `${FETCH_OBJECTS_PATH}/test01.json`; | ||
// const inputRequest1 = new Request(INPUT_JSON_PATH_1); | ||
|
||
// const INPUT_JSON_PATH_2 = `${FETCH_OBJECTS_PATH}/test02.json`; | ||
// const init2 = { | ||
// method: 'GET', | ||
// }; | ||
|
||
// const PATTERN = 'test'; | ||
// const REPLACEMENT = 'REPLACEMENT'; | ||
|
||
// runScriptlet(name, [PATTERN, REPLACEMENT, '*']); | ||
// const done1 = assert.async(); | ||
// // const done2 = assert.async(); | ||
|
||
// const response1 = await fetch(inputRequest1); | ||
// const parsedData1 = await response1.text(); | ||
|
||
// assert.notOk(parsedData1.includes(PATTERN), 'Pattern is removed'); | ||
// assert.ok(parsedData1.includes(REPLACEMENT), 'Replacement is set'); | ||
// assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); | ||
// done1(); | ||
// clearGlobalProps('hit'); | ||
// }); | ||
} |