Commit 271d1ea9 authored by Emily Ring's avatar Emily Ring Committed by Phil Hughes

Updated test with @vue/test-utils

Moved first three tests to use @vue/test-utils since there
have been issues with helpers/vue_mount_component_helper
parent bd3f5d88
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import { TEST_HOST } from 'helpers/test_constants';
import { TEST_HOST } from 'spec/test_constants';
import eventHub from '~/environments/event_hub'; import eventHub from '~/environments/event_hub';
import EnvironmentActions from '~/environments/components/environment_actions.vue'; import EnvironmentActions from '~/environments/components/environment_actions.vue';
import Icon from '~/vue_shared/components/icon.vue';
import { GlLoadingIcon } from '@gitlab/ui';
describe('EnvironmentActions Component', () => { describe('EnvironmentActions Component', () => {
const Component = Vue.extend(EnvironmentActions);
let vm; let vm;
beforeEach(() => {
vm = shallowMount(EnvironmentActions, { propsData: { actions: [] } });
});
afterEach(() => { afterEach(() => {
vm.$destroy(); vm.destroy();
});
it('should render a dropdown button with 2 icons', () => {
expect(vm.find('.dropdown-new').findAll(Icon).length).toBe(2);
});
it('should render a dropdown button with aria-label description', () => {
expect(vm.find('.dropdown-new').attributes('aria-label')).toEqual('Deploy to...');
});
describe('is loading', () => {
beforeEach(() => {
vm.setData({ isLoading: true });
});
it('should render a dropdown button with a loading icon', () => {
expect(vm.findAll(GlLoadingIcon).length).toBe(1);
});
}); });
describe('manual actions', () => { describe('manual actions', () => {
...@@ -30,32 +52,19 @@ describe('EnvironmentActions Component', () => { ...@@ -30,32 +52,19 @@ describe('EnvironmentActions Component', () => {
]; ];
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { actions }); vm.setProps({ actions });
});
it('should render a dropdown button with icon and title attribute', () => {
expect(vm.$el.querySelector('.fa-caret-down')).toBeDefined();
expect(vm.$el.querySelector('.dropdown-new').getAttribute('data-original-title')).toEqual(
'Deploy to...',
);
expect(vm.$el.querySelector('.dropdown-new').getAttribute('aria-label')).toEqual(
'Deploy to...',
);
}); });
it('should render a dropdown with the provided list of actions', () => { it('should render a dropdown with the provided list of actions', () => {
expect(vm.$el.querySelectorAll('.dropdown-menu li').length).toEqual(actions.length); expect(vm.findAll('.dropdown-menu li').length).toEqual(actions.length);
}); });
it("should render a disabled action when it's not playable", () => { it("should render a disabled action when it's not playable", () => {
expect( expect(vm.find('.dropdown-menu li:last-child button').attributes('disabled')).toEqual(
vm.$el.querySelector('.dropdown-menu li:last-child button').getAttribute('disabled'), 'disabled',
).toEqual('disabled'); );
expect( expect(vm.find('.dropdown-menu li:last-child button').classes('disabled')).toBe(true);
vm.$el.querySelector('.dropdown-menu li:last-child button').classList.contains('disabled'),
).toEqual(true);
}); });
}); });
...@@ -73,45 +82,43 @@ describe('EnvironmentActions Component', () => { ...@@ -73,45 +82,43 @@ describe('EnvironmentActions Component', () => {
scheduledAt: '2018-10-05T08:23:00Z', scheduledAt: '2018-10-05T08:23:00Z',
}; };
const findDropdownItem = action => { const findDropdownItem = action => {
const buttons = vm.$el.querySelectorAll('.dropdown-menu li button'); const buttons = vm.findAll('.dropdown-menu li button');
return Array.prototype.find.call(buttons, element => return buttons.filter(button => button.text().startsWith(action.name)).at(0);
element.innerText.trim().startsWith(action.name),
);
}; };
beforeEach(() => { beforeEach(() => {
spyOn(Date, 'now').and.callFake(() => new Date('2063-04-04T00:42:00Z').getTime()); jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
vm = mountComponent(Component, { actions: [scheduledJobAction, expiredJobAction] }); vm.setProps({ actions: [scheduledJobAction, expiredJobAction] });
}); });
it('emits postAction event after confirming', () => { it('emits postAction event after confirming', () => {
const emitSpy = jasmine.createSpy('emit'); const emitSpy = jest.fn();
eventHub.$on('postAction', emitSpy); eventHub.$on('postAction', emitSpy);
spyOn(window, 'confirm').and.callFake(() => true); jest.spyOn(window, 'confirm').mockImplementation(() => true);
findDropdownItem(scheduledJobAction).click(); findDropdownItem(scheduledJobAction).trigger('click');
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(emitSpy).toHaveBeenCalledWith({ endpoint: scheduledJobAction.playPath }); expect(emitSpy).toHaveBeenCalledWith({ endpoint: scheduledJobAction.playPath });
}); });
it('does not emit postAction event if confirmation is cancelled', () => { it('does not emit postAction event if confirmation is cancelled', () => {
const emitSpy = jasmine.createSpy('emit'); const emitSpy = jest.fn();
eventHub.$on('postAction', emitSpy); eventHub.$on('postAction', emitSpy);
spyOn(window, 'confirm').and.callFake(() => false); jest.spyOn(window, 'confirm').mockImplementation(() => false);
findDropdownItem(scheduledJobAction).click(); findDropdownItem(scheduledJobAction).trigger('click');
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(emitSpy).not.toHaveBeenCalled(); expect(emitSpy).not.toHaveBeenCalled();
}); });
it('displays the remaining time in the dropdown', () => { it('displays the remaining time in the dropdown', () => {
expect(findDropdownItem(scheduledJobAction)).toContainText('24:00:00'); expect(findDropdownItem(scheduledJobAction).text()).toContain('24:00:00');
}); });
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(findDropdownItem(expiredJobAction).text()).toContain('00:00:00');
}); });
}); });
}); });
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