Commit a3651c02 authored by Sean McGivern's avatar Sean McGivern

Move performance bar component specs to Jest

The main change is in using vue-test-utils, which don't work well with
Karma, but make our specs cleaner.
parent a5c9dfa1
import Vue from 'vue'; import DetailedMetric from '~/performance_bar/components/detailed_metric.vue';
import detailedMetric from '~/performance_bar/components/detailed_metric.vue'; import RequestWarning from '~/performance_bar/components/request_warning.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import { shallowMount } from '@vue/test-utils';
describe('detailedMetric', () => { describe('detailedMetric', () => {
let vm; const createComponent = props =>
shallowMount(DetailedMetric, {
afterEach(() => { propsData: {
vm.$destroy(); ...props,
}); },
});
describe('when the current request has no details', () => { describe('when the current request has no details', () => {
beforeEach(() => { const wrapper = createComponent({
vm = mountComponent(Vue.extend(detailedMetric), { currentRequest: {},
currentRequest: {}, metric: 'gitaly',
metric: 'gitaly', header: 'Gitaly calls',
header: 'Gitaly calls', details: 'details',
details: 'details', keys: ['feature', 'request'],
keys: ['feature', 'request'],
});
}); });
it('does not render the element', () => { it('does not render the element', () => {
expect(vm.$el.innerHTML).toEqual(undefined); expect(wrapper.isEmpty()).toBe(true);
}); });
}); });
...@@ -31,8 +30,8 @@ describe('detailedMetric', () => { ...@@ -31,8 +30,8 @@ describe('detailedMetric', () => {
{ duration: '23', feature: 'rebase_in_progress', request: '', backtrace: ['world', 'hello'] }, { duration: '23', feature: 'rebase_in_progress', request: '', backtrace: ['world', 'hello'] },
]; ];
beforeEach(() => { describe('with a default metric name', () => {
vm = mountComponent(Vue.extend(detailedMetric), { const wrapper = createComponent({
currentRequest: { currentRequest: {
details: { details: {
gitaly: { gitaly: {
...@@ -47,67 +46,65 @@ describe('detailedMetric', () => { ...@@ -47,67 +46,65 @@ describe('detailedMetric', () => {
header: 'Gitaly calls', header: 'Gitaly calls',
keys: ['feature', 'request'], keys: ['feature', 'request'],
}); });
});
it('diplays details', () => { it('displays details', () => {
expect(vm.$el.innerText.replace(/\s+/g, ' ')).toContain('123ms / 456'); expect(wrapper.text().replace(/\s+/g, ' ')).toContain('123ms / 456');
}); });
it('adds a modal with a table of the details', () => {
vm.$el
.querySelectorAll('.performance-bar-modal td:nth-child(1)')
.forEach((duration, index) => {
expect(duration.innerText).toContain(requestDetails[index].duration);
});
vm.$el
.querySelectorAll('.performance-bar-modal td:nth-child(2)')
.forEach((feature, index) => {
expect(feature.innerText).toContain(requestDetails[index].feature);
});
vm.$el it('adds a modal with a table of the details', () => {
.querySelectorAll('.performance-bar-modal td:nth-child(2)') wrapper
.forEach((request, index) => { .findAll('.performance-bar-modal td:nth-child(1)')
expect(request.innerText).toContain(requestDetails[index].request); .wrappers.forEach((duration, index) => {
expect(duration.text()).toContain(requestDetails[index].duration);
});
wrapper
.findAll('.performance-bar-modal td:nth-child(2)')
.wrappers.forEach((feature, index) => {
expect(feature.text()).toContain(requestDetails[index].feature);
});
wrapper
.findAll('.performance-bar-modal td:nth-child(2)')
.wrappers.forEach((request, index) => {
expect(request.text()).toContain(requestDetails[index].request);
});
expect(wrapper.find('.text-expander.js-toggle-button')).not.toBeNull();
wrapper.findAll('.performance-bar-modal td:nth-child(2)').wrappers.forEach(request => {
expect(request.text()).toContain('world');
}); });
expect(vm.$el.querySelector('.text-expander.js-toggle-button')).not.toBeNull();
vm.$el.querySelectorAll('.performance-bar-modal td:nth-child(2)').forEach(request => {
expect(request.innerText).toContain('world');
}); });
});
it('displays the metric title', () => { it('displays the metric title', () => {
expect(vm.$el.innerText).toContain('gitaly'); expect(wrapper.text()).toContain('gitaly');
}); });
it('displays request warnings', () => { it('displays request warnings', () => {
expect(vm.$el.querySelector('gl-emoji').dataset.name).toEqual('warning'); expect(wrapper.find(RequestWarning).exists()).toBe(true);
});
}); });
describe('when using a custom metric title', () => { describe('when using a custom metric title', () => {
beforeEach(() => { const wrapper = createComponent({
vm = mountComponent(Vue.extend(detailedMetric), { currentRequest: {
currentRequest: { details: {
details: { gitaly: {
gitaly: { duration: '123ms',
duration: '123ms', calls: '456',
calls: '456', details: requestDetails,
details: requestDetails,
},
}, },
}, },
metric: 'gitaly', },
title: 'custom', metric: 'gitaly',
header: 'Gitaly calls', title: 'custom',
keys: ['feature', 'request'], header: 'Gitaly calls',
}); keys: ['feature', 'request'],
}); });
it('displays the custom title', () => { it('displays the custom title', () => {
expect(vm.$el.innerText).toContain('custom'); expect(wrapper.text()).toContain('custom');
}); });
}); });
}); });
......
import Vue from 'vue'; import PerformanceBarApp from '~/performance_bar/components/performance_bar_app.vue';
import performanceBarApp from '~/performance_bar/components/performance_bar_app.vue';
import PerformanceBarStore from '~/performance_bar/stores/performance_bar_store'; import PerformanceBarStore from '~/performance_bar/stores/performance_bar_store';
import { shallowMount } from '@vue/test-utils';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
describe('performance bar app', () => { describe('performance bar app', () => {
let vm; const store = new PerformanceBarStore();
const wrapper = shallowMount(PerformanceBarApp, {
beforeEach(() => { propsData: {
const store = new PerformanceBarStore();
vm = mountComponent(Vue.extend(performanceBarApp), {
store, store,
env: 'development', env: 'development',
requestId: '123', requestId: '123',
peekUrl: '/-/peek/results', peekUrl: '/-/peek/results',
profileUrl: '?lineprofiler=true', profileUrl: '?lineprofiler=true',
}); },
});
afterEach(() => {
vm.$destroy();
}); });
it('sets the class to match the environment', () => { it('sets the class to match the environment', () => {
expect(vm.$el.getAttribute('class')).toContain('development'); expect(wrapper.element.getAttribute('class')).toContain('development');
}); });
}); });
import Vue from 'vue'; import RequestSelector from '~/performance_bar/components/request_selector.vue';
import requestSelector from '~/performance_bar/components/request_selector.vue'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
describe('request selector', () => { describe('request selector', () => {
const requests = [ const requests = [
...@@ -26,21 +25,18 @@ describe('request selector', () => { ...@@ -26,21 +25,18 @@ describe('request selector', () => {
}, },
]; ];
let vm; const wrapper = shallowMount(RequestSelector, {
propsData: {
beforeEach(() => {
vm = mountComponent(Vue.extend(requestSelector), {
requests, requests,
currentRequest: requests[1], currentRequest: requests[1],
}); },
});
afterEach(() => {
vm.$destroy();
}); });
function optionText(requestId) { function optionText(requestId) {
return vm.$el.querySelector(`[value='${requestId}']`).innerText.trim(); return wrapper
.find(`[value='${requestId}']`)
.text()
.trim();
} }
it('displays the last component of the path', () => { it('displays the last component of the path', () => {
...@@ -56,13 +52,13 @@ describe('request selector', () => { ...@@ -56,13 +52,13 @@ describe('request selector', () => {
}); });
it('has a warning icon if any requests have warnings', () => { it('has a warning icon if any requests have warnings', () => {
expect(vm.$el.querySelector('span > gl-emoji').dataset.name).toEqual('warning'); expect(wrapper.find('span > gl-emoji').element.dataset.name).toEqual('warning');
}); });
it('adds a warning icon to requests with warnings', () => { it('adds a warning icon to requests with warnings', () => {
const option = vm.$el.querySelector('[value="abc"]'); const option = wrapper.find('[value="abc"]');
expect(option.querySelector('gl-emoji').dataset.name).toEqual('warning'); expect(option.find('gl-emoji').element.dataset.name).toEqual('warning');
expect(option.innerText).toContain('discussions.json'); expect(option.text()).toContain('discussions.json');
}); });
}); });
import Vue from 'vue'; import RequestWarning from '~/performance_bar/components/request_warning.vue';
import requestWarning from '~/performance_bar/components/request_warning.vue'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
describe('request warning', () => { describe('request warning', () => {
const htmlId = 'request-123'; const htmlId = 'request-123';
let vm;
afterEach(() => {
vm.$destroy();
});
describe('when the request has warnings', () => { describe('when the request has warnings', () => {
beforeEach(() => { const wrapper = shallowMount(RequestWarning, {
vm = mountComponent(Vue.extend(requestWarning), { propsData: {
htmlId, htmlId,
details: { details: {
warnings: ['gitaly calls: 30 over 10', 'gitaly duration: 1500 over 1000'], warnings: ['gitaly calls: 30 over 10', 'gitaly duration: 1500 over 1000'],
}, },
}); },
}); });
it('adds a warning emoji with the correct ID', () => { it('adds a warning emoji with the correct ID', () => {
const wrapper = vm.$el.querySelector('span[id]'); expect(wrapper.find('span[id]').attributes('id')).toEqual(htmlId);
expect(wrapper.find('span[id] gl-emoji').element.dataset.name).toEqual('warning');
expect(wrapper.id).toEqual(htmlId);
expect(wrapper.querySelector('gl-emoji').dataset.name).toEqual('warning');
}); });
}); });
describe('when the request does not have warnings', () => { describe('when the request does not have warnings', () => {
beforeEach(() => { const wrapper = shallowMount(RequestWarning, {
vm = mountComponent(Vue.extend(requestWarning), { propsData: {
htmlId, htmlId,
details: { details: {
warnings: [], warnings: [],
}, },
}); },
}); });
it('does nothing', () => { it('does nothing', () => {
expect(vm.$el.innerText).toBeUndefined(); expect(wrapper.isEmpty()).toBe(true);
}); });
}); });
}); });
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