Skip to content

Commit

Permalink
cy.json({...}) fails to add edges involving new nodes #2210
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkfranz committed Nov 2, 2018
1 parent b7a889b commit 5efe5d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/core/index.js
Expand Up @@ -297,6 +297,8 @@ util.extend( corefn, {
let idInJson = {};

let updateEles = function( jsons, gr ){
let toAdd = [];

for( let i = 0; i < jsons.length; i++ ){
let json = jsons[ i ];
let id = json.data.id;
Expand All @@ -308,12 +310,14 @@ util.extend( corefn, {
ele.json( json );
} else { // otherwise should be added
if( gr ){
cy.add( util.extend( { group: gr }, json ) );
toAdd.push( util.extend( { group: gr }, json ) );
} else {
cy.add( json );
toAdd.push( json );
}
}
}

cy.add( toAdd );
};

if( is.array( obj.elements ) ){ // elements: []
Expand Down
19 changes: 19 additions & 0 deletions test/core-graph-manipulation.js
Expand Up @@ -340,6 +340,25 @@ describe('Core graph manipulation', function(){
expect( cb ).to.equal(1);
});

it('cy.json() adds elements with preceding edge', function(){
var cb = 0;
cy.on('add', function(){ cb++; });

cy.json({
elements: [
{ data: { id: "ab", source: "a", target: "b" } },
{ data: { id: "a" } },
{ data: { id: "b" } }
]
});

expect( cy.$('#a').length ).to.equal(1);
expect( cy.$('#b').length ).to.equal(1);
expect( cy.$('#ab').length ).to.equal(1);

expect( cb ).to.equal(3);
});

it('cy.json() removes element', function(){
var cb = 0;
cy.on('remove', function(){ cb++; });
Expand Down

0 comments on commit 5efe5d1

Please sign in to comment.