Skip to content

Commit

Permalink
Fix PropertyBase.parent
Browse files Browse the repository at this point in the history
  • Loading branch information
codenirvana committed Dec 13, 2024
1 parent 2219c42 commit 8717977
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
unreleased:
fixed bugs:
- >-
Fixed a bug where `PropertyBase.parent` used to return grandparent for
non-list parent
4.5.0:
date: 2024-08-01
new features:
Expand Down
10 changes: 8 additions & 2 deletions lib/collection/property-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,14 @@ _.assign(PropertyBase.prototype, /** @lends PropertyBase.prototype */ {
* @returns {*|undefined}
*/
parent () {
// @todo return grandparent only if it is a list
return this && this.__parent && (this.__parent.__parent || this.__parent) || undefined;
let parent = this.__parent;

// if the parent is a list, return the grandparent
if (parent && parent._postman_propertyIsList) {
parent = parent.__parent || parent;
}

return parent || undefined;
},

/**
Expand Down
44 changes: 36 additions & 8 deletions test/unit/property-base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,59 @@ describe('PropertyBase', function () {
});

describe('.parent()', function () {
it('should return grandparent by default', function () {
it('should return parent when set', function () {
var base = new sdk.PropertyBase();

base.__parent = {
p: true
};

expect(base.parent()).to.have.property('p', true);
});

it('should return undefined when parent is not set', function () {
var base = new sdk.PropertyBase();

expect(base.parent()).to.be.undefined;
});

it('should return undefined when parent is falsy', function () {
var base = new sdk.PropertyBase();

base.__parent = null;
expect(base.parent()).to.be.undefined;

base.__parent = false;
expect(base.parent()).to.be.undefined;

base.__parent = '';
expect(base.parent()).to.be.undefined;
});

it('should return grandparent when parent is of type list', function () {
var base = new sdk.PropertyBase();

base.__parent = {
_postman_propertyIsList: true,
p: true
};

base.__parent.__parent = {
g: true
};

expect(base.parent()).to.eql({
g: true
});
expect(base.parent()).to.have.property('g', true);
});

it('should return parent if grandparent is missing', function () {
it('should return parent if grandparent is missing for list type parent', function () {
var base = new sdk.PropertyBase();

base.__parent = {
_postman_propertyIsList: true,
p: true
};
expect(base.parent()).to.eql({
p: true
});

expect(base.parent()).to.have.property('p', true);
});
});
});

0 comments on commit 8717977

Please sign in to comment.