Allow Graph to be constructed with an array of entities

Useful for tests.
This commit is contained in:
John Firebaugh
2012-12-05 15:27:28 -05:00
parent 7691693f1d
commit d26c8638b3
2 changed files with 23 additions and 17 deletions
+9 -1
View File
@@ -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) {
+14 -16
View File
@@ -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() {