Commit 90b9f2eb authored by Paul Slaughter's avatar Paul Slaughter

Clean up some things in awards_handler_spec

- Use async/await
- Mock request_animation_frame_fallback for performance
- Move openAndWaitForEmojiMenu to its own helper function
parent 8d275e6d
...@@ -3,64 +3,48 @@ import Cookies from 'js-cookie'; ...@@ -3,64 +3,48 @@ import Cookies from 'js-cookie';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import loadAwardsHandler from '~/awards_handler'; import loadAwardsHandler from '~/awards_handler';
import '~/lib/utils/common_utils'; import { setTestTimeout } from './helpers/timeout';
import waitForPromises from './helpers/wait_for_promises';
import { EMOJI_VERSION } from '~/emoji'; import { EMOJI_VERSION } from '~/emoji';
window.gl = window.gl || {}; window.gl = window.gl || {};
window.gon = window.gon || {}; window.gon = window.gon || {};
let openAndWaitForEmojiMenu;
let mock; let mock;
let awardsHandler = null; let awardsHandler = null;
const urlRoot = gon.relative_url_root; const urlRoot = gon.relative_url_root;
const lazyAssert = (done, assertFn) => { jest.mock('~/lib/utils/request_animation_frame_fallback', () => cb => cb());
jest.runOnlyPendingTimers();
waitForPromises()
.then(() => {
assertFn();
done();
})
.catch(e => {
throw e;
});
};
describe('AwardsHandler', () => { describe('AwardsHandler', () => {
const emojiData = getJSONFixture('emojis/emojis.json'); const emojiData = getJSONFixture('emojis/emojis.json');
preloadFixtures('snippets/show.html'); preloadFixtures('snippets/show.html');
beforeEach(done => { const openAndWaitForEmojiMenu = (sel = '.js-add-award') => {
mock = new MockAdapter(axios); $(sel)
mock.onGet(`/-/emojis/${EMOJI_VERSION}/emojis.json`).reply(200, emojiData);
loadFixtures('snippets/show.html');
loadAwardsHandler(true)
.then(obj => {
awardsHandler = obj;
jest.spyOn(awardsHandler, 'postEmoji').mockImplementation((button, url, emoji, cb) => cb());
done();
})
.catch(done.fail);
let isEmojiMenuBuilt = false;
openAndWaitForEmojiMenu = () => {
return new Promise(resolve => {
if (isEmojiMenuBuilt) {
resolve();
} else {
$('.js-add-award')
.eq(0) .eq(0)
.click(); .click();
jest.advanceTimersByTime(200);
const $menu = $('.emoji-menu'); const $menu = $('.emoji-menu');
return new Promise(resolve => {
$menu.one('build-emoji-menu-finish', () => { $menu.one('build-emoji-menu-finish', () => {
isEmojiMenuBuilt = true;
resolve(); resolve();
}); });
}
}); });
}; };
beforeEach(async () => {
setTestTimeout(3000);
mock = new MockAdapter(axios);
mock.onGet(`/-/emojis/${EMOJI_VERSION}/emojis.json`).reply(200, emojiData);
loadFixtures('snippets/show.html');
awardsHandler = await loadAwardsHandler(true);
jest.spyOn(awardsHandler, 'postEmoji').mockImplementation((button, url, emoji, cb) => cb());
}); });
afterEach(() => { afterEach(() => {
...@@ -76,11 +60,9 @@ describe('AwardsHandler', () => { ...@@ -76,11 +60,9 @@ describe('AwardsHandler', () => {
}); });
describe('::showEmojiMenu', () => { describe('::showEmojiMenu', () => {
it('should show emoji menu when Add emoji button clicked', done => { it('should show emoji menu when Add emoji button clicked', async () => {
$('.js-add-award') await openAndWaitForEmojiMenu();
.eq(0)
.click();
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
...@@ -88,22 +70,18 @@ describe('AwardsHandler', () => { ...@@ -88,22 +70,18 @@ describe('AwardsHandler', () => {
expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1); expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1);
expect($('.js-awards-block.current').length).toBe(1); expect($('.js-awards-block.current').length).toBe(1);
}); });
});
it('should also show emoji menu for the smiley icon in notes', done => { it('should also show emoji menu for the smiley icon in notes', async () => {
$('.js-add-award.note-action-button').click(); await openAndWaitForEmojiMenu('.js-add-award.note-action-button');
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
}); });
});
it('should remove emoji menu when body is clicked', done => { it('should remove emoji menu when body is clicked', async () => {
$('.js-add-award') await openAndWaitForEmojiMenu();
.eq(0)
.click();
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
$('body').click(); $('body').click();
...@@ -111,13 +89,10 @@ describe('AwardsHandler', () => { ...@@ -111,13 +89,10 @@ describe('AwardsHandler', () => {
expect($emojiMenu.hasClass('is-visible')).toBe(false); expect($emojiMenu.hasClass('is-visible')).toBe(false);
expect($('.js-awards-block.current').length).toBe(0); expect($('.js-awards-block.current').length).toBe(0);
}); });
});
it('should not remove emoji menu when search is clicked', done => { it('should not remove emoji menu when search is clicked', async () => {
$('.js-add-award') await openAndWaitForEmojiMenu();
.eq(0)
.click();
lazyAssert(done, () => {
const $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
$('.emoji-search').click(); $('.emoji-search').click();
...@@ -126,7 +101,6 @@ describe('AwardsHandler', () => { ...@@ -126,7 +101,6 @@ describe('AwardsHandler', () => {
expect($('.js-awards-block.current').length).toBe(1); expect($('.js-awards-block.current').length).toBe(1);
}); });
}); });
});
describe('::addAwardToEmojiBar', () => { describe('::addAwardToEmojiBar', () => {
it('should add emoji to votes block', () => { it('should add emoji to votes block', () => {
...@@ -272,9 +246,9 @@ describe('AwardsHandler', () => { ...@@ -272,9 +246,9 @@ describe('AwardsHandler', () => {
}); });
describe('::searchEmojis', () => { describe('::searchEmojis', () => {
it('should filter the emoji', done => { it('should filter the emoji', async () => {
openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
expect($('[data-name=angel]').is(':visible')).toBe(true); expect($('[data-name=angel]').is(':visible')).toBe(true);
expect($('[data-name=anger]').is(':visible')).toBe(true); expect($('[data-name=anger]').is(':visible')).toBe(true);
awardsHandler.searchEmojis('ali'); awardsHandler.searchEmojis('ali');
...@@ -283,16 +257,11 @@ describe('AwardsHandler', () => { ...@@ -283,16 +257,11 @@ describe('AwardsHandler', () => {
expect($('[data-name=anger]').is(':visible')).toBe(false); expect($('[data-name=anger]').is(':visible')).toBe(false);
expect($('[data-name=alien]').is(':visible')).toBe(true); expect($('[data-name=alien]').is(':visible')).toBe(true);
expect($('.js-emoji-menu-search').val()).toBe('ali'); expect($('.js-emoji-menu-search').val()).toBe('ali');
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should clear the search when searching for nothing', done => { it('should clear the search when searching for nothing', async () => {
openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
awardsHandler.searchEmojis('ali'); awardsHandler.searchEmojis('ali');
expect($('[data-name=angel]').is(':visible')).toBe(false); expect($('[data-name=angel]').is(':visible')).toBe(false);
...@@ -304,16 +273,12 @@ describe('AwardsHandler', () => { ...@@ -304,16 +273,12 @@ describe('AwardsHandler', () => {
expect($('[data-name=anger]').is(':visible')).toBe(true); expect($('[data-name=anger]').is(':visible')).toBe(true);
expect($('[data-name=alien]').is(':visible')).toBe(true); expect($('[data-name=alien]').is(':visible')).toBe(true);
expect($('.js-emoji-menu-search').val()).toBe(''); expect($('.js-emoji-menu-search').val()).toBe('');
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
}); });
describe('emoji menu', () => { describe('emoji menu', () => {
const emojiSelector = '[data-name="sunglasses"]'; const emojiSelector = '[data-name="sunglasses"]';
const openEmojiMenuAndAddEmoji = () => { const openEmojiMenuAndAddEmoji = () => {
return openAndWaitForEmojiMenu().then(() => { return openAndWaitForEmojiMenu().then(() => {
const $menu = $('.emoji-menu'); const $menu = $('.emoji-menu');
...@@ -329,17 +294,13 @@ describe('AwardsHandler', () => { ...@@ -329,17 +294,13 @@ describe('AwardsHandler', () => {
}); });
}; };
it('should add selected emoji to awards block', done => { it('should add selected emoji to awards block', async () => {
openEmojiMenuAndAddEmoji() await openEmojiMenuAndAddEmoji();
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should remove already selected emoji', done => { it('should remove already selected emoji', async () => {
openEmojiMenuAndAddEmoji() await openEmojiMenuAndAddEmoji();
.then(() => {
$('.js-add-award') $('.js-add-award')
.eq(0) .eq(0)
.click(); .click();
...@@ -350,11 +311,6 @@ describe('AwardsHandler', () => { ...@@ -350,11 +311,6 @@ describe('AwardsHandler', () => {
$emoji.click(); $emoji.click();
expect($block.find(emojiSelector).length).toBe(0); expect($block.find(emojiSelector).length).toBe(0);
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
}); });
...@@ -364,25 +320,20 @@ describe('AwardsHandler', () => { ...@@ -364,25 +320,20 @@ describe('AwardsHandler', () => {
Cookies.set('frequently_used_emojis', ''); Cookies.set('frequently_used_emojis', '');
}); });
it('shouldn\'t have any "Frequently used" heading if no frequently used emojis', done => { it('shouldn\'t have any "Frequently used" heading if no frequently used emojis', async () => {
return openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
const emojiMenu = document.querySelector('.emoji-menu'); const emojiMenu = document.querySelector('.emoji-menu');
Array.prototype.forEach.call(emojiMenu.querySelectorAll('.emoji-menu-title'), title => { Array.prototype.forEach.call(emojiMenu.querySelectorAll('.emoji-menu-title'), title => {
expect(title.textContent.trim().toLowerCase()).not.toBe('frequently used'); expect(title.textContent.trim().toLowerCase()).not.toBe('frequently used');
}); });
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should have any frequently used section when there are frequently used emojis', done => { it('should have any frequently used section when there are frequently used emojis', async () => {
awardsHandler.addEmojiToFrequentlyUsedList('8ball'); awardsHandler.addEmojiToFrequentlyUsedList('8ball');
return openAndWaitForEmojiMenu() await openAndWaitForEmojiMenu();
.then(() => {
const emojiMenu = document.querySelector('.emoji-menu'); const emojiMenu = document.querySelector('.emoji-menu');
const hasFrequentlyUsedHeading = Array.prototype.some.call( const hasFrequentlyUsedHeading = Array.prototype.some.call(
emojiMenu.querySelectorAll('.emoji-menu-title'), emojiMenu.querySelectorAll('.emoji-menu-title'),
...@@ -390,11 +341,6 @@ describe('AwardsHandler', () => { ...@@ -390,11 +341,6 @@ describe('AwardsHandler', () => {
); );
expect(hasFrequentlyUsedHeading).toBe(true); expect(hasFrequentlyUsedHeading).toBe(true);
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
it('should disregard invalid frequently used emoji that are being attempted to be added', () => { it('should disregard invalid frequently used emoji that are being attempted to be added', () => {
......
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