From d26c8638b35a216e00348fa61f98b5fc9082af97 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 5 Dec 2012 15:27:28 -0500 Subject: [PATCH] Allow Graph to be constructed with an array of entities Useful for tests. --- js/id/graph/graph.js | 10 +++++++++- test/spec/graph/graph.js | 30 ++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index 8d4d2b666..a97356ff1 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -1,7 +1,15 @@ iD.Graph = function(entities, annotation) { if (!(this instanceof iD.Graph)) return new iD.Graph(entities, annotation); - this.entities = entities || {}; + if (_.isArray(entities)) { + this.entities = {}; + for (var i = 0; i < entities.length; i++) { + this.entities[entities[i].id] = entities[i]; + } + } else { + this.entities = entities || {}; + } + this.annotation = annotation; if (iD.debug) { diff --git a/test/spec/graph/graph.js b/test/spec/graph/graph.js index e41f36bf6..31ef038b9 100644 --- a/test/spec/graph/graph.js +++ b/test/spec/graph/graph.js @@ -1,21 +1,19 @@ -describe('Graph', function() { +describe('iD.Graph', function() { + it("can be constructed with an entities Object", function () { + var entity = iD.Entity(), + graph = iD.Graph({'n-1': entity}); + expect(graph.entity('n-1')).to.equal(entity); + }); - describe('Construction and access', function() { - it('entity', function() { - var entities = { 'n-1': { - type: 'node', - loc: [-80, 30], - id: 'n-1' - } - }; - var graph = iD.Graph(entities, 'first graph'); - expect(graph.entity('n-1')).to.equal(entities['n-1']); - }); + it("can be constructed with an entities Array", function () { + var entity = iD.Entity(), + graph = iD.Graph([entity]); + expect(graph.entity(entity.id)).to.equal(entity); + }); - it('annotation', function() { - var graph = iD.Graph({}, 'first graph'); - expect(graph.annotation).to.equal('first graph'); - }); + it('can be constructed with an annotation', function() { + var graph = iD.Graph({}, 'first graph'); + expect(graph.annotation).to.equal('first graph'); }); describe('operations', function() {