Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Commit

Permalink
Added tests for stylesheet importing methods as well
Browse files Browse the repository at this point in the history
  • Loading branch information
KockaAdmiralac committed Oct 19, 2018
1 parent e442386 commit 4f2bafc
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
74 changes: 73 additions & 1 deletion skins/common/spec/wikibits.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ describe('wikibits', function() {
var url3expect = '//dev.wikia.com/index.php?title=MediaWiki:UserTags/code.js&action=raw&ctype=text/javascript';
var url4 = 'https://dev.wikia.com/index.php?title=MediaWiki:UserTags/code.js&action=raw&ctype=text/javascript';
var url5 = 'http://platform.twitter.com/widgets.js';
var url6 = '/index.php?title=MediaWiki:WikiaNavigationBarStyle/code.css&action=raw&ctype=text/css';
var url6expect = '/index.php?title=MediaWiki:WikiaNavigationBarStyle.css&action=raw&ctype=text/css';
var url7 = 'http://dev.wikia.com/index.php?title=MediaWiki:WikiaNavigationBarStyle/code.css&action=raw&ctype=text/css';
var url7expect = '//dev.wikia.com/index.php?title=MediaWiki:WikiaNavigationBarStyle/code.css&action=raw&ctype=text/css';
var url7expect2 = '//dev.wikia.com/index.php?title=MediaWiki:WikiaNavigationBarStyle.css&action=raw&ctype=text/css';
var url8 = 'http://platform.twitter.com/widgets.css';
var tsScripts = '1539643173';
var tsReviewed = '1539734490';
var currentParam = '&current=' + tsScripts;
var reviewedParam = '&reviewed=' + tsReviewed;
var mediaAttr = 'sampleMedia';

it('should define needed global functions', function () {
expect(typeof window.addOnloadHook).toBe('function');
Expand Down Expand Up @@ -115,8 +122,8 @@ describe('wikibits', function() {
mw.config.set('wgReviewedScriptsTimestamp', tsReviewed);
mw.config.set('wgScript', '/index.php');
mw.config.set('wgScriptsTimestamp', tsScripts);
mw.config.set('wgWikiaBaseDomainRegex', '((wikia|fandom)\.com|(wikia|fandom)-dev\.(com|us|pl))');
mw.config.set('wgWikiaBaseDomain', 'wikia.com');
mw.config.set('wgWikiaBaseDomainRegex', '((wikia|fandom)\.com|(wikia|fandom)-dev\.(com|us|pl))');

// Ensure proper local URL is being imported
expect(window.importScriptPage('MediaWiki:UserTags/code.js').getAttribute('src'))
Expand All @@ -139,4 +146,69 @@ describe('wikibits', function() {
expect(window.importScriptPage('MediaWiki:UserTags/code.js', 'dev').getAttribute('src'))
.toEqual(url1expect2);
});

it('should import a stylesheet by URL properly', function() {
// Reset mw.config values
mw.config = new mw.Map();
mw.config.set('wgWikiaBaseDomainRegex', '((wikia|fandom)\.com|(wikia|fandom)-dev\.(com|us|pl))');

var stylesheet = window.importStylesheetURI(url6);

// Ensure proper DOM node attributes and insertion
expect(stylesheet instanceof Node).toBe(true);
expect(stylesheet.nodeName).toBe('LINK');
expect(stylesheet.getAttribute('type')).toEqual('text/css');
expect(stylesheet.getAttribute('rel')).toEqual('stylesheet');
expect(stylesheet.parentElement).toBe(document.head);

// Ensure HTTPS isn't enforced on protocol-relative URLs
expect(stylesheet.getAttribute('href')).toEqual(url6);

var stylesheet2 = window.importStylesheetURI(url7, mediaAttr);

// Ensure media attribute works
expect(stylesheet2.getAttribute('media')).toEqual(mediaAttr);
expect(stylesheet2.getAttribute('href')).toEqual(url7expect);

// Ensure nothing is done to external stylesheet URLs
expect(window.importStylesheetURI(url8).getAttribute('href'))
.toEqual(url8);
});

it('should import a stylesheet using importStylesheet properly', function() {
// Reset mw.config values
mw.config = new mw.Map();
mw.config.set('wgScript', '/index.php');
mw.config.set('wgWikiaBaseDomainRegex', '((wikia|fandom)\.com|(wikia|fandom)-dev\.(com|us|pl))');

// Ensure proper local URL is being imported
expect(window.importStylesheet('MediaWiki:WikiaNavigationBarStyle/code.css').getAttribute('href'))
.toEqual(url6);

// Ensure proper local URL is being imported on Dev Wiki
mw.config.set('wgCityId', '7931');
expect(window.importStylesheet('MediaWiki:WikiaNavigationBarStyle/code.css').getAttribute('href'))
.toEqual(url6expect);
});

it('should import a stylesheet using importStylesheetPage properly', function() {
// Reset mw.config values
mw.config = new mw.Map();
mw.config.set('wgScript', '/index.php');
mw.config.set('wgWikiaBaseDomain', 'wikia.com');
mw.config.set('wgWikiaBaseDomainRegex', '((wikia|fandom)\.com|(wikia|fandom)-dev\.(com|us|pl))');

// Ensure proper local URL is being imported
expect(window.importStylesheetPage('MediaWiki:WikiaNavigationBarStyle/code.css').getAttribute('href'))
.toEqual(url6);

// Ensure proper Dev Wiki URL is being imported
expect(window.importStylesheetPage('MediaWiki:WikiaNavigationBarStyle/code.css', 'dev').getAttribute('href'))
.toEqual(url7expect2);

// Ensure proper local URL is being imported on Dev Wiki
mw.config.set('wgCityId', '7931');
expect(window.importStylesheetPage('MediaWiki:WikiaNavigationBarStyle/code.css').getAttribute('href'))
.toEqual(url6expect);
});
});
25 changes: 14 additions & 11 deletions skins/common/wikibits.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ function maybeRedirectDevWikiCodeSubpage(url) {
url.indexOf('http://dev.' + mw.config.get('wgWikiaBaseDomain')) === 0 ||
url.indexOf('https://dev.' + mw.config.get('wgWikiaBaseDomain')) === 0
) &&
url.indexOf('/code.js') != -1
(
url.indexOf('/code.js') !== -1 ||
url.indexOf('/code.css') !== -1
)
) {
return url.replace(/\/code\.(js|css)/, '.$1')
}
Expand All @@ -111,17 +114,17 @@ window.importScript = function(page) {
};

window.loadedScripts = {}; // included-scripts tracker
window.importScriptURI = function( url ) {
window.importScriptURI = function(url) {
url = maybeMakeProtocolRelative(forceReviewedContent(url));

if ( loadedScripts[url] ) {
if (loadedScripts[url]) {
return null;
}
loadedScripts[url] = true;
var s = document.createElement( 'script' );
s.setAttribute( 'src', url );
s.setAttribute( 'type', 'text/javascript' );
document.getElementsByTagName('head')[0].appendChild( s );
var s = document.createElement('script');
s.setAttribute('src', url);
s.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);
return s;
};

Expand All @@ -133,15 +136,15 @@ window.importStylesheet = function(page) {
return importStylesheetURI(uri);
};

window.importStylesheetURI = function( url, media ) {
var l = document.createElement( 'link' );
window.importStylesheetURI = function(url, media) {
var l = document.createElement('link');
l.type = 'text/css';
l.rel = 'stylesheet';
l.href = maybeMakeProtocolRelative(url);
if( media ) {
if (media) {
l.media = media;
}
document.getElementsByTagName('head')[0].appendChild( l );
document.getElementsByTagName('head')[0].appendChild(l);
return l;
};

Expand Down

0 comments on commit 4f2bafc

Please sign in to comment.