Commit 7e3680dd authored by Mike Greiling's avatar Mike Greiling

Merge branch 'migrate-loading-button-spec' into 'master'

Migrate loading button spec to jest

See merge request gitlab-org/gitlab!22027
parents c5d57bf1 805e1aee
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import LoadingButton from '~/vue_shared/components/loading_button.vue';
import loadingButton from '~/vue_shared/components/loading_button.vue';
const LABEL = 'Hello'; const LABEL = 'Hello';
describe('LoadingButton', function() { describe('LoadingButton', () => {
let vm; let wrapper;
let LoadingButton;
beforeEach(() => {
LoadingButton = Vue.extend(loadingButton);
});
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
}); });
const buildWrapper = (propsData = {}) => {
wrapper = shallowMount(LoadingButton, {
propsData,
});
};
const findButtonLabel = () => wrapper.find('.js-loading-button-label');
const findButtonIcon = () => wrapper.find('.js-loading-button-icon');
describe('loading spinner', () => { describe('loading spinner', () => {
it('shown when loading', () => { it('shown when loading', () => {
vm = mountComponent(LoadingButton, { buildWrapper({ loading: true });
loading: true,
});
expect(vm.$el.querySelector('.js-loading-button-icon')).toBeDefined(); expect(findButtonIcon().exists()).toBe(true);
}); });
}); });
describe('disabled state', () => { describe('disabled state', () => {
it('disabled when loading', () => { it('disabled when loading', () => {
vm = mountComponent(LoadingButton, { buildWrapper({ loading: true });
loading: true, expect(wrapper.attributes('disabled')).toBe('disabled');
});
expect(vm.$el.disabled).toEqual(true);
}); });
it('not disabled when normal', () => { it('not disabled when normal', () => {
vm = mountComponent(LoadingButton, { buildWrapper({ loading: false });
loading: false,
});
expect(vm.$el.disabled).toEqual(false); expect(wrapper.attributes('disabled')).toBe(undefined);
}); });
}); });
describe('label', () => { describe('label', () => {
it('shown when normal', () => { it('shown when normal', () => {
vm = mountComponent(LoadingButton, { buildWrapper({ loading: false, label: LABEL });
loading: false, expect(findButtonLabel().text()).toBe(LABEL);
label: LABEL,
});
const label = vm.$el.querySelector('.js-loading-button-label');
expect(label.textContent.trim()).toEqual(LABEL);
}); });
it('shown when loading', () => { it('shown when loading', () => {
vm = mountComponent(LoadingButton, { buildWrapper({ loading: false, label: LABEL });
loading: true, expect(findButtonLabel().text()).toBe(LABEL);
label: LABEL,
});
const label = vm.$el.querySelector('.js-loading-button-label');
expect(label.textContent.trim()).toEqual(LABEL);
}); });
}); });
describe('container class', () => { describe('container class', () => {
it('should default to btn btn-align-content', () => { it('should default to btn btn-align-content', () => {
vm = mountComponent(LoadingButton, {}); buildWrapper();
expect(vm.$el.classList.contains('btn')).toEqual(true); expect(wrapper.classes()).toContain('btn');
expect(vm.$el.classList.contains('btn-align-content')).toEqual(true); expect(wrapper.classes()).toContain('btn-align-content');
}); });
it('should be configurable through props', () => { it('should be configurable through props', () => {
vm = mountComponent(LoadingButton, { const containerClass = 'test-class';
containerClass: 'test-class',
buildWrapper({
containerClass,
}); });
expect(vm.$el.classList.contains('btn')).toEqual(false); expect(wrapper.classes()).not.toContain('btn');
expect(vm.$el.classList.contains('btn-align-content')).toEqual(false); expect(wrapper.classes()).not.toContain('btn-align-content');
expect(vm.$el.classList.contains('test-class')).toEqual(true); expect(wrapper.classes()).toContain(containerClass);
}); });
}); });
describe('click callback prop', () => { describe('click callback prop', () => {
it('calls given callback when normal', () => { it('calls given callback when normal', () => {
vm = mountComponent(LoadingButton, { buildWrapper({
loading: false, loading: false,
}); });
spyOn(vm, '$emit');
vm.$el.click(); wrapper.trigger('click');
expect(vm.$emit).toHaveBeenCalledWith('click', jasmine.any(Object)); expect(wrapper.emitted('click')).toBeTruthy();
}); });
it('does not call given callback when disabled because of loading', () => { it('does not call given callback when disabled because of loading', () => {
vm = mountComponent(LoadingButton, { buildWrapper({
loading: true, loading: true,
}); });
spyOn(vm, '$emit');
vm.$el.click(); wrapper.trigger('click');
expect(vm.$emit).not.toHaveBeenCalled(); expect(wrapper.emitted('click')).toBeFalsy();
}); });
}); });
}); });
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