2021-02-22 17:27:13 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import { GlIcon } from '@gitlab/ui';
|
|
|
|
import ToggleButton from '~/vue_shared/components/toggle_button.vue';
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe('Toggle Button component', () => {
|
|
|
|
let wrapper;
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
function createComponent(propsData = {}) {
|
|
|
|
wrapper = shallowMount(ToggleButton, {
|
|
|
|
propsData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const findInput = () => wrapper.find('input');
|
|
|
|
const findButton = () => wrapper.find('button');
|
|
|
|
const findToggleIcon = () => wrapper.find(GlIcon);
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
afterEach(() => {
|
2021-02-22 17:27:13 +05:30
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it('renders input with provided name', () => {
|
|
|
|
createComponent({
|
|
|
|
name: 'foo',
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(findInput().attributes('name')).toBe('foo');
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe.each`
|
|
|
|
value | iconName
|
|
|
|
${true} | ${'status_success_borderless'}
|
|
|
|
${false} | ${'status_failed_borderless'}
|
|
|
|
`('when `value` prop is `$value`', ({ value, iconName }) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
beforeEach(() => {
|
2021-02-22 17:27:13 +05:30
|
|
|
createComponent({
|
|
|
|
value,
|
|
|
|
name: 'foo',
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it('renders input with correct value attribute', () => {
|
|
|
|
expect(findInput().attributes('value')).toBe(`${value}`);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it('renders correct icon', () => {
|
|
|
|
const icon = findToggleIcon();
|
|
|
|
expect(icon.isVisible()).toBe(true);
|
|
|
|
expect(icon.props('name')).toBe(iconName);
|
|
|
|
expect(findButton().classes('is-checked')).toBe(value);
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe('when clicked', () => {
|
|
|
|
it('emits `change` event with correct event', async () => {
|
|
|
|
findButton().trigger('click');
|
|
|
|
await wrapper.vm.$nextTick();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(wrapper.emitted('change')).toStrictEqual([[!value]]);
|
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe('when `disabledInput` prop is `true`', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
beforeEach(() => {
|
2021-02-22 17:27:13 +05:30
|
|
|
createComponent({
|
2018-03-17 18:26:18 +05:30
|
|
|
value: true,
|
|
|
|
disabledInput: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders disabled button', () => {
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(findButton().classes()).toContain('is-disabled');
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it('does not emit change event when clicked', async () => {
|
|
|
|
findButton().trigger('click');
|
|
|
|
await wrapper.vm.$nextTick();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(wrapper.emitted('change')).toBeFalsy();
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe('when `isLoading` prop is `true`', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
beforeEach(() => {
|
2021-02-22 17:27:13 +05:30
|
|
|
createComponent({
|
2018-03-17 18:26:18 +05:30
|
|
|
value: true,
|
|
|
|
isLoading: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders loading class', () => {
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(findButton().classes()).toContain('is-loading');
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|