From 1e76291ada7040a0fdd9f96979e26f782b82bf2d Mon Sep 17 00:00:00 2001 From: Mark Goldsmith Date: Wed, 19 Feb 2020 08:51:03 -0500 Subject: [PATCH] [DEV-7060] Set document in constructor of ElementStyler --- src/ui/ElementStyler.js | 11 +++++++++-- test/ui/ElementStylerTest.js | 16 ++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/ui/ElementStyler.js b/src/ui/ElementStyler.js index 4ee28cc..3101a05 100644 --- a/src/ui/ElementStyler.js +++ b/src/ui/ElementStyler.js @@ -88,6 +88,12 @@ module.exports = Class( 'ElementStyler', */ 'private _jquery': null, + /** + * HTML Document + * @type {HTMLElement} + */ + 'private _document': null, + _answerStyles: { 'deductible': function( value, _, default_val ) @@ -193,6 +199,7 @@ module.exports = Class( 'ElementStyler', { this._$context = jquery; this._jquery = jquery; + this._document = jquery( 'body' ).context; }, @@ -855,10 +862,10 @@ module.exports = Class( 'ElementStyler', if ( hasindex ) { var id = this._getElementId( name, index ); - + if ( id ) { - element = document.getElementById( id ); + element = this._document.getElementById( id ); // let's hope for the best if ( element !== null ) diff --git a/test/ui/ElementStylerTest.js b/test/ui/ElementStylerTest.js index e7d16af..30812a8 100644 --- a/test/ui/ElementStylerTest.js +++ b/test/ui/ElementStylerTest.js @@ -115,10 +115,15 @@ describe( 'ui.ElementStyler', () => { it( "determines the correct element id for " + name, () => { - // Stub all objects - $ = sinon.stub(); - jQuery = sinon.stub(); - document = sinon.stub(); + // Stub document and jQuery calls + $ = sinon.stub(); + jQuery = sinon.stub(); + + const document = { + getElementById: sinon.stub() + }; + + jQuery.withArgs( 'body' ).returns( { context: document } ); const sut = Sut( jQuery ); @@ -136,12 +141,11 @@ describe( 'ui.ElementStyler', () => html: html, }; - document.getElementById = sinon.stub(); document.getElementById.returns( node ); sut.getWidgetByName( name, index, null, context ); - const actual_id = document.getElementById.getCall(0).args[0]; + const actual_id = document.getElementById.getCall( 0 ).args[ 0 ]; expect( actual_id ) .to.equal( expected_id );