From 01fc1b56c55a246a98f920b3414814f056fe6d42 Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Mon, 7 Jun 2021 13:53:51 +0200 Subject: [PATCH] chore(DqElement): fix up fromFrame --- lib/core/utils/dq-element.js | 17 ++++---- lib/core/utils/merge-results.js | 2 +- test/core/utils/dq-element.js | 77 ++++++++++++++++++++------------- 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/lib/core/utils/dq-element.js b/lib/core/utils/dq-element.js index b654a52fdd..85f3e0ce61 100644 --- a/lib/core/utils/dq-element.js +++ b/lib/core/utils/dq-element.js @@ -30,6 +30,7 @@ function getSource(element) { * @param {Object} spec Properties to use in place of the element when instantiated on Elements from other frames */ function DqElement(elm, options = {}, spec = {}) { + this.spec = spec; if (elm instanceof AbstractVirtualNode) { this._virtualNode = elm; this._element = elm.actualNode; @@ -38,8 +39,12 @@ function DqElement(elm, options = {}, spec = {}) { this._virtualNode = getNodeFromTree(elm); } - this._fromFrame = !!spec; - this.spec = spec; + /** + * Whether DqElement was created from an iframe + * @type {boolean} + */ + this.fromFrame = this.spec.selector?.length > 1; + if (options.absolutePaths) { this._options = { toRoot: true }; } @@ -49,8 +54,8 @@ function DqElement(elm, options = {}, spec = {}) { * @type {Number} */ this.nodeIndexes = []; - if (Array.isArray(spec.nodeIndexes)) { - this.nodeIndexes = spec.nodeIndexes + if (Array.isArray(this.spec.nodeIndexes)) { + this.nodeIndexes = this.spec.nodeIndexes } else if (this._virtualNode?.nodeIndex) { this.nodeIndexes = [this._virtualNode?.nodeIndex] } @@ -98,10 +103,6 @@ DqElement.prototype = { return this._element; }, - get fromFrame() { - return this._fromFrame; - }, - toJSON() { return { selector: this.selector, diff --git a/lib/core/utils/merge-results.js b/lib/core/utils/merge-results.js index 739ed8421d..754348fc8f 100644 --- a/lib/core/utils/merge-results.js +++ b/lib/core/utils/merge-results.js @@ -116,7 +116,7 @@ function mergeResults(frameResults, options) { const bNode = b.node.element; // only sort if the nodes are from different frames - if (aNode !== bNode && (a.node._fromFrame || b.node._fromFrame)) { + if (aNode !== bNode && (a.node.fromFrame || b.node.fromFrame)) { return nodeSorter(aNode, bNode); } diff --git a/test/core/utils/dq-element.js b/test/core/utils/dq-element.js index 99506a2625..d85bef8d66 100644 --- a/test/core/utils/dq-element.js +++ b/test/core/utils/dq-element.js @@ -198,35 +198,54 @@ describe('DqElement', function() { dqIframe = new DqElement(iframe, {}, iframeSpec); }); - it('returns a new DqElement', function() { - assert.instanceOf(DqElement.fromFrame(dqMain, {}, dqIframe), DqElement); - }); - - it('sets options for DqElement', function() { - var options = { absolutePaths: true }; - var dqElm = DqElement.fromFrame(dqMain, options, dqIframe); - assert.isTrue(dqElm._options.toRoot); - }); - - it('merges node and frame selectors', function() { - var dqElm = DqElement.fromFrame(dqMain, {}, dqIframe); - assert.deepEqual(dqElm.selector, [ - dqIframe.selector[0], - dqMain.selector[0] - ]); - assert.deepEqual(dqElm.ancestry, [ - dqIframe.ancestry[0], - dqMain.ancestry[0] - ]); - assert.deepEqual(dqElm.xpath, [dqIframe.xpath[0], dqMain.xpath[0]]); - }); - - it('merges nodeIndexes', function () { - var dqElm = DqElement.fromFrame(dqMain, {}, dqIframe); - assert.deepEqual(dqElm.nodeIndexes, [ - dqIframe.nodeIndexes[0], - dqMain.nodeIndexes[0] - ]); + describe('DqElement.fromFrame', function () { + it('returns a new DqElement', function() { + assert.instanceOf(DqElement.fromFrame(dqMain, {}, dqIframe), DqElement); + }); + + it('sets options for DqElement', function() { + var options = { absolutePaths: true }; + var dqElm = DqElement.fromFrame(dqMain, options, dqIframe); + assert.isTrue(dqElm._options.toRoot); + }); + + it('merges node and frame selectors', function() { + var dqElm = DqElement.fromFrame(dqMain, {}, dqIframe); + assert.deepEqual(dqElm.selector, [ + dqIframe.selector[0], + dqMain.selector[0] + ]); + assert.deepEqual(dqElm.ancestry, [ + dqIframe.ancestry[0], + dqMain.ancestry[0] + ]); + assert.deepEqual(dqElm.xpath, [dqIframe.xpath[0], dqMain.xpath[0]]); + }); + + it('merges nodeIndexes', function () { + var dqElm = DqElement.fromFrame(dqMain, {}, dqIframe); + assert.deepEqual(dqElm.nodeIndexes, [ + dqIframe.nodeIndexes[0], + dqMain.nodeIndexes[0] + ]); + }) + }); + + describe('DqElement.prototype.fromFrame', function () { + it('is false when created without a spec', function () { + assert.isFalse(dqMain.fromFrame); + }); + + it('is false when spec is not from a frame', function () { + var specMain = dqMain.toJSON(); + var dqElm = new DqElement(dqMain, {}, specMain); + assert.isFalse(dqElm.fromFrame); + }) + + it('is true when created with a spec', function () { + var dqElm = DqElement.fromFrame(dqMain, {}, dqIframe); + assert.isTrue(dqElm.fromFrame); + }) }) }); });