From fcacdb440049a23bef2efb956d181e2370da146f Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Thu, 21 Dec 2017 11:18:06 -0500 Subject: [PATCH] Fix hover test (requires context.hasEntity stub) --- test/spec/behavior/hover.js | 105 +++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 48 deletions(-) diff --git a/test/spec/behavior/hover.js b/test/spec/behavior/hover.js index 8ad0cf3d8..4dbc5ac9a 100644 --- a/test/spec/behavior/hover.js +++ b/test/spec/behavior/hover.js @@ -1,108 +1,117 @@ describe('iD.behaviorHover', function() { - var container, context; + var _container; + var _context; + var _graph; beforeEach(function() { - container = d3.select('body').append('div'); - context = { + _container = d3.select('body').append('div'); + _context = { hover: function() {}, - mode: function() { return { id: 'browse' }; } + mode: function() { return { id: 'browse' }; }, + hasEntity: function(d) { return _graph && _graph.hasEntity(d); } }; }); afterEach(function() { - container.remove(); + _container.remove(); + _graph = null; }); describe('#off', function () { it('removes the .hover class from all elements', function () { - container.append('span').attr('class', 'hover'); - container.call(iD.behaviorHover(context).off); - expect(container.select('span').classed('hover')).to.be.false; + _container.append('span').attr('class', 'hover'); + _container.call(iD.behaviorHover(_context).off); + expect(_container.select('span').classed('hover')).to.be.false; }); it('removes the .hover-disabled class from the surface element', function () { - container.attr('class', 'hover-disabled'); - container.call(iD.behaviorHover(context).off); - expect(container.classed('hover-disabled')).to.be.false; + _container.attr('class', 'hover-disabled'); + _container.call(iD.behaviorHover(_context).off); + expect(_container.classed('hover-disabled')).to.be.false; }); }); describe('mouseover', function () { it('adds the .hover class to all elements to which the same datum is bound', function () { - var a = iD.Node({id: 'a'}), - b = iD.Node({id: 'b'}); + var a = iD.osmNode({id: 'a'}); + var b = iD.osmNode({id: 'b'}); + _graph = iD.coreGraph([a, b]); - container.selectAll('span') + _container.selectAll('span') .data([a, b, a, b]) .enter().append('span').attr('class', function(d) { return d.id; }); - container.call(iD.behaviorHover(context)); - iD.utilTriggerEvent(container.selectAll('.a'), 'mouseover'); + _container.call(iD.behaviorHover(_context)); + iD.utilTriggerEvent(_container.selectAll('.a'), 'mouseover'); - expect(container.selectAll('.a.hover').nodes()).to.have.length(2); - expect(container.selectAll('.b.hover').nodes()).to.have.length(0); + expect(_container.selectAll('.a.hover').nodes()).to.have.length(2); + expect(_container.selectAll('.b.hover').nodes()).to.have.length(0); }); it('adds the .hover class to all members of a relation', function() { - container.selectAll('span') - .data([iD.Relation({id: 'a', members: [{id: 'b'}]}), iD.Node({id: 'b'})]) + var a = iD.osmRelation({id: 'a', members: [{id: 'b'}]}); + var b = iD.osmNode({id: 'b'}); + _graph = iD.coreGraph([a, b]); + + _container.selectAll('span') + .data([a, b]) .enter().append('span').attr('class', function(d) { return d.id; }); - container.call(iD.behaviorHover(context)); - iD.utilTriggerEvent(container.selectAll('.a'), 'mouseover'); + _container.call(iD.behaviorHover(_context)); + iD.utilTriggerEvent(_container.selectAll('.a'), 'mouseover'); - expect(container.selectAll('.a.hover').nodes()).to.have.length(1); - expect(container.selectAll('.b.hover').nodes()).to.have.length(1); + expect(_container.selectAll('.a.hover').nodes()).to.have.length(1); + expect(_container.selectAll('.b.hover').nodes()).to.have.length(1); }); }); describe('mouseout', function () { it('removes the .hover class from all elements', function () { - container.append('span').attr('class', 'hover'); + _container.append('span').attr('class', 'hover'); - container.call(iD.behaviorHover(context)); - iD.utilTriggerEvent(container.selectAll('.hover'), 'mouseout'); + _container.call(iD.behaviorHover(_context)); + iD.utilTriggerEvent(_container.selectAll('.hover'), 'mouseout'); - expect(container.selectAll('.hover').nodes()).to.have.length(0); + expect(_container.selectAll('.hover').nodes()).to.have.length(0); }); }); describe('alt keydown', function () { it('replaces the .hover class with .hover-suppressed', function () { - container.append('span').attr('class', 'hover'); - container.call(iD.behaviorHover(context).altDisables(true)); + _container.append('span').attr('class', 'hover'); + _container.call(iD.behaviorHover(_context).altDisables(true)); - happen.keydown(window, {keyCode: 18}); - expect(container.selectAll('.hover').nodes()).to.have.length(0); - expect(container.selectAll('.hover-suppressed').nodes()).to.have.length(1); - happen.keyup(window, {keyCode: 18}); + happen.keydown(window, { keyCode: 18 }); + expect(_container.selectAll('.hover').nodes()).to.have.length(0); + expect(_container.selectAll('.hover-suppressed').nodes()).to.have.length(1); + happen.keyup(window, { keyCode: 18 }); }); it('adds the .hover-disabled class to the surface', function () { - container.call(iD.behaviorHover(context).altDisables(true)); + _container.call(iD.behaviorHover(_context).altDisables(true)); - happen.keydown(window, {keyCode: 18}); - expect(container.classed('hover-disabled')).to.be.true; - happen.keyup(window, {keyCode: 18}); + happen.keydown(window, { keyCode: 18 }); + expect(_container.classed('hover-disabled')).to.be.true; + happen.keyup(window, { keyCode: 18 }); }); }); describe('alt keyup', function () { it('replaces the .hover-suppressed class with .hover', function () { - container.append('span').attr('class', 'hover-suppressed'); - container.call(iD.behaviorHover(context).altDisables(true)); + _container.append('span').attr('class', 'hover-suppressed'); + _container.call(iD.behaviorHover(_context).altDisables(true)); - happen.keydown(window, {keyCode: 18}); - happen.keyup(window, {keyCode: 18}); - expect(container.selectAll('.hover').nodes()).to.have.length(1); - expect(container.selectAll('.hover-suppressed').nodes()).to.have.length(0); + happen.keydown(window, { keyCode: 18 }); + happen.keyup(window, { keyCode: 18 }); + expect(_container.selectAll('.hover').nodes()).to.have.length(1); + expect(_container.selectAll('.hover-suppressed').nodes()).to.have.length(0); }); it('removes the .hover-disabled class from the surface', function () { - container.call(iD.behaviorHover(context).altDisables(true)); + _container.call(iD.behaviorHover(_context).altDisables(true)); - happen.keydown(window, {keyCode: 18}); - happen.keyup(window, {keyCode: 18}); - expect(container.classed('hover-disabled')).to.be.false; + happen.keydown(window, { keyCode: 18 }); + happen.keyup(window, { keyCode: 18 }); + expect(_container.classed('hover-disabled')).to.be.false; }); }); });