Test; simplify

This commit is contained in:
John Firebaugh
2015-02-12 10:21:09 -08:00
parent c9162ea65e
commit 9aab74c187
3 changed files with 37 additions and 21 deletions

View File

@@ -32,18 +32,16 @@ iD.actions.DeleteWay = function(wayId) {
};
action.disabled = function(graph) {
var way = graph.entity(wayId);
var reltypes = ['route','boundary','multipolygon'];
var required_roles = { 'multipolygon': 'outer' };
var disabled = false;
graph.parentRelations(way)
.forEach(function(parent) {
if (reltypes.indexOf(parent.tags.type)>-1) {
if (!required_roles[parent.tags.type] || parent.containsEntityInRole(way,required_roles[parent.tags.type])) {
disabled = 'part_of_relation';
}
}
});
graph.parentRelations(graph.entity(wayId)).forEach(function(parent) {
var type = parent.tags.type,
role = parent.memberById(wayId).role || 'outer';
if (type === 'route' || type === 'boundary' || (type === 'multipolygon' && role === 'outer')) {
disabled = 'part_of_relation';
}
});
return disabled;
};

View File

@@ -115,16 +115,6 @@ _.extend(iD.Relation.prototype, {
}
},
// Test whether the relation contains an entity in the given role.
containsEntityInRole: function(entity, role) {
for (var i = 0; i < this.members.length; i++) {
if (this.members[i].id === entity.id && this.members[i].type === entity.type && this.members[i].role === role) {
return true;
}
}
return false;
},
addMember: function(member, index) {
var members = this.members.slice();
members.splice(index === undefined ? members.length : index, 0, member);

View File

@@ -68,4 +68,32 @@ describe("iD.actions.DeleteWay", function() {
graph = iD.Graph([way, relation]).update(action);
expect(graph.hasEntity(relation.id)).to.be.undefined;
});
describe("#disabled", function () {
it("returns 'part_of_relation' for members of route and boundary relations", function () {
var a = iD.Way({id: 'a'}),
b = iD.Way({id: 'b'}),
route = iD.Relation({members: [{id: 'a'}], tags: {type: 'route'}}),
boundary = iD.Relation({members: [{id: 'b'}], tags: {type: 'boundary'}}),
graph = iD.Graph([a, b, route, boundary]);
expect(iD.actions.DeleteWay('a').disabled(graph)).to.equal('part_of_relation');
expect(iD.actions.DeleteWay('b').disabled(graph)).to.equal('part_of_relation');
});
it("returns 'part_of_relation' for outer members of multipolygons", function () {
var way = iD.Way({id: 'w'}),
relation = iD.Relation({members: [{id: 'w', role: 'outer'}], tags: {type: 'multipolygon'}}),
graph = iD.Graph([way, relation]),
action = iD.actions.DeleteWay(way.id);
expect(action.disabled(graph)).to.equal('part_of_relation');
});
it("returns falsy for inner members of multipolygons", function () {
var way = iD.Way({id: 'w'}),
relation = iD.Relation({members: [{id: 'w', role: 'inner'}], tags: {type: 'multipolygon'}}),
graph = iD.Graph([way, relation]),
action = iD.actions.DeleteWay(way.id);
expect(action.disabled(graph)).not.ok;
});
});
});