Skip to content

Commit 56bfad1

Browse files
test(Angular): refactor auto bootstrap tests
1 parent 424f4b8 commit 56bfad1

File tree

1 file changed

+61
-66
lines changed

1 file changed

+61
-66
lines changed

test/AngularSpec.js

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,83 +1697,78 @@ describe('angular', function() {
16971697
dealoc(appElement);
16981698
});
16991699

1700-
it('should bootstrap from an extension into an extension document for same-origin documents only', function() {
1701-
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1702-
if (msie) return;
1703-
1704-
// Extension URLs are browser-specific, so we must choose a scheme that is supported by the browser to make
1705-
// sure that the URL is properly parsed.
1706-
var extensionScheme;
1707-
var userAgent = window.navigator.userAgent;
1708-
if (/Firefox\//.test(userAgent)) {
1709-
extensionScheme = 'moz-extension';
1710-
} else if (/Edge\//.test(userAgent)) {
1711-
extensionScheme = 'ms-browser-extension';
1712-
} else if (/Chrome\//.test(userAgent)) {
1713-
extensionScheme = 'chrome-extension';
1714-
} else if (/Safari\//.test(userAgent)) {
1715-
extensionScheme = 'safari-extension';
1716-
} else {
1717-
extensionScheme = 'browserext'; // Upcoming standard scheme.
1718-
}
1700+
// IE does not support `document.currentScript` (nor extensions with protocol), so skip tests.
1701+
if (!msie) {
1702+
describe('auto bootstrap restrictions', function() {
17191703

1720-
var src = extensionScheme + '://something';
1721-
// Fake a minimal document object (the actual document.currentScript is readonly).
1722-
var fakeDoc = {
1723-
currentScript: { getAttribute: function() { return src; } },
1724-
location: {protocol: extensionScheme + ':', origin: extensionScheme + '://something'},
1725-
createElement: document.createElement.bind(document)
1726-
};
1727-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1704+
function createFakeDoc(attrs, protocol, currentScript) {
17281705

1729-
src = extensionScheme + '://something-else';
1730-
expect(allowAutoBootstrap(fakeDoc)).toBe(false);
1731-
});
1706+
protocol = protocol || 'http:';
1707+
var origin = protocol + '//something';
17321708

1733-
it('should bootstrap from a script with an empty or missing `src` attribute', function() {
1734-
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1735-
if (msie) return;
1709+
if (currentScript === undefined) {
1710+
currentScript = document.createElement('script');
1711+
Object.keys(attrs).forEach(function(key) { currentScript.setAttribute(key, attrs[key]); });
1712+
}
17361713

1737-
// Fake a minimal document object (the actual document.currentScript is readonly).
1738-
var src;
1739-
var fakeDoc = {
1740-
createElement: document.createElement.bind(document),
1741-
currentScript: {getAttribute: function() { return src; }},
1742-
location: {origin: 'some-value', protocol: 'http:'}
1743-
};
1714+
// Fake a minimal document object (the actual document.currentScript is readonly).
1715+
return {
1716+
currentScript: currentScript,
1717+
location: {protocol: protocol, origin: origin},
1718+
createElement: document.createElement.bind(document)
1719+
};
1720+
}
17441721

1745-
src = null;
1746-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1722+
it('should bootstrap from an extension into an extension document for same-origin documents only', function() {
1723+
1724+
// Extension URLs are browser-specific, so we must choose a scheme that is supported by the browser to make
1725+
// sure that the URL is properly parsed.
1726+
var protocol;
1727+
var userAgent = window.navigator.userAgent;
1728+
if (/Firefox\//.test(userAgent)) {
1729+
protocol = 'moz-extension:';
1730+
} else if (/Edge\//.test(userAgent)) {
1731+
protocol = 'ms-browser-extension:';
1732+
} else if (/Chrome\//.test(userAgent)) {
1733+
protocol = 'chrome-extension:';
1734+
} else if (/Safari\//.test(userAgent)) {
1735+
protocol = 'safari-extension:';
1736+
} else {
1737+
protocol = 'browserext:'; // Upcoming standard scheme.
1738+
}
17471739

1748-
src = '';
1749-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1750-
});
1740+
expect(allowAutoBootstrap(createFakeDoc({src: protocol + '//something'}, protocol))).toBe(true);
1741+
expect(allowAutoBootstrap(createFakeDoc({src: protocol + '//something-else'}, protocol))).toBe(false);
1742+
});
17511743

1752-
it('should not bootstrap from an extension into a non-extension document', function() {
1753-
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1754-
if (msie) return;
1744+
it('should bootstrap from a script with empty or no source (e.g. src, href or xlink:href attributes)', function() {
17551745

1756-
var src = 'resource://something';
1757-
// Fake a minimal document object (the actual document.currentScript is readonly).
1758-
var fakeDoc = {
1759-
currentScript: { getAttribute: function() { return src; } },
1760-
location: {protocol: 'http:'},
1761-
createElement: document.createElement.bind(document)
1762-
};
1763-
expect(allowAutoBootstrap(fakeDoc)).toBe(false);
1746+
expect(allowAutoBootstrap(createFakeDoc({src: null}))).toBe(true);
1747+
expect(allowAutoBootstrap(createFakeDoc({src: ''}))).toBe(true);
17641748

1765-
src = 'file://whatever';
1766-
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1767-
});
1749+
expect(allowAutoBootstrap(createFakeDoc({href: null}))).toBe(true);
1750+
expect(allowAutoBootstrap(createFakeDoc({href: ''}))).toBe(true);
17681751

1769-
it('should not bootstrap if bootstrapping is disabled', function() {
1770-
isAutoBootstrapAllowed = false;
1771-
angularInit(jqLite('<div ng-app></div>')[0], bootstrapSpy);
1772-
expect(bootstrapSpy).not.toHaveBeenCalled();
1773-
isAutoBootstrapAllowed = true;
1774-
});
1775-
});
1752+
expect(allowAutoBootstrap(createFakeDoc({'xlink:href': null}))).toBe(true);
1753+
expect(allowAutoBootstrap(createFakeDoc({'xlink:href': ''}))).toBe(true);
1754+
});
1755+
1756+
1757+
it('should not bootstrap from an extension into a non-extension document', function() {
17761758

1759+
expect(allowAutoBootstrap(createFakeDoc({src: 'resource://something'}))).toBe(false);
1760+
expect(allowAutoBootstrap(createFakeDoc({src: 'file://whatever'}))).toBe(true);
1761+
});
1762+
1763+
it('should not bootstrap if bootstrapping is disabled', function() {
1764+
isAutoBootstrapAllowed = false;
1765+
angularInit(jqLite('<div ng-app></div>')[0], bootstrapSpy);
1766+
expect(bootstrapSpy).not.toHaveBeenCalled();
1767+
isAutoBootstrapAllowed = true;
1768+
});
1769+
});
1770+
}
1771+
});
17771772

17781773
describe('angular service', function() {
17791774
it('should override services', function() {

0 commit comments

Comments
 (0)