Commit e3e58f1d authored by Paul Slaughter's avatar Paul Slaughter

Migrate toggle_buttons spec to Jest

- Use waitForPromises instead of
  getSetTimeoutPromise
parent 51272874
import $ from 'jquery'; import $ from 'jquery';
import setupToggleButtons from '~/toggle_buttons'; import setupToggleButtons from '~/toggle_buttons';
import getSetTimeoutPromise from './helpers/set_timeout_promise_helper'; import waitForPromises from './helpers/wait_for_promises';
function generateMarkup(isChecked = true) { function generateMarkup(isChecked = true) {
return ` return `
...@@ -31,19 +31,16 @@ describe('ToggleButtons', () => { ...@@ -31,19 +31,16 @@ describe('ToggleButtons', () => {
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true'); expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true');
}); });
it('should toggle to unchecked when clicked', done => { it('should toggle to unchecked when clicked', () => {
const wrapper = setupFixture(true); const wrapper = setupFixture(true);
const toggleButton = wrapper.querySelector('.js-project-feature-toggle'); const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
toggleButton.click(); toggleButton.click();
getSetTimeoutPromise() return waitForPromises().then(() => {
.then(() => { expect(toggleButton.classList.contains('is-checked')).toEqual(false);
expect(toggleButton.classList.contains('is-checked')).toEqual(false); expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false');
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false'); });
})
.then(done)
.catch(done.fail);
}); });
}); });
...@@ -58,24 +55,21 @@ describe('ToggleButtons', () => { ...@@ -58,24 +55,21 @@ describe('ToggleButtons', () => {
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false'); expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false');
}); });
it('should toggle to checked when clicked', done => { it('should toggle to checked when clicked', () => {
const wrapper = setupFixture(false); const wrapper = setupFixture(false);
const toggleButton = wrapper.querySelector('.js-project-feature-toggle'); const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
toggleButton.click(); toggleButton.click();
getSetTimeoutPromise() return waitForPromises().then(() => {
.then(() => { expect(toggleButton.classList.contains('is-checked')).toEqual(true);
expect(toggleButton.classList.contains('is-checked')).toEqual(true); expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true');
expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true'); });
})
.then(done)
.catch(done.fail);
}); });
}); });
it('should emit `trigger-change` event', done => { it('should emit `trigger-change` event', () => {
const changeSpy = jasmine.createSpy('changeEventHandler'); const changeSpy = jest.fn();
const wrapper = setupFixture(false); const wrapper = setupFixture(false);
const toggleButton = wrapper.querySelector('.js-project-feature-toggle'); const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
const input = wrapper.querySelector('.js-project-feature-toggle-input'); const input = wrapper.querySelector('.js-project-feature-toggle-input');
...@@ -84,16 +78,13 @@ describe('ToggleButtons', () => { ...@@ -84,16 +78,13 @@ describe('ToggleButtons', () => {
toggleButton.click(); toggleButton.click();
getSetTimeoutPromise() return waitForPromises().then(() => {
.then(() => { expect(changeSpy).toHaveBeenCalled();
expect(changeSpy).toHaveBeenCalled(); });
})
.then(done)
.catch(done.fail);
}); });
describe('clickCallback', () => { describe('clickCallback', () => {
it('should show loading indicator while waiting', done => { it('should show loading indicator while waiting', () => {
const isChecked = true; const isChecked = true;
const clickCallback = (newValue, toggleButton) => { const clickCallback = (newValue, toggleButton) => {
const input = toggleButton.querySelector('.js-project-feature-toggle-input'); const input = toggleButton.querySelector('.js-project-feature-toggle-input');
...@@ -107,15 +98,12 @@ describe('ToggleButtons', () => { ...@@ -107,15 +98,12 @@ describe('ToggleButtons', () => {
expect(input.value).toEqual('true'); expect(input.value).toEqual('true');
// After the callback finishes, check that the loading state is gone // After the callback finishes, check that the loading state is gone
getSetTimeoutPromise() return waitForPromises().then(() => {
.then(() => { expect(toggleButton.classList.contains('is-checked')).toEqual(false);
expect(toggleButton.classList.contains('is-checked')).toEqual(false); expect(toggleButton.classList.contains('is-loading')).toEqual(false);
expect(toggleButton.classList.contains('is-loading')).toEqual(false); expect(toggleButton.disabled).toEqual(false);
expect(toggleButton.disabled).toEqual(false); expect(input.value).toEqual('false');
expect(input.value).toEqual('false'); });
})
.then(done)
.catch(done.fail);
}; };
const wrapper = setupFixture(isChecked, clickCallback); const wrapper = setupFixture(isChecked, clickCallback);
......
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