diff --git a/src/d3-ext/util.js b/src/d3-ext/util.js new file mode 100644 index 0000000..4f0e010 --- /dev/null +++ b/src/d3-ext/util.js @@ -0,0 +1,23 @@ +/** + * @fileOverview + * Utility functions related to d3 selections. + */ +define( +function() { + 'use strict'; + + return { + + /** + * Same as d3.select but allows a d3.selection as input too. + * @param {d3.selection|string} selection + */ + select: function(selection) { + return (selection instanceof d3.selection) ? + selection : + d3.select(selection); + } + + }; + +}); diff --git a/test/unit/d3-ext/util.spec.js b/test/unit/d3-ext/util.spec.js new file mode 100644 index 0000000..b25da7d --- /dev/null +++ b/test/unit/d3-ext/util.spec.js @@ -0,0 +1,32 @@ +define([ + 'd3-ext/util' +], +function(selection) { + 'use strict'; + + describe('d3-ext.selection', function() { + + beforeEach(function() { + }); + + it('selects with a text selector', function() { + var fixture = jasmine.htmlFixture(), + sel = selection.select('#html-fixture'); + expect(sel.node()).toBe(fixture.node()); + }); + + it('selects with a d3 selection', function() { + var fixture = jasmine.htmlFixture(), + sel = selection.select(d3.select('#html-fixture')); + expect(sel.node()).toBe(fixture.node()); + }); + + it('selects with an DOM node', function() { + var fixture = jasmine.htmlFixture(), + sel = selection.select(fixture.node()); + expect(sel.node()).toBe(fixture.node()); + }); + + }); + +});