Commit ec59541e authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'migrate-pipelines-actions-spec' into 'master'

Migrate pipelines actions spec to Jest

See merge request gitlab-org/gitlab!31113
parents 2667a777 39bef72c
import Vue from 'vue'; import { shallowMount } 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 { TEST_HOST } from 'spec/test_constants'; import { TEST_HOST } from 'spec/test_constants';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import PipelinesActions from '~/pipelines/components/pipelines_actions.vue'; import PipelinesActions from '~/pipelines/components/pipelines_actions.vue';
import { GlDeprecatedButton } from '@gitlab/ui';
import GlCountdown from '~/vue_shared/components/gl_countdown.vue';
import waitForPromises from 'helpers/wait_for_promises';
describe('Pipelines Actions dropdown', () => { describe('Pipelines Actions dropdown', () => {
const Component = Vue.extend(PipelinesActions); let wrapper;
let vm;
let mock; let mock;
afterEach(() => { const createComponent = (actions = []) => {
vm.$destroy(); wrapper = shallowMount(PipelinesActions, {
mock.restore(); propsData: {
}); actions,
},
});
};
const findAllDropdownItems = () => wrapper.findAll(GlDeprecatedButton);
const findAllCountdowns = () => wrapper.findAll(GlCountdown);
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
}); });
afterEach(() => {
wrapper.destroy();
wrapper = null;
mock.restore();
});
describe('manual actions', () => { describe('manual actions', () => {
const actions = [ const mockActions = [
{ {
name: 'stop_review', name: 'stop_review',
path: `${TEST_HOST}/root/review-app/builds/1893/play`, path: `${TEST_HOST}/root/review-app/builds/1893/play`,
...@@ -33,33 +47,31 @@ describe('Pipelines Actions dropdown', () => { ...@@ -33,33 +47,31 @@ describe('Pipelines Actions dropdown', () => {
]; ];
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { actions }); createComponent(mockActions);
}); });
it('renders a dropdown with the provided actions', () => { it('renders a dropdown with the provided actions', () => {
const dropdownItems = vm.$el.querySelectorAll('.dropdown-menu li'); expect(findAllDropdownItems()).toHaveLength(mockActions.length);
expect(dropdownItems.length).toEqual(actions.length);
}); });
it("renders a disabled action when it's not playable", () => { it("renders a disabled action when it's not playable", () => {
const dropdownItem = vm.$el.querySelector('.dropdown-menu li:last-child button'); expect(
findAllDropdownItems()
expect(dropdownItem).toBeDisabled(); .at(1)
.attributes('disabled'),
).toBe('true');
}); });
describe('on click', () => { describe('on click', () => {
it('makes a request and toggles the loading state', done => { it('makes a request and toggles the loading state', () => {
mock.onPost(actions.path).reply(200); mock.onPost(mockActions.path).reply(200);
vm.$el.querySelector('.dropdown-menu li button').click();
expect(vm.isLoading).toEqual(true); wrapper.find(GlDeprecatedButton).vm.$emit('click');
setTimeout(() => { expect(wrapper.vm.isLoading).toBe(true);
expect(vm.isLoading).toEqual(false);
done(); return waitForPromises().then(() => {
expect(wrapper.vm.isLoading).toBe(false);
}); });
}); });
}); });
...@@ -78,51 +90,53 @@ describe('Pipelines Actions dropdown', () => { ...@@ -78,51 +90,53 @@ describe('Pipelines Actions dropdown', () => {
playable: true, playable: true,
scheduled_at: '2018-10-05T08:23:00Z', scheduled_at: '2018-10-05T08:23:00Z',
}; };
const findDropdownItem = action => {
const buttons = vm.$el.querySelectorAll('.dropdown-menu li button');
return Array.prototype.find.call(buttons, element =>
element.innerText.trim().startsWith(action.name),
);
};
beforeEach(done => {
spyOn(Date, 'now').and.callFake(() => new Date('2063-04-04T00:42:00Z').getTime());
vm = mountComponent(Component, { actions: [scheduledJobAction, expiredJobAction] });
Vue.nextTick() beforeEach(() => {
.then(done) jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
.catch(done.fail); createComponent([scheduledJobAction, expiredJobAction]);
}); });
it('makes post request after confirming', done => { it('makes post request after confirming', () => {
mock.onPost(scheduledJobAction.path).reply(200); mock.onPost(scheduledJobAction.path).reply(200);
spyOn(window, 'confirm').and.callFake(() => true); jest.spyOn(window, 'confirm').mockReturnValue(true);
findDropdownItem(scheduledJobAction).click(); findAllDropdownItems()
.at(0)
.vm.$emit('click');
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
setTimeout(() => {
return waitForPromises().then(() => {
expect(mock.history.post.length).toBe(1); expect(mock.history.post.length).toBe(1);
done();
}); });
}); });
it('does not make post request if confirmation is cancelled', () => { it('does not make post request if confirmation is cancelled', () => {
mock.onPost(scheduledJobAction.path).reply(200); mock.onPost(scheduledJobAction.path).reply(200);
spyOn(window, 'confirm').and.callFake(() => false); jest.spyOn(window, 'confirm').mockReturnValue(false);
findDropdownItem(scheduledJobAction).click(); findAllDropdownItems()
.at(0)
.vm.$emit('click');
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(mock.history.post.length).toBe(0); expect(mock.history.post.length).toBe(0);
}); });
it('displays the remaining time in the dropdown', () => { it('displays the remaining time in the dropdown', () => {
expect(findDropdownItem(scheduledJobAction)).toContainText('24:00:00'); expect(
findAllCountdowns()
.at(0)
.props('endDateString'),
).toBe(scheduledJobAction.scheduled_at);
}); });
it('displays 00:00:00 for expired jobs in the dropdown', () => { it('displays 00:00:00 for expired jobs in the dropdown', () => {
expect(findDropdownItem(expiredJobAction)).toContainText('00:00:00'); expect(
findAllCountdowns()
.at(1)
.props('endDateString'),
).toBe(expiredJobAction.scheduled_at);
}); });
}); });
}); });
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