Skip to content

Commit

Permalink
chore(DqElement): fix up fromFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Jun 7, 2021
1 parent e2b6db2 commit 01fc1b5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 38 deletions.
17 changes: 9 additions & 8 deletions lib/core/utils/dq-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
}
Expand All @@ -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]
}
Expand Down Expand Up @@ -98,10 +103,6 @@ DqElement.prototype = {
return this._element;
},

get fromFrame() {
return this._fromFrame;
},

toJSON() {
return {
selector: this.selector,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/utils/merge-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
77 changes: 48 additions & 29 deletions test/core/utils/dq-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
})
});
});

0 comments on commit 01fc1b5

Please sign in to comment.