Skip to content

Commit

Permalink
fix: use closest parent/child pair in ancestor/descendant ralations (#69
Browse files Browse the repository at this point in the history
)
  • Loading branch information
LastLeaf committed Jul 24, 2023
1 parent bf9f7b3 commit c8c9361
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions glass-easel/src/relation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export class Relation {
break
}
}
if (newLink) break
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions glass-easel/tests/legacy/relation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,59 @@ describe('Component Relations', function () {
expect(c3.getRelationNodes('')).toStrictEqual([])
})

it('should link cascade ancestors and descendants', function () {
var ancestorBeh = regBeh({})
var descendantBeh = regBeh({})
regElem({
is: 'relation-ancestor-b',
behaviors: [ancestorBeh],
relations: {
'relation-b': {
target: descendantBeh,
type: 'descendant',
linked: function (target) {
expect(this).toBe(target.parentNode)
callOrder.push(this.id)
},
},
},
})
regElem({
is: 'relation-descendant-b',
behaviors: [descendantBeh],
relations: {
'relation-b': {
target: ancestorBeh,
type: 'ancestor',
linked: function (target) {
expect(target).toBe(this.parentNode)
callOrder.push(this.id)
},
},
},
})
regElem({
is: 'relation-ancestor-descendant',
template: `
<relation-ancestor-b id="p1">
<relation-descendant-b id="c1">
<relation-ancestor-b id="p2">
<relation-descendant-b id="c2" />
</relation-ancestor-b>
</relation-descendant-b>
</relation-ancestor-b>
`,
})
var elem = createElem('relation-ancestor-descendant')
var callOrder = []
glassEasel.Element.pretendAttached(elem)
expect(callOrder).toStrictEqual(['p1', 'c1', 'p2', 'c2'])
expect(elem.$.p1.getRelationNodes('relation-b')).toStrictEqual([elem.$.c1])
expect(elem.$.c1.getRelationNodes('relation-b')).toStrictEqual([elem.$.p1])
expect(elem.$.p2.getRelationNodes('relation-b')).toStrictEqual([elem.$.c2])
expect(elem.$.c2.getRelationNodes('relation-b')).toStrictEqual([elem.$.p2])
})

it('should trigger linkFailed handler when cannot link two nodes', function () {
regElem({
is: 'relation-cnt-failed',
Expand Down

0 comments on commit c8c9361

Please sign in to comment.