Commit 6231aa41 authored by Illya Klymov's avatar Illya Klymov

Merge branch 'migrate-stage-spec' into 'master'

Migrate stage spec to jest

See merge request gitlab-org/gitlab!30859
parents 82ee4b7f 7173fa11
...@@ -137,7 +137,7 @@ export default { ...@@ -137,7 +137,7 @@ export default {
}, },
isDropdownOpen() { isDropdownOpen() {
return this.$el.classList.contains('open'); return this.$el.classList.contains('show');
}, },
pipelineActionRequestComplete() { pipelineActionRequestComplete() {
......
import Vue from 'vue'; import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import stage from '~/pipelines/components/stage.vue'; import StageComponent from '~/pipelines/components/stage.vue';
import eventHub from '~/pipelines/event_hub'; import eventHub from '~/pipelines/event_hub';
import { stageReply } from './mock_data'; import { stageReply } from './mock_data';
import waitForPromises from 'helpers/wait_for_promises';
describe('Pipelines stage component', () => { describe('Pipelines stage component', () => {
let StageComponent; let wrapper;
let component;
let mock; let mock;
beforeEach(() => { const defaultProps = {
mock = new MockAdapter(axios);
StageComponent = Vue.extend(stage);
component = mountComponent(StageComponent, {
stage: { stage: {
status: { status: {
group: 'success', group: 'success',
...@@ -26,51 +20,77 @@ describe('Pipelines stage component', () => { ...@@ -26,51 +20,77 @@ describe('Pipelines stage component', () => {
dropdown_path: 'path.json', dropdown_path: 'path.json',
}, },
updateDropdown: false, updateDropdown: false,
};
const createComponent = (props = {}) => {
wrapper = mount(StageComponent, {
propsData: {
...defaultProps,
...props,
},
}); });
};
beforeEach(() => {
mock = new MockAdapter(axios);
}); });
afterEach(() => { afterEach(() => {
component.$destroy(); wrapper.destroy();
wrapper = null;
mock.restore(); mock.restore();
}); });
describe('default', () => {
beforeEach(() => {
createComponent();
});
it('should render a dropdown with the status icon', () => { it('should render a dropdown with the status icon', () => {
expect(component.$el.getAttribute('class')).toEqual('dropdown'); expect(wrapper.attributes('class')).toEqual('dropdown');
expect(component.$el.querySelector('svg')).toBeDefined(); expect(wrapper.find('svg').exists()).toBe(true);
expect(component.$el.querySelector('button').getAttribute('data-toggle')).toEqual('dropdown'); expect(wrapper.find('button').attributes('data-toggle')).toEqual('dropdown');
});
}); });
describe('with successful request', () => { describe('with successful request', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet('path.json').reply(200, stageReply); mock.onGet('path.json').reply(200, stageReply);
createComponent();
}); });
it('should render the received data and emit `clickedDropdown` event', done => { it('should render the received data and emit `clickedDropdown` event', () => {
spyOn(eventHub, '$emit'); jest.spyOn(eventHub, '$emit');
component.$el.querySelector('button').click(); wrapper.find('button').trigger('click');
setTimeout(() => { return waitForPromises().then(() => {
expect( expect(wrapper.find('.js-builds-dropdown-container ul').text()).toContain(
component.$el.querySelector('.js-builds-dropdown-container ul').textContent.trim(), stageReply.latest_statuses[0].name,
).toContain(stageReply.latest_statuses[0].name); );
expect(eventHub.$emit).toHaveBeenCalledWith('clickedDropdown'); expect(eventHub.$emit).toHaveBeenCalledWith('clickedDropdown');
done(); });
}, 0);
}); });
}); });
describe('when request fails', () => { describe('when request fails', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet('path.json').reply(500); mock.onGet('path.json').reply(500);
createComponent();
}); });
it('should close the dropdown', () => { it('should close the dropdown', () => {
component.$el.click(); wrapper.setMethods({
closeDropdown: jest.fn(),
isDropdownOpen: jest.fn().mockReturnValue(false),
});
setTimeout(() => { wrapper.find('button').trigger('click');
expect(component.$el.classList.contains('open')).toEqual(false);
}, 0); return waitForPromises().then(() => {
expect(wrapper.vm.closeDropdown).toHaveBeenCalled();
});
}); });
}); });
...@@ -79,27 +99,29 @@ describe('Pipelines stage component', () => { ...@@ -79,27 +99,29 @@ describe('Pipelines stage component', () => {
const copyStage = Object.assign({}, stageReply); const copyStage = Object.assign({}, stageReply);
copyStage.latest_statuses[0].name = 'this is the updated content'; copyStage.latest_statuses[0].name = 'this is the updated content';
mock.onGet('bar.json').reply(200, copyStage); mock.onGet('bar.json').reply(200, copyStage);
}); createComponent({
stage: {
it('should update the stage to request the new endpoint provided', done => {
component.stage = {
status: { status: {
group: 'running', group: 'running',
icon: 'status_running', icon: 'status_running',
title: 'running', title: 'running',
}, },
dropdown_path: 'bar.json', dropdown_path: 'bar.json',
}; },
});
Vue.nextTick(() => {
component.$el.querySelector('button').click();
setTimeout(() => {
expect(
component.$el.querySelector('.js-builds-dropdown-container ul').textContent.trim(),
).toContain('this is the updated content');
done();
}); });
it('should update the stage to request the new endpoint provided', () => {
return wrapper.vm
.$nextTick()
.then(() => {
wrapper.find('button').trigger('click');
return waitForPromises();
})
.then(() => {
expect(wrapper.find('.js-builds-dropdown-container ul').text()).toContain(
'this is the updated content',
);
}); });
}); });
}); });
...@@ -109,27 +131,25 @@ describe('Pipelines stage component', () => { ...@@ -109,27 +131,25 @@ describe('Pipelines stage component', () => {
mock.onGet('path.json').reply(200, stageReply); mock.onGet('path.json').reply(200, stageReply);
mock.onPost(`${stageReply.latest_statuses[0].status.action.path}.json`).reply(200); mock.onPost(`${stageReply.latest_statuses[0].status.action.path}.json`).reply(200);
createComponent({ type: 'PIPELINES_TABLE' });
}); });
describe('within pipeline table', () => { describe('within pipeline table', () => {
it('emits `refreshPipelinesTable` event when `pipelineActionRequestComplete` is triggered', done => { it('emits `refreshPipelinesTable` event when `pipelineActionRequestComplete` is triggered', () => {
spyOn(eventHub, '$emit'); jest.spyOn(eventHub, '$emit');
component.type = 'PIPELINES_TABLE'; wrapper.find('button').trigger('click');
component.$el.querySelector('button').click();
setTimeout(() => { return waitForPromises()
component.$el.querySelector('.js-ci-action').click();
setTimeout(() => {
component
.$nextTick()
.then(() => { .then(() => {
expect(eventHub.$emit).toHaveBeenCalledWith('refreshPipelinesTable'); wrapper.find('.js-ci-action').trigger('click');
return waitForPromises();
}) })
.then(done) .then(() => {
.catch(done.fail); expect(eventHub.$emit).toHaveBeenCalledWith('refreshPipelinesTable');
}, 0); });
}, 0);
}); });
}); });
}); });
......
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