I've got a Vue component and inside mounted
method I have this:
this.el = d3.select(this.$el);
this.svg = this.el.select('svg')
.attr('width', mainSvgPos.svgWidth)
.attr('height', mainSvgPos.svgHeight)
.attr('viewBox', "0 0 " + mainSvgPos.svgWidth + " " + mainSvgPos.svgHeight)
.attr('style',"position:absolute;left:0;top:20px;width:100%;height:100%")
this.chart = this.svg.select('g.chart').attr('transform', "translate(" + chartCenter.leftOffset + ", " + chartCenter.topOffset + ")")
I'm testing this component with jest
and vue-test-util
My test looks like this:
describe('gauge', () => {
const wrapper = shallow(gauge, {
propsData: ...some data,
})
it('renders correctly', () => {
expect(wrapper.vm.$el).toMatchSnapshot()
});
})
When it runs for the first time, as expected, it creates the shapshot. In this snapshot, I have the parent svg
element with all attributes set correctly (width, height, viewBox, style). However the g.chart
element does not contain any attributes (it should contain transform
). The mounted method creates a bunch of other elements using D3 syntax after that (I have not pasted them here)...none of that gets in the snapshot.
So my question is what happens in this.svg = this.el.select('svg')...
that prevents the snapshot from being created properly and how do I fix that.
I've tried nextTick
, jest.useFakeTimers()
, shallow
mounting, nothing gives me what I want.
Thanks