Commit 55b92a6b authored by Vitaly Slobodin's avatar Vitaly Slobodin

Merge branch 'refactor-toggle-button-spec' into 'master'

Refactor spec `spec/frontend/vue_shared/components/toggle_button_spec.js`

See merge request gitlab-org/gitlab!38544
parents 6a3855f1 8d817429
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'helpers/vue_mount_component_helper'; import { GlIcon } from '@gitlab/ui';
import toggleButton from '~/vue_shared/components/toggle_button.vue'; import ToggleButton from '~/vue_shared/components/toggle_button.vue';
describe('Toggle Button', () => { describe('Toggle Button component', () => {
let vm; let wrapper;
let Component;
beforeEach(() => { function createComponent(propsData = {}) {
Component = Vue.extend(toggleButton); wrapper = shallowMount(ToggleButton, {
}); propsData,
});
}
const findInput = () => wrapper.find('input');
const findButton = () => wrapper.find('button');
const findToggleIcon = () => wrapper.find(GlIcon);
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
wrapper = null;
}); });
describe('render output', () => { it('renders input with provided name', () => {
beforeEach(() => { createComponent({
vm = mountComponent(Component, { name: 'foo',
value: true,
name: 'foo',
});
});
it('renders input with provided name', () => {
expect(vm.$el.querySelector('input').getAttribute('name')).toEqual('foo');
}); });
it('renders input with provided value', () => { expect(findInput().attributes('name')).toBe('foo');
expect(vm.$el.querySelector('input').getAttribute('value')).toEqual('true');
});
it('renders input status icon', () => {
expect(vm.$el.querySelectorAll('span.toggle-icon').length).toEqual(1);
expect(vm.$el.querySelectorAll('svg.s18').length).toEqual(1);
});
}); });
describe('is-checked', () => { describe.each`
value | iconName
${true} | ${'status_success_borderless'}
${false} | ${'status_failed_borderless'}
`('when `value` prop is `$value`', ({ value, iconName }) => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
value: true, value,
name: 'foo',
}); });
jest.spyOn(vm, '$emit').mockImplementation(() => {});
}); });
it('renders is checked class', () => { it('renders input with correct value attribute', () => {
expect(vm.$el.querySelector('button').classList.contains('is-checked')).toEqual(true); expect(findInput().attributes('value')).toBe(`${value}`);
}); });
it('sets aria-label representing toggle state', () => { it('renders correct icon', () => {
vm.value = true; const icon = findToggleIcon();
expect(icon.isVisible()).toBe(true);
expect(vm.ariaLabel).toEqual('Toggle Status: ON'); expect(icon.props('name')).toBe(iconName);
expect(findButton().classes('is-checked')).toBe(value);
vm.value = false;
expect(vm.ariaLabel).toEqual('Toggle Status: OFF');
}); });
it('emits change event when clicked', () => { describe('when clicked', () => {
vm.$el.querySelector('button').click(); it('emits `change` event with correct event', async () => {
findButton().trigger('click');
await wrapper.vm.$nextTick();
expect(vm.$emit).toHaveBeenCalledWith('change', false); expect(wrapper.emitted('change')).toStrictEqual([[!value]]);
});
}); });
}); });
describe('is-disabled', () => { describe('when `disabledInput` prop is `true`', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
value: true, value: true,
disabledInput: true, disabledInput: true,
}); });
jest.spyOn(vm, '$emit').mockImplementation(() => {});
}); });
it('renders disabled button', () => { it('renders disabled button', () => {
expect(vm.$el.querySelector('button').classList.contains('is-disabled')).toEqual(true); expect(findButton().classes()).toContain('is-disabled');
}); });
it('does not emit change event when clicked', () => { it('does not emit change event when clicked', async () => {
vm.$el.querySelector('button').click(); findButton().trigger('click');
await wrapper.vm.$nextTick();
expect(vm.$emit).not.toHaveBeenCalled(); expect(wrapper.emitted('change')).toBeFalsy();
}); });
}); });
describe('is-loading', () => { describe('when `isLoading` prop is `true`', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
value: true, value: true,
isLoading: true, isLoading: true,
}); });
}); });
it('renders loading class', () => { it('renders loading class', () => {
expect(vm.$el.querySelector('button').classList.contains('is-loading')).toEqual(true); expect(findButton().classes()).toContain('is-loading');
}); });
}); });
}); });
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