Commit 423bbfb1 authored by Martin Wortschack's avatar Martin Wortschack

Migrate issues analytics component spec to jest

parent b7fa9dc4
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import Vuex from 'vuex';
import { TEST_HOST } from 'helpers/test_constants';
import IssuesAnalytics from 'ee/issues_analytics/components/issues_analytics.vue'; import IssuesAnalytics from 'ee/issues_analytics/components/issues_analytics.vue';
import EmptyState from 'ee/issues_analytics/components/empty_state.vue'; import EmptyState from 'ee/issues_analytics/components/empty_state.vue';
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { createLocalVue, shallowMount } from '@vue/test-utils';
import { GlLoadingIcon } from '@gitlab/ui';
import { createStore } from 'ee/issues_analytics/stores'; import { createStore } from 'ee/issues_analytics/stores';
const localVue = createLocalVue(); const localVue = createLocalVue();
localVue.use(Vuex);
describe('Issues Analytics component', () => { describe('Issues Analytics component', () => {
let vm; let wrapper;
let store; let store;
let mountComponent; let mountComponent;
let axiosMock;
const mockChartData = { '2017-11': 0, '2017-12': 2 }; const mockChartData = { '2017-11': 0, '2017-12': 2 };
beforeEach(() => { beforeEach(() => {
axiosMock = new MockAdapter(axios);
store = createStore(); store = createStore();
spyOn(store, 'dispatch').and.stub(); jest.spyOn(store, 'dispatch').mockImplementation();
mountComponent = data => { mountComponent = data => {
setFixtures('<div id="mock-filter"></div>'); setFixtures('<div id="mock-filter"></div>');
const props = data || { const propsData = data || {
endpoint: gl.TEST_HOST, endpoint: TEST_HOST,
filterBlockEl: document.querySelector('#mock-filter'), filterBlockEl: document.querySelector('#mock-filter'),
}; };
return shallowMount(localVue.extend(IssuesAnalytics), { return shallowMount(IssuesAnalytics, {
propsData: props, localVue,
propsData,
stubs: { stubs: {
GlColumnChart: true, GlColumnChart: true,
EmptyState,
}, },
store, store,
localVue, });
}).vm;
}; };
vm = mountComponent(); wrapper = mountComponent();
}); });
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
axiosMock.restore();
}); });
const findLoadingIcon = () => wrapper.find(GlLoadingIcon);
const findChartContainer = () => wrapper.find('.issues-analytics-chart');
const findEmptyState = () => wrapper.find(EmptyState);
it('fetches chart data when mounted', () => { it('fetches chart data when mounted', () => {
expect(store.dispatch).toHaveBeenCalledWith('issueAnalytics/fetchChartData', gl.TEST_HOST); expect(store.dispatch).toHaveBeenCalledWith('issueAnalytics/fetchChartData', TEST_HOST);
}); });
it('renders loading state when loading', done => { it('renders loading state when loading', () => {
vm.$store.state.issueAnalytics.loading = true; wrapper.vm.$store.state.issueAnalytics.loading = true;
vm.$nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(vm.$el.querySelector('.issues-analytics-loading')).not.toBe(null); expect(findLoadingIcon().exists()).toBe(true);
expect(vm.$el.querySelector('.issues-analytics-chart')).toBe(null); expect(findChartContainer().exists()).toBe(false);
done();
}); });
}); });
it('renders chart when data is present', done => { it('renders chart when data is present', () => {
vm.$store.state.issueAnalytics.chartData = mockChartData; wrapper.vm.$store.state.issueAnalytics.chartData = mockChartData;
vm.$nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(vm.$el.querySelector('.issues-analytics-chart')).not.toBe(null); expect(findChartContainer().exists()).toBe(true);
done();
}); });
}); });
it('fetches data when filters are applied', done => { it('fetches data when filters are applied', () => {
vm.$store.state.issueAnalytics.filters = '?hello=world'; wrapper.vm.$store.state.issueAnalytics.filters = '?hello=world';
vm.$nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(store.dispatch).toHaveBeenCalledTimes(2); expect(store.dispatch).toHaveBeenCalledTimes(2);
expect(store.dispatch.calls.argsFor(1)).toEqual([ expect(store.dispatch.mock.calls[1]).toEqual(['issueAnalytics/fetchChartData', TEST_HOST]);
'issueAnalytics/fetchChartData',
gl.TEST_HOST,
]);
done();
}); });
}); });
it('renders empty state when chart data is empty', done => { it('renders empty state when chart data is empty', () => {
vm.$store.state.issueAnalytics.chartData = {}; wrapper.vm.$store.state.issueAnalytics.chartData = {};
vm.$nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(vm.$el.querySelector('.empty-state')).not.toBe(null); expect(findEmptyState().exists()).toBe(true);
expect(vm.showNoDataEmptyState).toBe(true); expect(wrapper.vm.showNoDataEmptyState).toBe(true);
done();
}); });
}); });
it('renders filters empty state when filters are applied and chart data is empty', done => { it('renders filters empty state when filters are applied and chart data is empty', () => {
vm.$store.state.issueAnalytics.chartData = {}; wrapper.vm.$store.state.issueAnalytics.chartData = {};
vm.$store.state.issueAnalytics.filters = '?hello=world'; wrapper.vm.$store.state.issueAnalytics.filters = '?hello=world';
vm.$nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(vm.$el.querySelector('.empty-state')).not.toBe(null); expect(findEmptyState().exists()).toBe(true);
expect(vm.showFiltersEmptyState).toBe(true); expect(wrapper.vm.showFiltersEmptyState).toBe(true);
done();
}); });
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment