Commit d1bba971 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'vs-migrate-insights-to-jest' into 'master'

Migrate ee/insights to jest

Closes #194290

See merge request gitlab-org/gitlab!29072
parents 1cba8539 60bcc616
...@@ -20,11 +20,11 @@ describe('Insights chart error component', () => { ...@@ -20,11 +20,11 @@ describe('Insights chart error component', () => {
}); });
it('renders the component', () => { it('renders the component', () => {
expect(wrapper.find('.content-title').text()).toEqual(`${title}: "${chartName}"`); expect(wrapper.find('.content-title').text()).toBe(`${title}: "${chartName}"`);
const summaries = wrapper.findAll('.content-summary'); const summaries = wrapper.findAll('.content-summary');
expect(summaries.at(0).text()).toEqual(summary); expect(summaries.at(0).text()).toBe(summary);
expect(summaries.at(1).text()).toEqual(error); expect(summaries.at(1).text()).toBe(error);
}); });
}); });
import { GlColumnChart, GlLineChart, GlStackedColumnChart } from '@gitlab/ui/dist/charts'; import { GlColumnChart, GlLineChart, GlStackedColumnChart } from '@gitlab/ui/dist/charts';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { chartInfo, barChartData, lineChartData, stackedBarChartData } from '../mock_data'; import {
chartInfo,
barChartData,
lineChartData,
stackedBarChartData,
} from 'ee_jest/insights/mock_data';
import InsightsChart from 'ee/insights/components/insights_chart.vue'; import InsightsChart from 'ee/insights/components/insights_chart.vue';
import InsightsChartError from 'ee/insights/components/insights_chart_error.vue'; import InsightsChartError from 'ee/insights/components/insights_chart_error.vue';
import { CHART_TYPES } from 'ee/insights/constants'; import { CHART_TYPES } from 'ee/insights/constants';
......
...@@ -27,7 +27,7 @@ describe('Insights config warning component', () => { ...@@ -27,7 +27,7 @@ describe('Insights config warning component', () => {
.attributes('src'), .attributes('src'),
).toContain(image); ).toContain(image);
expect(wrapper.find('.content-title').text()).toEqual(title); expect(wrapper.find('.content-title').text()).toBe(title);
expect(wrapper.find('.content-summary').text()).toEqual(summary); expect(wrapper.find('.content-summary').text()).toBe(summary);
}); });
}); });
import Vue from 'vue'; import Vue from 'vue';
import InsightsPage from 'ee/insights/components/insights_page.vue'; import InsightsPage from 'ee/insights/components/insights_page.vue';
import { createStore } from 'ee/insights/stores'; import { createStore } from 'ee/insights/stores';
import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { mountComponentWithStore } from 'helpers/vue_mount_component_helper';
import { chartInfo, pageInfo, pageInfoNoCharts } from '../mock_data'; import { TEST_HOST } from 'helpers/test_constants';
import { chartInfo, pageInfo, pageInfoNoCharts } from 'ee_jest/insights/mock_data';
describe('Insights page component', () => { describe('Insights page component', () => {
let component; let component;
...@@ -11,7 +12,7 @@ describe('Insights page component', () => { ...@@ -11,7 +12,7 @@ describe('Insights page component', () => {
beforeEach(() => { beforeEach(() => {
store = createStore(); store = createStore();
spyOn(store, 'dispatch').and.stub(); jest.spyOn(store, 'dispatch').mockImplementation(() => {});
Component = Vue.extend(InsightsPage); Component = Vue.extend(InsightsPage);
}); });
...@@ -20,17 +21,16 @@ describe('Insights page component', () => { ...@@ -20,17 +21,16 @@ describe('Insights page component', () => {
}); });
describe('no chart config available', () => { describe('no chart config available', () => {
it('shows an empty state', done => { it('shows an empty state', () => {
component = mountComponentWithStore(Component, { component = mountComponentWithStore(Component, {
store, store,
props: { props: {
queryEndpoint: `${gl.TEST_HOST}/query`, queryEndpoint: `${TEST_HOST}/query`,
pageConfig: pageInfoNoCharts, pageConfig: pageInfoNoCharts,
}, },
}); });
expect(component.$el.querySelector('.js-empty-state')).not.toBe(null); expect(component.$el.querySelector('.js-empty-state')).not.toBe(null);
done();
}); });
}); });
...@@ -39,18 +39,17 @@ describe('Insights page component', () => { ...@@ -39,18 +39,17 @@ describe('Insights page component', () => {
component = mountComponentWithStore(Component, { component = mountComponentWithStore(Component, {
store, store,
props: { props: {
queryEndpoint: `${gl.TEST_HOST}/query`, queryEndpoint: `${TEST_HOST}/query`,
pageConfig: pageInfo, pageConfig: pageInfo,
}, },
}); });
}); });
it('fetches chart data when mounted', done => { it('fetches chart data when mounted', () => {
expect(store.dispatch).toHaveBeenCalledWith('insights/fetchChartData', { expect(store.dispatch).toHaveBeenCalledWith('insights/fetchChartData', {
endpoint: `${gl.TEST_HOST}/query`, endpoint: `${TEST_HOST}/query`,
chart: chartInfo, chart: chartInfo,
}); });
done();
}); });
describe('when charts loading', () => { describe('when charts loading', () => {
...@@ -58,35 +57,29 @@ describe('Insights page component', () => { ...@@ -58,35 +57,29 @@ describe('Insights page component', () => {
component.$store.state.insights.pageLoading = true; component.$store.state.insights.pageLoading = true;
}); });
it('renders loading state', done => { it('renders loading state', () => {
Vue.nextTick(() => { return component.$nextTick(() => {
expect( expect(
component.$el.querySelector('.js-insights-page-container .insights-chart-loading'), component.$el.querySelector('.js-insights-page-container .insights-chart-loading'),
).not.toBe(null); ).not.toBe(null);
done();
}); });
}); });
it('does not display chart area', done => { it('does not display chart area', () => {
Vue.nextTick(() => { return component.$nextTick(() => {
expect(component.$el.querySelector('.js-insights-page-container .insights-charts')).toBe( expect(component.$el.querySelector('.js-insights-page-container .insights-charts')).toBe(
null, null,
); );
done();
}); });
}); });
}); });
describe('pageConfig changes', () => { describe('pageConfig changes', () => {
it('reflects new state', done => { it('reflects new state', () => {
// Establish rendered state
component.$nextTick();
component.pageConfig = pageInfoNoCharts; component.pageConfig = pageInfoNoCharts;
component.$nextTick(() => { return component.$nextTick(() => {
expect(component.$el.querySelector('.js-empty-state')).not.toBe(null); expect(component.$el.querySelector('.js-empty-state')).not.toBe(null);
done();
}); });
}); });
}); });
......
import Vue from 'vue'; import Vue from 'vue';
import { TEST_HOST } from 'helpers/test_constants';
import Insights from 'ee/insights/components/insights.vue'; import Insights from 'ee/insights/components/insights.vue';
import { createStore } from 'ee/insights/stores'; import { createStore } from 'ee/insights/stores';
import createRouter from 'ee/insights/insights_router'; import createRouter from 'ee/insights/insights_router';
import { pageInfo } from '../mock_data'; import { pageInfo } from 'ee_jest/insights/mock_data';
describe('Insights component', () => { describe('Insights component', () => {
let vm; let vm;
...@@ -13,14 +14,14 @@ describe('Insights component', () => { ...@@ -13,14 +14,14 @@ describe('Insights component', () => {
beforeEach(() => { beforeEach(() => {
store = createStore(); store = createStore();
spyOn(store, 'dispatch').and.stub(); jest.spyOn(store, 'dispatch').mockImplementation(() => {});
mountComponent = data => { mountComponent = data => {
const el = null; const el = null;
const props = data || { const props = data || {
endpoint: gl.TEST_HOST, endpoint: TEST_HOST,
queryEndpoint: `${gl.TEST_HOST}/query`, queryEndpoint: `${TEST_HOST}/query`,
}; };
return new Component({ return new Component({
...@@ -34,22 +35,21 @@ describe('Insights component', () => { ...@@ -34,22 +35,21 @@ describe('Insights component', () => {
}); });
afterEach(() => { afterEach(() => {
store.dispatch.mockReset();
vm.$destroy(); vm.$destroy();
}); });
it('fetches config data when mounted', done => { it('fetches config data when mounted', () => {
expect(store.dispatch).toHaveBeenCalledWith('insights/fetchConfigData', gl.TEST_HOST); expect(store.dispatch).toHaveBeenCalledWith('insights/fetchConfigData', TEST_HOST);
done();
}); });
describe('when loading config', () => { describe('when loading config', () => {
it('renders config loading state', done => { it('renders config loading state', () => {
vm.$store.state.insights.configLoading = true; vm.$store.state.insights.configLoading = true;
vm.$nextTick(() => { return vm.$nextTick(() => {
expect(vm.$el.querySelector('.insights-config-loading')).not.toBe(null); expect(vm.$el.querySelector('.insights-config-loading')).not.toBe(null);
expect(vm.$el.querySelector('.insights-wrapper')).toBe(null); expect(vm.$el.querySelector('.insights-wrapper')).toBe(null);
done();
}); });
}); });
}); });
...@@ -68,13 +68,12 @@ describe('Insights component', () => { ...@@ -68,13 +68,12 @@ describe('Insights component', () => {
}; };
}); });
it('has the correct nav tabs', done => { it('has the correct nav tabs', () => {
vm.$nextTick(() => { return vm.$nextTick(() => {
expect(vm.$el.querySelector('.js-insights-dropdown')).not.toBe(null); expect(vm.$el.querySelector('.js-insights-dropdown')).not.toBe(null);
expect(vm.$el.querySelector('.js-insights-dropdown .dropdown-item').innerText.trim()).toBe( expect(vm.$el.querySelector('.js-insights-dropdown .dropdown-item').innerText.trim()).toBe(
title, title,
); );
done();
}); });
}); });
...@@ -83,12 +82,11 @@ describe('Insights component', () => { ...@@ -83,12 +82,11 @@ describe('Insights component', () => {
vm.$store.state.insights.pageLoading = true; vm.$store.state.insights.pageLoading = true;
}); });
it('disables the tab selector', done => { it('disables the tab selector', () => {
vm.$nextTick(() => { return vm.$nextTick(() => {
expect( expect(
vm.$el.querySelector('.js-insights-dropdown > button').getAttribute('disabled'), vm.$el.querySelector('.js-insights-dropdown > button').getAttribute('disabled'),
).toBe('disabled'); ).toBe('disabled');
done();
}); });
}); });
}); });
...@@ -100,12 +98,11 @@ describe('Insights component', () => { ...@@ -100,12 +98,11 @@ describe('Insights component', () => {
vm.$store.state.insights.configData = null; vm.$store.state.insights.configData = null;
}); });
it('it displays a warning', done => { it('it displays a warning', () => {
vm.$nextTick(() => { return vm.$nextTick(() => {
expect(vm.$el.querySelector('.js-empty-state').innerText.trim()).toContain( expect(vm.$el.querySelector('.js-empty-state').innerText.trim()).toContain(
'Invalid Insights config file detected', 'Invalid Insights config file detected',
); );
done();
}); });
}); });
}); });
...@@ -128,22 +125,24 @@ describe('Insights component', () => { ...@@ -128,22 +125,24 @@ describe('Insights component', () => {
window.location.hash = ''; window.location.hash = '';
}); });
it('selects the first tab if invalid', done => { it('selects the first tab if invalid', () => {
window.location.hash = '#/invalid'; window.location.hash = '#/invalid';
vm.$nextTick(() => { jest.runOnlyPendingTimers();
return vm.$nextTick(() => {
expect(store.dispatch).toHaveBeenCalledWith('insights/setActiveTab', defaultKey); expect(store.dispatch).toHaveBeenCalledWith('insights/setActiveTab', defaultKey);
}); });
done();
}); });
it('selects the specified tab if valid', done => { it('selects the specified tab if valid', () => {
window.location.hash = `#/${selectedKey}`; window.location.hash = `#/${selectedKey}`;
vm.$nextTick(() => { jest.runOnlyPendingTimers();
return vm.$nextTick(() => {
expect(store.dispatch).toHaveBeenCalledWith('insights/setActiveTab', selectedKey); expect(store.dispatch).toHaveBeenCalledWith('insights/setActiveTab', selectedKey);
}); });
done();
}); });
}); });
}); });
...@@ -11,7 +11,7 @@ describe('insights router', () => { ...@@ -11,7 +11,7 @@ describe('insights router', () => {
it(`sets the activeTab when route changed`, () => { it(`sets the activeTab when route changed`, () => {
const route = 'route'; const route = 'route';
spyOn(store, 'dispatch').and.stub(); jest.spyOn(store, 'dispatch').mockImplementation(() => {});
router.push(`/${route}`); router.push(`/${route}`);
......
...@@ -3,7 +3,7 @@ import mutations from 'ee/insights/stores/modules/insights/mutations'; ...@@ -3,7 +3,7 @@ import mutations from 'ee/insights/stores/modules/insights/mutations';
import * as types from 'ee/insights/stores/modules/insights/mutation_types'; import * as types from 'ee/insights/stores/modules/insights/mutation_types';
import { CHART_TYPES } from 'ee/insights/constants'; import { CHART_TYPES } from 'ee/insights/constants';
import { configData } from '../../../../javascripts/insights/mock_data'; import { configData } from 'ee_jest/insights/mock_data';
describe('Insights mutations', () => { describe('Insights mutations', () => {
let state; let state;
......
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