Migrate pipelines specs to Jest & VTU

Migrated pipelines specs that rely on tooltips to Jest & Vue Test Utils
parent 22bb21fb
import Vue from 'vue'; import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import actionComponent from '~/pipelines/components/graph/action_component.vue'; import ActionComponent from '~/pipelines/components/graph/action_component.vue';
import mountComponent from '../../helpers/vue_mount_component_helper';
describe('pipeline graph action component', () => { describe('pipeline graph action component', () => {
let component; let wrapper;
let mock; let mock;
beforeEach(done => { beforeEach(() => {
const ActionComponent = Vue.extend(actionComponent);
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
mock.onPost('foo.json').reply(200); mock.onPost('foo.json').reply(200);
component = mountComponent(ActionComponent, { wrapper = mount(ActionComponent, {
propsData: {
tooltipText: 'bar', tooltipText: 'bar',
link: 'foo', link: 'foo',
actionIcon: 'cancel', actionIcon: 'cancel',
},
sync: false,
}); });
Vue.nextTick(done);
}); });
afterEach(() => { afterEach(() => {
mock.restore(); mock.restore();
component.$destroy(); wrapper.destroy();
}); });
it('should render the provided title as a bootstrap tooltip', () => { it('should render the provided title as a bootstrap tooltip', () => {
expect(component.$el.getAttribute('data-original-title')).toEqual('bar'); expect(wrapper.attributes('data-original-title')).toBe('bar');
}); });
it('should update bootstrap tooltip when title changes', done => { it('should update bootstrap tooltip when title changes', done => {
component.tooltipText = 'changed'; wrapper.setProps({ tooltipText: 'changed' });
component wrapper.vm
.$nextTick() .$nextTick()
.then(() => { .then(() => {
expect(component.$el.getAttribute('data-original-title')).toBe('changed'); expect(wrapper.attributes('data-original-title')).toBe('changed');
}) })
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
}); });
it('should render an svg', () => { it('should render an svg', () => {
expect(component.$el.querySelector('.ci-action-icon-wrapper')).toBeDefined(); expect(wrapper.find('.ci-action-icon-wrapper')).toBeDefined();
expect(component.$el.querySelector('svg')).toBeDefined(); expect(wrapper.find('svg')).toBeDefined();
}); });
describe('on click', () => { describe('on click', () => {
it('emits `pipelineActionRequestComplete` after a successful request', done => { it('emits `pipelineActionRequestComplete` after a successful request', done => {
spyOn(component, '$emit'); jest.spyOn(wrapper.vm, '$emit');
component.$el.click(); wrapper.find('button').trigger('click');
setTimeout(() => { waitForPromises()
component
.$nextTick()
.then(() => { .then(() => {
expect(component.$emit).toHaveBeenCalledWith('pipelineActionRequestComplete'); expect(wrapper.vm.$emit).toHaveBeenCalledWith('pipelineActionRequestComplete');
done();
}) })
.catch(done.fail); .catch(done.fail);
done();
}, 0);
}); });
it('renders a loading icon while waiting for request', done => { it('renders a loading icon while waiting for request', done => {
component.$el.click(); wrapper.find('button').trigger('click');
component.$nextTick(() => { wrapper.vm.$nextTick(() => {
expect(component.$el.querySelector('.js-action-icon-loading')).not.toBeNull(); expect(wrapper.find('.js-action-icon-loading').exists()).toBe(true);
setTimeout(() => {
done(); done();
}); });
}); });
}); });
});
}); });
...@@ -17,6 +17,7 @@ describe('Pipelines Triggerer', () => { ...@@ -17,6 +17,7 @@ describe('Pipelines Triggerer', () => {
const createComponent = () => { const createComponent = () => {
wrapper = mount(pipelineTriggerer, { wrapper = mount(pipelineTriggerer, {
propsData: mockData, propsData: mockData,
sync: false,
}); });
}; };
...@@ -49,6 +50,8 @@ describe('Pipelines Triggerer', () => { ...@@ -49,6 +50,8 @@ describe('Pipelines Triggerer', () => {
}, },
}); });
wrapper.vm.$nextTick(() => {
expect(wrapper.find('.js-pipeline-url-api').text()).toEqual('API'); expect(wrapper.find('.js-pipeline-url-api').text()).toEqual('API');
}); });
});
}); });
import Vue from 'vue'; import { mount } from '@vue/test-utils';
import tableRowComp from '~/pipelines/components/pipelines_table_row.vue'; import PipelinesTableRowComponent from '~/pipelines/components/pipelines_table_row.vue';
import eventHub from '~/pipelines/event_hub'; import eventHub from '~/pipelines/event_hub';
describe('Pipelines Table Row', () => { describe('Pipelines Table Row', () => {
const jsonFixtureName = 'pipelines/pipelines.json'; const jsonFixtureName = 'pipelines/pipelines.json';
const buildComponent = pipeline => {
const PipelinesTableRowComponent = Vue.extend(tableRowComp); const createWrapper = pipeline =>
return new PipelinesTableRowComponent({ mount(PipelinesTableRowComponent, {
el: document.querySelector('.test-dom-element'),
propsData: { propsData: {
pipeline, pipeline,
autoDevopsHelpPath: 'foo', autoDevopsHelpPath: 'foo',
viewType: 'root', viewType: 'root',
}, },
}).$mount(); sync: false,
}; });
let component; let wrapper;
let pipeline; let pipeline;
let pipelineWithoutAuthor; let pipelineWithoutAuthor;
let pipelineWithoutCommit; let pipelineWithoutCommit;
...@@ -32,28 +31,29 @@ describe('Pipelines Table Row', () => { ...@@ -32,28 +31,29 @@ describe('Pipelines Table Row', () => {
}); });
afterEach(() => { afterEach(() => {
component.$destroy(); wrapper.destroy();
wrapper = null;
}); });
it('should render a table row', () => { it('should render a table row', () => {
component = buildComponent(pipeline); wrapper = createWrapper(pipeline);
expect(component.$el.getAttribute('class')).toContain('gl-responsive-table-row'); expect(wrapper.attributes('class')).toContain('gl-responsive-table-row');
}); });
describe('status column', () => { describe('status column', () => {
beforeEach(() => { beforeEach(() => {
component = buildComponent(pipeline); wrapper = createWrapper(pipeline);
}); });
it('should render a pipeline link', () => { it('should render a pipeline link', () => {
expect( expect(wrapper.find('.table-section.commit-link a').attributes('href')).toEqual(
component.$el.querySelector('.table-section.commit-link a').getAttribute('href'), pipeline.path,
).toEqual(pipeline.path); );
}); });
it('should render status text', () => { it('should render status text', () => {
expect(component.$el.querySelector('.table-section.commit-link a').textContent).toContain( expect(wrapper.find('.table-section.commit-link a').text()).toContain(
pipeline.details.status.text, pipeline.details.status.text,
); );
}); });
...@@ -61,33 +61,32 @@ describe('Pipelines Table Row', () => { ...@@ -61,33 +61,32 @@ describe('Pipelines Table Row', () => {
describe('information column', () => { describe('information column', () => {
beforeEach(() => { beforeEach(() => {
component = buildComponent(pipeline); wrapper = createWrapper(pipeline);
}); });
it('should render a pipeline link', () => { it('should render a pipeline link', () => {
expect( expect(wrapper.find('.table-section:nth-child(2) a').attributes('href')).toEqual(
component.$el.querySelector('.table-section:nth-child(2) a').getAttribute('href'), pipeline.path,
).toEqual(pipeline.path); );
}); });
it('should render pipeline ID', () => { it('should render pipeline ID', () => {
expect( expect(wrapper.find('.table-section:nth-child(2) a > span').text()).toEqual(
component.$el.querySelector('.table-section:nth-child(2) a > span').textContent, `#${pipeline.id}`,
).toEqual(`#${pipeline.id}`); );
}); });
describe('when a user is provided', () => { describe('when a user is provided', () => {
it('should render user information', () => { it('should render user information', () => {
expect( expect(
component.$el wrapper.find('.table-section:nth-child(3) .js-pipeline-url-user').attributes('href'),
.querySelector('.table-section:nth-child(3) .js-pipeline-url-user')
.getAttribute('href'),
).toEqual(pipeline.user.path); ).toEqual(pipeline.user.path);
expect( expect(
component.$el wrapper
.querySelector('.table-section:nth-child(3) .js-user-avatar-image-toolip') .find('.table-section:nth-child(3) .js-user-avatar-image-toolip')
.textContent.trim(), .text()
.trim(),
).toEqual(pipeline.user.name); ).toEqual(pipeline.user.name);
}); });
}); });
...@@ -95,40 +94,47 @@ describe('Pipelines Table Row', () => { ...@@ -95,40 +94,47 @@ describe('Pipelines Table Row', () => {
describe('commit column', () => { describe('commit column', () => {
it('should render link to commit', () => { it('should render link to commit', () => {
component = buildComponent(pipeline); wrapper = createWrapper(pipeline);
const commitLink = component.$el.querySelector('.branch-commit .commit-sha'); const commitLink = wrapper.find('.branch-commit .commit-sha');
expect(commitLink.getAttribute('href')).toEqual(pipeline.commit.commit_path); expect(commitLink.attributes('href')).toEqual(pipeline.commit.commit_path);
}); });
const findElements = () => { const findElements = () => {
const commitTitleElement = component.$el.querySelector('.branch-commit .commit-title'); const commitTitleElement = wrapper.find('.branch-commit .commit-title');
const commitAuthorElement = commitTitleElement.querySelector('a.avatar-image-container'); const commitAuthorElement = commitTitleElement.find('a.avatar-image-container');
if (!commitAuthorElement) { if (!commitAuthorElement.exists()) {
return { commitAuthorElement }; return {
commitAuthorElement,
};
} }
const commitAuthorLink = commitAuthorElement.getAttribute('href'); const commitAuthorLink = commitAuthorElement.attributes('href');
const commitAuthorName = commitAuthorElement const commitAuthorName = commitAuthorElement
.querySelector('.js-user-avatar-image-toolip') .find('.js-user-avatar-image-toolip')
.textContent.trim(); .text()
.trim();
return { commitAuthorElement, commitAuthorLink, commitAuthorName };
return {
commitAuthorElement,
commitAuthorLink,
commitAuthorName,
};
}; };
it('renders nothing without commit', () => { it('renders nothing without commit', () => {
expect(pipelineWithoutCommit.commit).toBe(null); expect(pipelineWithoutCommit.commit).toBe(null);
component = buildComponent(pipelineWithoutCommit);
wrapper = createWrapper(pipelineWithoutCommit);
const { commitAuthorElement } = findElements(); const { commitAuthorElement } = findElements();
expect(commitAuthorElement).toBe(null); expect(commitAuthorElement.exists()).toBe(false);
}); });
it('renders commit author', () => { it('renders commit author', () => {
component = buildComponent(pipeline); wrapper = createWrapper(pipeline);
const { commitAuthorLink, commitAuthorName } = findElements(); const { commitAuthorLink, commitAuthorName } = findElements();
expect(commitAuthorLink).toEqual(pipeline.commit.author.path); expect(commitAuthorLink).toEqual(pipeline.commit.author.path);
...@@ -137,8 +143,8 @@ describe('Pipelines Table Row', () => { ...@@ -137,8 +143,8 @@ describe('Pipelines Table Row', () => {
it('renders commit with unregistered author', () => { it('renders commit with unregistered author', () => {
expect(pipelineWithoutAuthor.commit.author).toBe(null); expect(pipelineWithoutAuthor.commit.author).toBe(null);
component = buildComponent(pipelineWithoutAuthor);
wrapper = createWrapper(pipelineWithoutAuthor);
const { commitAuthorLink, commitAuthorName } = findElements(); const { commitAuthorLink, commitAuthorName } = findElements();
expect(commitAuthorLink).toEqual(`mailto:${pipelineWithoutAuthor.commit.author_email}`); expect(commitAuthorLink).toEqual(`mailto:${pipelineWithoutAuthor.commit.author_email}`);
...@@ -148,13 +154,12 @@ describe('Pipelines Table Row', () => { ...@@ -148,13 +154,12 @@ describe('Pipelines Table Row', () => {
describe('stages column', () => { describe('stages column', () => {
beforeEach(() => { beforeEach(() => {
component = buildComponent(pipeline); wrapper = createWrapper(pipeline);
}); });
it('should render an icon for each stage', () => { it('should render an icon for each stage', () => {
expect( expect(
component.$el.querySelectorAll('.table-section:nth-child(4) .js-builds-dropdown-button') wrapper.findAll('.table-section:nth-child(4) .js-builds-dropdown-button').length,
.length,
).toEqual(pipeline.details.stages.length); ).toEqual(pipeline.details.stages.length);
}); });
}); });
...@@ -172,44 +177,49 @@ describe('Pipelines Table Row', () => { ...@@ -172,44 +177,49 @@ describe('Pipelines Table Row', () => {
withActions.cancel_path = '/cancel'; withActions.cancel_path = '/cancel';
withActions.retry_path = '/retry'; withActions.retry_path = '/retry';
component = buildComponent(withActions); wrapper = createWrapper(withActions);
}); });
it('should render the provided actions', () => { it('should render the provided actions', () => {
expect(component.$el.querySelector('.js-pipelines-retry-button')).not.toBeNull(); expect(wrapper.find('.js-pipelines-retry-button').exists()).toBe(true);
expect(component.$el.querySelector('.js-pipelines-cancel-button')).not.toBeNull(); expect(wrapper.find('.js-pipelines-cancel-button').exists()).toBe(true);
const dropdownMenu = component.$el.querySelectorAll('.dropdown-menu'); const dropdownMenu = wrapper.find('.dropdown-menu');
expect(dropdownMenu).toContainText(scheduledJobAction.name); expect(dropdownMenu.text()).toContain(scheduledJobAction.name);
}); });
it('emits `retryPipeline` event when retry button is clicked and toggles loading', () => { it('emits `retryPipeline` event when retry button is clicked and toggles loading', () => {
eventHub.$on('retryPipeline', endpoint => { eventHub.$on('retryPipeline', endpoint => {
expect(endpoint).toEqual('/retry'); expect(endpoint).toBe('/retry');
}); });
component.$el.querySelector('.js-pipelines-retry-button').click(); wrapper.find('.js-pipelines-retry-button').trigger('click');
expect(wrapper.vm.isRetrying).toBe(true);
expect(component.isRetrying).toEqual(true);
}); });
it('emits `openConfirmationModal` event when cancel button is clicked and toggles loading', () => { it('emits `openConfirmationModal` event when cancel button is clicked and toggles loading', () => {
eventHub.$once('openConfirmationModal', data => { eventHub.$once('openConfirmationModal', data => {
const { id, ref, commit } = pipeline; const { id, ref, commit } = pipeline;
expect(data.endpoint).toEqual('/cancel'); expect(data.endpoint).toBe('/cancel');
expect(data.pipeline).toEqual(jasmine.objectContaining({ id, ref, commit })); expect(data.pipeline).toEqual(
expect.objectContaining({
id,
ref,
commit,
}),
);
}); });
component.$el.querySelector('.js-pipelines-cancel-button').click(); wrapper.find('.js-pipelines-cancel-button').trigger('click');
}); });
it('renders a loading icon when `cancelingPipeline` matches pipeline id', done => { it('renders a loading icon when `cancelingPipeline` matches pipeline id', done => {
component.cancelingPipeline = pipeline.id; wrapper.setProps({ cancelingPipeline: pipeline.id });
component wrapper.vm
.$nextTick() .$nextTick()
.then(() => { .then(() => {
expect(component.isCancelling).toEqual(true); expect(wrapper.vm.isCancelling).toBe(true);
}) })
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
......
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