Aarcos has uploaded a new change for review. https://gerrit.wikimedia.org/r/98009
Change subject: Add a tests to MultimediaViewer. Based on https://gerrit.wikimedia.org/r/#/c/96152/1 These are just smoke tests. I will add more in coming versions of this change. ...................................................................... Add a tests to MultimediaViewer. Based on https://gerrit.wikimedia.org/r/#/c/96152/1 These are just smoke tests. I will add more in coming versions of this change. Change-Id: I366c7af9a5cf43361d8293183c9da117bc5d4971 --- M MultimediaViewer.php M MultimediaViewerHooks.php M resources/ext.multimediaViewer/ext.multimediaViewer.js A tests/qunit/ext.multimediaViewer.test.js 4 files changed, 106 insertions(+), 18 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MultimediaViewer refs/changes/09/98009/1 diff --git a/MultimediaViewer.php b/MultimediaViewer.php index ac743ee..ea9f2df 100644 --- a/MultimediaViewer.php +++ b/MultimediaViewer.php @@ -246,6 +246,7 @@ $wgHooks['BeforePageDisplay'][] = 'MultimediaViewerHooks::getModulesForArticle'; $wgHooks['CategoryPageView'][] = 'MultimediaViewerHooks::getModulesForCategory'; $wgHooks['ResourceLoaderGetConfigVars'][] = 'MultimediaViewerHooks::resourceLoaderGetConfigVars'; +$wgHooks['ResourceLoaderTestModules'][] = 'MultimediaViewerHooks::getTestModules'; $wgExtensionCredits['other'][] = array( 'path' => __FILE__, diff --git a/MultimediaViewerHooks.php b/MultimediaViewerHooks.php index ad862f9..baea8ad 100644 --- a/MultimediaViewerHooks.php +++ b/MultimediaViewerHooks.php @@ -107,4 +107,25 @@ ); return true; } + + /** + * Get modules for testing our JavaScript + * @param array $testModules + * @param ResourceLoader resourceLoader + * @return bool + */ + public static function getTestModules( array &$testModules, ResourceLoader &$resourceLoader ) { + $testModules['qunit']['ext.multimediaViewer.tests'] = array( + 'scripts' => array( + 'tests/qunit/ext.multimediaViewer.test.js', + ), + 'dependencies' => array( + 'ext.multimediaViewer', + ), + 'localBasePath' => __DIR__, + 'remoteExtPath' => 'MultimediaViewer', + ); + + return true; + } } diff --git a/resources/ext.multimediaViewer/ext.multimediaViewer.js b/resources/ext.multimediaViewer/ext.multimediaViewer.js index 9b306e1..15c195c 100755 --- a/resources/ext.multimediaViewer/ext.multimediaViewer.js +++ b/resources/ext.multimediaViewer/ext.multimediaViewer.js @@ -96,23 +96,7 @@ urls.push( thisImage ); $links.click( function ( e ) { - // Do not interfere with non-left clicks or if modifier keys are pressed. - if ( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) { - return; - } - - var $this = $( this ), - initial = $thumbContain.find( 'img' ).prop( 'src' ); - - if ( $this.is( 'a.image' ) ) { - viewer.log( 'thumbnail-link-click' ); - } else if ( $this.is( '.magnify a' ) ) { - viewer.log( 'enlarge-link-click' ); - } - - e.preventDefault(); - - viewer.loadImage( thisImage, initial ); + viewer.clickLinkCallback( e, this, $thumbContain, thisImage ); return false; } ); @@ -179,6 +163,36 @@ } MMVP = MultimediaViewer.prototype; + + /** + * Handles clicks on legit image links. + * + * @protected + * + * @param {jQuery.Event} e click event + * @param {HTMLElement|jQuery} clickedEle clicked element + * @param {jQuery} $thumbContain thumbnail container element + * @param {mw.LightboxImage} thisImage lightboximage object + */ + MMVP.clickLinkCallback = function ( e, clickedEle, $thumbContain, thisImage ) { + // Do not interfere with non-left clicks or if modifier keys are pressed. + if ( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) { + return; + } + + var $clickedEle = $( clickedEle ), + initial = $thumbContain.find( 'img' ).prop( 'src' ); + + if ( $clickedEle.is( 'a.image' ) ) { + this.log( 'thumbnail-link-click' ); + } else if ( $clickedEle.is( '.magnify a' ) ) { + this.log( 'enlarge-link-click' ); + } + + e.preventDefault(); + + this.loadImage( thisImage, initial ); + } MMVP.resize = function ( ui ) { var api = new mw.Api(), @@ -722,7 +736,9 @@ mw.mediaViewer.loadImage( statedIndex, $( imgsSelector ).eq( linkState[2] ).prop( 'src' ) ); } } else { - mw.mediaViewer.lightbox.iface.unattach(); + if (mw.mediaViewer.lightbox) { + mw.mediaViewer.lightbox.iface.unattach(); + } } } diff --git a/tests/qunit/ext.multimediaViewer.test.js b/tests/qunit/ext.multimediaViewer.test.js new file mode 100644 index 0000000..46129a7 --- /dev/null +++ b/tests/qunit/ext.multimediaViewer.test.js @@ -0,0 +1,50 @@ +( function ( mw, $ ) { + // TODO: Create test cases for each one of these entries. + clickCases = { + valid: [ + $( '<img>' ).appendTo( $( '<a>' ).addClass( 'image' ).appendTo( $( '<div>' ).addClass( 'gallery' ).appendTo( '#qunit-fixture' ) ) ), + $( '<img>' ).appendTo( $( '<a>' ).addClass( 'image' ).appendTo( '#qunit-fixture' ) ) + ], + + invalid: [ + $( '<img>' ).appendTo( $( '<p>' ).appendTo( '#qunit-fixture' ) ) + ] + }; + + QUnit.module( 'ext.multimediaViewer', QUnit.newMwEnvironment() ); + + QUnit.test( 'Check viewer not invoked if no image links', 0, function ( assert ) { + // Valid fragment to launch viewer + var div = $( $( '<div class="gallery">' ) ).appendTo( '#qunit-fixture' ); + var link = $( $( '<a>' ) ).appendTo( div ); + + // Create viewer so link analysis happens now that we have some images. + var viewer = new mw.MultimediaViewer(); + + // Mock clickLink callback + viewer.clickLinkCallback = function () { + ok( false, "A click was caught when it shouldn't. No image on the page." ); + }; + + // Click on the link + link.trigger('click'); + } ); + + QUnit.test( 'Check viewer invoked when clicking on an image link', 1, function ( assert ) { + // Valid fragment to launch viewer + var div = $( $( '<div class="gallery">' ) ).appendTo( '#qunit-fixture' ); + var link = $( $( '<a class="image">' ) ).appendTo( div ); + var image = $( '<img src="thumb.jpg">' ).appendTo( link ); + + // Create viewer so link analysis happens now that we have some images. + var viewer = new mw.MultimediaViewer(); + + // Mock clickLink callback + viewer.clickLinkCallback = function () { + ok( true, "click callback called." ); + }; + + // Click on the link + link.trigger('click'); + } ); +}( mediaWiki, jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/98009 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I366c7af9a5cf43361d8293183c9da117bc5d4971 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MultimediaViewer Gerrit-Branch: master Gerrit-Owner: Aarcos <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
