Files
iD/test/spec/actions/join.js
2013-05-07 12:34:04 -07:00

202 lines
7.4 KiB
JavaScript

describe("iD.actions.Join", function () {
describe("#disabled", function () {
it("returns falsy for ways that share an end/start node", function () {
// a --> b ==> c
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b']}),
'=': iD.Way({id: '=', nodes: ['b', 'c']})
});
expect(iD.actions.Join(['-', '=']).disabled(graph)).not.to.be.ok;
});
it("returns falsy for ways that share a start/end node", function () {
// a <-- b <== c
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['b', 'a']}),
'=': iD.Way({id: '=', nodes: ['c', 'b']})
});
expect(iD.actions.Join(['-', '=']).disabled(graph)).not.to.be.ok;
});
it("returns falsy for ways that share a start/start node", function () {
// a <-- b ==> c
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['b', 'a']}),
'=': iD.Way({id: '=', nodes: ['b', 'c']})
});
expect(iD.actions.Join(['-', '=']).disabled(graph)).not.to.be.ok;
});
it("returns falsy for ways that share an end/end node", function () {
// a --> b <== c
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b']}),
'=': iD.Way({id: '=', nodes: ['c', 'b']})
});
expect(iD.actions.Join(['-', '=']).disabled(graph)).not.to.be.ok;
});
it("returns 'not_eligible' for non-line geometries", function () {
var graph = iD.Graph({
'a': iD.Node({id: 'a'})
});
expect(iD.actions.Join(['a']).disabled(graph)).to.equal('not_eligible');
});
it("returns 'not_adjacent' for ways that don't share the necessary nodes", function () {
// a -- b -- c
// |
// d
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'd': iD.Node({id: 'd'}),
'-': iD.Way({id: '-', nodes: ['a', 'b', 'c']}),
'=': iD.Way({id: '=', nodes: ['b', 'd']})
});
expect(iD.actions.Join(['-', '=']).disabled(graph)).to.equal('not_adjacent');
});
});
it("joins a --> b ==> c", function () {
// Expected result:
// a --> b --> c
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b']}),
'=': iD.Way({id: '=', nodes: ['b', 'c']})
});
graph = iD.actions.Join(['-', '='])(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'c']);
expect(graph.hasEntity('=')).to.be.undefined;
});
it("joins a <-- b <== c", function () {
// Expected result:
// a <-- b <-- c
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['b', 'a']}),
'=': iD.Way({id: '=', nodes: ['c', 'b']})
});
graph = iD.actions.Join(['-', '='])(graph);
expect(graph.entity('-').nodes).to.eql(['c', 'b', 'a']);
expect(graph.hasEntity('=')).to.be.undefined;
});
it("joins a <-- b ==> c", function () {
// Expected result:
// a <-- b <-- c
// tags on === reversed
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['b', 'a']}),
'=': iD.Way({id: '=', nodes: ['b', 'c'], tags: {'lanes:forward': 2}})
});
graph = iD.actions.Join(['-', '='])(graph);
expect(graph.entity('-').nodes).to.eql(['c', 'b', 'a']);
expect(graph.hasEntity('=')).to.be.undefined;
expect(graph.entity('-').tags).to.eql({'lanes:backward': 2});
});
it("joins a --> b <== c", function () {
// Expected result:
// a --> b --> c
// tags on === reversed
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b']}),
'=': iD.Way({id: '=', nodes: ['c', 'b'], tags: {'lanes:forward': 2}})
});
graph = iD.actions.Join(['-', '='])(graph);
expect(graph.entity('-').nodes).to.eql(['a', 'b', 'c']);
expect(graph.hasEntity('=')).to.be.undefined;
expect(graph.entity('-').tags).to.eql({'lanes:backward': 2});
});
it("prefers to keep existing ways", function () {
// a --> b ==> c
// --- is new, === is existing
// Expected result:
// a ==> b ==> c
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'w-1': iD.Way({id: 'w-1', nodes: ['a', 'b']}),
'w1': iD.Way({id: 'w1', nodes: ['b', 'c']})
});
graph = iD.actions.Join(['w-1', 'w1'])(graph);
expect(graph.entity('w1').nodes).to.eql(['a', 'b', 'c']);
expect(graph.hasEntity('w-1')).to.be.undefined;
});
it("merges tags", function () {
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b'], tags: {a: 'a', b: '-', c: 'c'}}),
'=': iD.Way({id: '=', nodes: ['b', 'c'], tags: {a: 'a', b: '=', d: 'd'}})
});
graph = iD.actions.Join(['-', '='])(graph);
expect(graph.entity('-').tags).to.eql({a: 'a', b: '-;=', c: 'c', d: 'd'});
});
it("merges relations", function () {
var graph = iD.Graph({
'a': iD.Node({id: 'a'}),
'b': iD.Node({id: 'b'}),
'c': iD.Node({id: 'c'}),
'-': iD.Way({id: '-', nodes: ['a', 'b']}),
'=': iD.Way({id: '=', nodes: ['b', 'c']}),
'r1': iD.Relation({id: 'r1', members: [{id: '=', role: 'r1', type: 'way'}]}),
'r2': iD.Relation({id: 'r2', members: [{id: '=', role: 'r2', type: 'way'}, {id: '-', role: 'r2', type: 'way'}]})
});
graph = iD.actions.Join(['-', '='])(graph);
expect(graph.entity('r1').members).to.eql([{id: '-', role: 'r1', type: 'way'}]);
expect(graph.entity('r2').members).to.eql([{id: '-', role: 'r2', type: 'way'}]);
});
});