forked from krux/postscribe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverwrite.spec.js
83 lines (73 loc) · 2.36 KB
/
overwrite.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import postscribe from '../../src/postscribe';
describe('overwrite', () => {
function readNativeDocumentMethodString(method) {
// Cache because this takes a long time.
let cache = readNativeDocumentMethodString.cache;
if (!cache) {
cache = readNativeDocumentMethodString.cache = {};
}
let result = cache[method];
if (result) {
return result;
}
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
result = cache[method] = String(iframe.contentDocument[method]);
iframe.parentNode.removeChild(iframe);
return result;
}
function isNative(method) {
return String(document[method]) === readNativeDocumentMethodString(method);
}
// Must be async to avoid polluting doc.write for the next tests.
it('overrides document.write for normal scripts.', done => {
expect(isNative('write')).to.be.ok();
postscribe(document.body, '<script src="remote/describe-write.js"></script>', {
releaseAsync: true,
done
});
expect(isNative('write')).not.to.be.ok();
});
it('does not override document.write for async scripts.', done => {
expect(isNative('write')).to.be.ok();
postscribe(document.body, '<script async src="remote/describe-write.js"></script>', {
releaseAsync: true,
done
});
expect(isNative('write')).not.to.be.ok();
});
it('afterAsync fires when async ignored.', done => {
postscribe(document.body, '<script async src="remote/describe-write.js"></script>', {
releaseAsync: false,
afterAsync: () => {
expect(1);
done();
}
});
});
it('afterAsync fires when no async attr ignored.', done => {
postscribe(document.body, '<script src="remote/describe-write.js"></script>', {
releaseAsync: true,
afterAsync: () => {
expect(1);
done();
}
});
});
it('overrides and returns writeln, open, and close', done => {
function all(assertion = (val => expect(val).to.be.ok())) {
const methods = ['writeln', 'open', 'close'];
for (let i = 0, len = methods.length; i < len; ++i) {
assertion(isNative(methods[i]));
}
}
all();
postscribe(document.body, '<script src="remote/describe-write.js"></script>', {
afterAsync: () => {
all();
done();
}
});
all(v => expect(v).not.to.be.ok());
});
});