Skip to content

Commit

Permalink
feat: set $parent on create and set
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
philippfromme committed Feb 3, 2023
1 parent 2527a94 commit 7287a70
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/base.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isArray } from 'min-dash';

/**
* Moddle base element.
*/
Expand All @@ -8,5 +10,21 @@ Base.prototype.get = function(name) {
};

Base.prototype.set = function(name, value) {
const descriptor = this.$model.getPropertyDescriptor(this, name);

if (descriptor && !descriptor.isReference) {
if (isArray(value)) {
value.forEach(setParent(this));
} else if (value instanceof Base) {
setParent(this)(value);
}
}

this.$model.properties.set(this, name, value);
};
};

function setParent(parent) {
return function(value) {
value.$parent = parent;
};
}
129 changes: 129 additions & 0 deletions test/spec/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,134 @@ describe('properties', function() {
});


describe('parent', function() {

describe('create', function() {

it('should set parent if model element', function() {

// when
var complexNesting = model.create('props:ComplexNesting', {
nested: model.create('props:Complex')
});

// then
expect(complexNesting.get('nested').$parent).to.equal(complexNesting);
});


it('should not set parent if not model element', function() {

// when
var complexNesting = model.create('props:ComplexNesting', {
foo: {}
});

// then
expect(complexNesting.get('foo').$parent).not.to.exist;
});

});


describe('set', function() {

describe('one', function() {

it('should set parent if model element', function() {

// given
var complexNesting = model.create('props:ComplexNesting');

var nested = model.create('props:Complex');

// when
complexNesting.set('nested', nested);

// then
expect(complexNesting.get('nested')).to.equal(nested);
expect(nested.$parent).to.equal(complexNesting);
});


it('should not set parent if not model element', function() {

// given
var complexNesting = model.create('props:ComplexNesting');

// when
complexNesting.set('foo', {});

// then
expect(complexNesting.get('foo').$parent).not.to.exist;
});


it('should not set parent if model element is reference', function() {

// given
var referencingSingle = model.create('props:ReferencingSingle');

var referencedComplex = model.create('props:Complex');

// when
referencingSingle.set('referencedComplex', referencedComplex);

// then
expect(referencingSingle.get('referencedComplex')).to.equal(referencedComplex);
expect(referencedComplex.$parent).not.to.exist;
});

});


describe('many', function() {

it('should set parent if model elements', function() {

// given
var containedColllection = model.create('props:ContainedCollection');

var children = [
model.create('props:Complex'),
model.create('props:Complex')
];

// when
containedColllection.set('children', children);

// then
expect(containedColllection.get('children')).to.eql(children);

containedColllection.get('children').forEach(child => {
expect(child.$parent).to.equal(containedColllection);
});
});


it('should not set parent if not model element', function() {

// given
var containedCollection = model.create('props:ContainedCollection');

// when
containedCollection.set('foos', [
{},
{}
]);

// then
containedCollection.get('foos').forEach(foo => {
expect(foo.$parent).not.to.exist;
});
});

});

});

});

forEach([
false,
true,
Expand Down Expand Up @@ -315,6 +443,7 @@ describe('properties', function() {
});

});

});


Expand Down

0 comments on commit 7287a70

Please sign in to comment.