Paste P7871

TestingCtaDrawer
ActivePublic

Authored by Jdlrobson on Thu, Nov 29, 11:30 PM.
diff --git a/tests/node-qunit/mobile.startup/CtaDrawer.test.js b/tests/node-qunit/mobile.startup/CtaDrawer.test.js
new file mode 100644
index 000000000..caec52177
--- /dev/null
+++ b/tests/node-qunit/mobile.startup/CtaDrawer.test.js
@@ -0,0 +1,74 @@
+var
+ // Imports
+ CtaDrawer,
+ dom = require( '../utils/dom' ),
+ Drawer,
+ jQuery = require( '../utils/jQuery' ),
+ mw = require( '../utils/mw' ),
+ oo = require( '../utils/oo' ),
+ sandbox,
+ sinon = require( 'sinon' ),
+
+ // Variables
+ parent;
+
+function stripWhitespaceHTML( str ) {
+ return str.replace( /\s+/g, ' ' )
+ // strip spaces before opening tags
+ .replace( / </g, '<' );
+}
+QUnit.module( 'MobileFrontend CtaDrawer.js', {
+ beforeEach: function () {
+ var parentID = 'ctaDrawerParent';
+
+ sandbox = sinon.sandbox.create();
+
+ // Set up required by all Views.
+ dom.setUp( sandbox, global );
+ jQuery.setUp( sandbox, global );
+ oo.setUp( sandbox, global );
+
+ // Additional CtaDrawer global dependency.
+ mw.setUp( sandbox, global );
+
+ // Dynamically import Drawer and CtaDrawer to use fresh sandboxed dependencies.
+ Drawer = require( '../../../src/mobile.startup/Drawer' );
+ CtaDrawer = require( '../../../src/mobile.startup/CtaDrawer' );
+
+ sandbox.stub( global.mw, 'msg' )
+ .withArgs( 'mobile-frontend-watchlist-cta-button-login' )
+ .returns( 'login' )
+ .withArgs( 'mobile-frontend-watchlist-cta-button-signup' )
+ .returns( 'sign up' );
+ // Rewire the prototype, not the instance, since this property is used during construction.
+ sandbox.stub( Drawer.prototype, 'appendToElement', '#' + parentID );
+
+ // Create a disposable host Element. See T209129.
+ parent = document.createElement( 'div' );
+ parent.id = parentID;
+ document.documentElement.appendChild( parent );
+ },
+
+ afterEach: function () {
+ // Discard host Element.
+ document.documentElement.removeChild( parent );
+ parent = undefined;
+
+ CtaDrawer = undefined;
+ Drawer = undefined;
+
+ jQuery.tearDown();
+
+ sandbox.restore();
+ }
+} );
+
+QUnit.test( 'HTML is valid', function ( assert ) {
+ var subject = new CtaDrawer();
+
+ sinon.assert.match(
+ stripWhitespaceHTML( subject.$el.get( 0 ).outerHTML ),
+ '<div class="drawer position-fixed view-border-box"><div class="mw-ui-icon mw-ui-icon-mf-arrow mw-ui-icon-element cancel" title=""></div><p></p><a href="Special:UserLogin" class="mw-ui-button mw-ui-progressive ">mobile-frontend-watchlist-cta-button-login</a><div><a href="Special:UserLogin" class="mw-ui-anchor mw-ui-progressive ">mobile-frontend-watchlist-cta-button-signup</a></div></div>'
+ );
+ assert.ok( true );
+} );