From 6e97f9b29c5f2b2ca93e68b2741502a6e2d735e2 Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Tue, 7 Nov 2023 10:15:34 -0500 Subject: [PATCH] fixup test by guarding element access in dollarsign.js --- .../js/disclosures/index.js | 1 - .../js/disclosures/utils/dollar-sign.js | 28 +++++-------------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js index 6d66de20523..1549340d93d 100755 --- a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js +++ b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/index.js @@ -38,7 +38,6 @@ const ready = function (callback) { const app = { urlValues: {}, init: function () { - // jquery promise to delay full model creation until ajax resolves getApiValues.initialData().then((resp) => { const [constants, expenses] = resp; financialModel.init(constants[0]); diff --git a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/dollar-sign.js b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/dollar-sign.js index 0320945f684..92265298d3f 100644 --- a/cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/dollar-sign.js +++ b/cfgov/unprocessed/apps/paying-for-college/js/disclosures/utils/dollar-sign.js @@ -17,17 +17,11 @@ function Query(selector) { if (typeof selector === 'string' && selector !== '') { this.elements = document.querySelectorAll(selector); } - - // if ( this.elements.length > 1 ) { - // return this.elements; - // } else { - // return this.elements[0]; - // } } Query.prototype.attr = function (name, value) { if (typeof value === 'undefined') { - return this.elements[0].getAttribute(name); + return this.elements.length ? this.elements[0].getAttribute(name) : null; } else { this.elements.forEach((elem) => { elem.setAttribute(name, value); @@ -36,13 +30,13 @@ Query.prototype.attr = function (name, value) { }; Query.prototype.cloner = function () { - return this.elements[0].cloneNode(true); + return this.elements.length ? this.elements[0].cloneNode(true) : null; }; Query.prototype.text = function (value) { // getter if (typeof value === 'undefined') { - return this.elements[0].textContent; + return this.elements.length ? this.elements[0].textContent : null; } //setter else { @@ -55,7 +49,7 @@ Query.prototype.text = function (value) { Query.prototype.val = function (value) { // getter if (typeof value === 'undefined' && this.elements.length > 0) { - return this.elements[0].value; + return this.elements.length ? this.elements[0].value : null; } //setter else { @@ -146,19 +140,11 @@ Query.prototype.show = function (className) { }; Query.prototype.height = function () { - if (this.elements.length > 0) { - return this.elements[0].offsetHeight; - } else { - return null; - } + return this.elements.length ? this.elements[0].offsetHeight : null; }; Query.prototype.top = function () { - if (this.elements.length > 0) { - return this.elements[0].offsetTop; - } else { - return null; - } + return this.elements.length ? this.elements[0].offsetTop : null; }; Query.prototype.listen = function (eventType, callback) { @@ -168,7 +154,7 @@ Query.prototype.listen = function (eventType, callback) { }; Query.prototype.tagName = function () { - return this.elements[0].tagName; + return this.elements.length ? this.elements[0].tagName : null; }; Query.prototype.addClass = function (classNames) {