reactjs - Unit Test d3v4 zoom behavior in React with Chai -
using d3v4, react , chai (chai-enzyme, chai-jquery) unit testing.
so have zoom behavior attached graph.
const zoom = d3 .zoom() .scaleextent([1, infinity]) .on('zoom', () => { this.zoomed() })
and zoom behavior attached svg element.
const svg = d3 .select(this.node) .select('svg') .attr('preserveaspectratio', 'xminymin meet') .attr('viewbox', `0 0 ${svgwidth} ${svgheight}`) .classed('svg-content-responsive', true) .select('g') .attr('transform', `translate(${margin.left},${margin.top})`) .attr('height', height + margin.top + margin.bottom) .attr('width', width + margin.left + margin.right) .call(zoom)
the on('zoom') callback defined as
zoomed () { const {gxaxis, plotplantext, plotzones, width, xaxis, xscale} = this.graphobjects const transform = d3.event.transform // xmin , xmax in viewable world const [xmin, xmax] = xscale.domain().map(d => xscale(d)) // reverse transform xmin , xmax. if (transform.invertx(xmin) < 0) { transform.x = -xmin * transform.k } if (transform.invertx(xmax) > width) { transform.x = xmax - width * transform.k } // transform bars zones if (!isnan(transform.x) && !isnan(transform.y) && !isnan(transform.k)) { // rescale x linear scale can draw x axis const xnewscale = transform.rescalex(xscale) xaxis.scale(xnewscale) // rescale xaxis gxaxis.call(xaxis) plotzones .selectall('rect') .attr('x', (d, i) => transform.applyx(xscale(d.maxfrequency))) .attr('width', (d) => -(transform.applyx(xscale(d.maxfrequency)) - transform.applyx(xscale(d.minfrequency)))) // transform flow text plotplantext .selectall('.plan-text-src') .attr('x', d => (transform.applyx(xscale(d.maxfrequency)) + transform.applyx(xscale(d.minfrequency))) / 2) plotplantext .selectall('.plan-text-dest') .attr('x', d => (transform.applyx(xscale(d.maxfrequency)) + transform.applyx(xscale(d.minfrequency))) / 2) }}
my unit test firing "zoom" event is
describe('d3 event handling', function () { beforeeach(function () { $.fn.triggersvgevent = function (eventname, delta) { let event = new event(eventname, {'bubbles': true, 'cancelable': false}) if (delta) { event.deltax = delta.x event.deltay = delta.y } this[0].dispatchevent(event) return $(this) } }) describe('when chart zoomed', function () { let initialx beforeeach(function () { $('.flow-zone-container > svg > g').triggersvgevent('wheel') }) it('should update elements when zoomed', function () { ... }) }) })
when use chai-jquery trigger "wheel" event, zoom event fired d3.event.tranform in zoomed() method gives nan x, y , k. want test zoomed() callback such have d3.event.transform values test logic in zoomed() method.
how can fire zoomevent using "wheel" event or other event such d3.event fired values?
solved this. instead of using jquery fire event, using zoombehaviour.scaleby(selection, scale)
fire callback.
Comments
Post a Comment