2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-01-24 16:54:33 -05:00
|
|
|
import setupToggleButtons from '~/toggle_buttons';
|
2020-05-22 14:08:21 -04:00
|
|
|
import waitForPromises from './helpers/wait_for_promises';
|
2018-01-24 16:54:33 -05:00
|
|
|
|
|
|
|
function generateMarkup(isChecked = true) {
|
|
|
|
return `
|
|
|
|
<button type="button" class="${isChecked ? 'is-checked' : ''} js-project-feature-toggle">
|
|
|
|
<input type="hidden" class="js-project-feature-toggle-input" value="${isChecked}" />
|
|
|
|
</button>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupFixture(isChecked, clickCallback) {
|
|
|
|
const wrapper = document.createElement('div');
|
|
|
|
wrapper.innerHTML = generateMarkup(isChecked);
|
|
|
|
|
|
|
|
setupToggleButtons(wrapper, clickCallback);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('ToggleButtons', () => {
|
|
|
|
describe('when input value is true', () => {
|
|
|
|
it('should initialize as checked', () => {
|
|
|
|
const wrapper = setupFixture(true);
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
expect(
|
|
|
|
wrapper.querySelector('.js-project-feature-toggle').classList.contains('is-checked'),
|
|
|
|
).toEqual(true);
|
2018-10-17 03:21:28 -04:00
|
|
|
|
2018-01-24 16:54:33 -05:00
|
|
|
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true');
|
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should toggle to unchecked when clicked', () => {
|
2018-01-24 16:54:33 -05:00
|
|
|
const wrapper = setupFixture(true);
|
|
|
|
const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
|
|
|
|
|
|
|
|
toggleButton.click();
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(toggleButton.classList.contains('is-checked')).toEqual(false);
|
|
|
|
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false');
|
|
|
|
});
|
2018-01-24 16:54:33 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when input value is false', () => {
|
|
|
|
it('should initialize as unchecked', () => {
|
|
|
|
const wrapper = setupFixture(false);
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
expect(
|
|
|
|
wrapper.querySelector('.js-project-feature-toggle').classList.contains('is-checked'),
|
|
|
|
).toEqual(false);
|
2018-10-17 03:21:28 -04:00
|
|
|
|
2018-01-24 16:54:33 -05:00
|
|
|
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false');
|
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should toggle to checked when clicked', () => {
|
2018-01-24 16:54:33 -05:00
|
|
|
const wrapper = setupFixture(false);
|
|
|
|
const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
|
|
|
|
|
|
|
|
toggleButton.click();
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(toggleButton.classList.contains('is-checked')).toEqual(true);
|
|
|
|
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true');
|
|
|
|
});
|
2018-01-24 16:54:33 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should emit `trigger-change` event', () => {
|
|
|
|
const changeSpy = jest.fn();
|
2018-01-24 16:54:33 -05:00
|
|
|
const wrapper = setupFixture(false);
|
|
|
|
const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
|
|
|
|
const input = wrapper.querySelector('.js-project-feature-toggle-input');
|
|
|
|
|
|
|
|
$(input).on('trigger-change', changeSpy);
|
|
|
|
|
|
|
|
toggleButton.click();
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(changeSpy).toHaveBeenCalled();
|
|
|
|
});
|
2018-01-24 16:54:33 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('clickCallback', () => {
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should show loading indicator while waiting', () => {
|
2018-01-24 16:54:33 -05:00
|
|
|
const isChecked = true;
|
|
|
|
const clickCallback = (newValue, toggleButton) => {
|
|
|
|
const input = toggleButton.querySelector('.js-project-feature-toggle-input');
|
|
|
|
|
|
|
|
expect(newValue).toEqual(false);
|
|
|
|
|
|
|
|
// Check for the loading state
|
|
|
|
expect(toggleButton.classList.contains('is-checked')).toEqual(false);
|
|
|
|
expect(toggleButton.classList.contains('is-loading')).toEqual(true);
|
|
|
|
expect(toggleButton.disabled).toEqual(true);
|
|
|
|
expect(input.value).toEqual('true');
|
|
|
|
|
|
|
|
// After the callback finishes, check that the loading state is gone
|
2020-05-22 14:08:21 -04:00
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(toggleButton.classList.contains('is-checked')).toEqual(false);
|
|
|
|
expect(toggleButton.classList.contains('is-loading')).toEqual(false);
|
|
|
|
expect(toggleButton.disabled).toEqual(false);
|
|
|
|
expect(input.value).toEqual('false');
|
|
|
|
});
|
2018-01-24 16:54:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = setupFixture(isChecked, clickCallback);
|
|
|
|
const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
|
|
|
|
|
|
|
|
toggleButton.click();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|