Skip to content

Commit

Permalink
make sure createElement is not changed
Browse files Browse the repository at this point in the history
  • Loading branch information
chinesedfan committed Jul 7, 2018
1 parent 56b2b54 commit 43be935
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/wrapper/jsonp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function init(wrapUrl, jsonpKey) {
setAttribute.call(node, label, value);
};

// don't afraid that `node.prototype` is changed
// because `desc` is just a copy
var desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(node), 'src');
var setter = desc.set;
desc.set = function(value) {
Expand Down
12 changes: 9 additions & 3 deletions test/wrapper/jsonp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('jsonp', function() {
runScripts: 'outside-only'
});
const win = dom.window;
const createElement = win.document.createElement;
win.oldCreateElement = win.document.createElement;
// prepare in sub-jsdom
if (window[coverageVar]) {
// hack
Expand All @@ -58,15 +58,21 @@ describe('jsonp', function() {
win.eval(`
(${jsonp.init.toString()})(wrapUrl);
window.script = document.createElement('script');
script.src = 'path';
script.src = 'path'; // do nothing
window.script2 = oldCreateElement.call(document, 'script');
script2.src = 'path2?callback=1'; // also do nothing
`);

expect(win.document.createElement).not.toBe(createElement);
expect(win.document.createElement).not.toBe(win.oldCreateElement);
expect(wrapUrl).toHaveBeenCalledTimes(0);

const script = win.script;
expect(script.src).toEqual('https://xx.com/path');
expect(script.getAttribute('src')).toEqual('path');
const script2 = win.script2;
expect(script2.src).toEqual('https://xx.com/path2?callback=1');
expect(script2.getAttribute('src')).toEqual('path2?callback=1');
});
});
it('should call wrapUrl in script.src.setter', function() {
Expand Down

0 comments on commit 43be935

Please sign in to comment.