UNPKG

@gitlab/ui

Version:
93 lines (74 loc) 2.4 kB
import { shallowMount } from '@vue/test-utils'; import { createMockChartInstance } from '~helpers/chart_stubs'; import { expectHeightAutoClasses } from '~helpers/chart_height'; import Chart from '../chart/chart.vue'; import ChartTooltip from '../tooltip/tooltip.vue'; import BarChart from './bar.vue'; let mockChartInstance; jest.mock('echarts', () => ({ getInstanceByDom: () => mockChartInstance, })); const defaultChartProps = { xAxisTitle: 'Pushes per day', xAxisType: 'value', yAxisTitle: 'User', data: { Office: [ [100, 'Scranton Strangler'], [300, 'Dwight'], ], }, }; describe('Bar chart component', () => { let wrapper; const findChart = () => wrapper.findComponent(Chart); const findChartTooltip = () => wrapper.findComponent(ChartTooltip); const getOptions = () => findChart().props('options'); const emitChartCreated = () => findChart().vm.$emit('created', mockChartInstance); const createComponent = (props = {}) => { wrapper = shallowMount(BarChart, { propsData: { ...defaultChartProps, ...props }, }); }; beforeEach(() => { mockChartInstance = createMockChartInstance(); }); describe('when mounted', () => { beforeEach(() => { createComponent(); }); it('should render chart with axis and series', () => { const chart = findChart(); expect(chart.props('options')).toMatchSnapshot(); }); it('should not emit created', () => { expect(wrapper.emitted('created')).toBeUndefined(); }); }); describe('when mounted and chart has created', () => { beforeEach(() => { createComponent(); emitChartCreated(); }); it('emits "created" when onCreated is called', () => { expect(wrapper.emitted('created')).toEqual([[mockChartInstance]]); }); it('long Y axis labels are ellipsized', () => { const { yAxis: { axisLabel: { formatter } } = {} } = getOptions(); defaultChartProps.data.Office.map(([, name]) => expect(formatter(name)).toMatchSnapshot()); }); it('configures chart tooltip', () => { expect(findChartTooltip().props()).toMatchObject({ chart: mockChartInstance, useDefaultTooltipFormatter: false, }); }); }); describe('height', () => { expectHeightAutoClasses({ createComponent, findContainer: () => wrapper, findChart, }); }); });