Commit 5a6fffcf authored by Mike Greiling's avatar Mike Greiling

Prettify awards_handler_spec.js script

parent 6faaa317
/* eslint-disable no-var, one-var, no-unused-expressions, no-unused-vars, prefer-template */
import $ from 'jquery'; import $ from 'jquery';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import loadAwardsHandler from '~/awards_handler'; import loadAwardsHandler from '~/awards_handler';
import '~/lib/utils/common_utils'; import '~/lib/utils/common_utils';
(function() { window.gl = window.gl || {};
var awardsHandler, lazyAssert, urlRoot, openAndWaitForEmojiMenu; window.gon = window.gon || {};
awardsHandler = null;
window.gl || (window.gl = {});
window.gon || (window.gon = {}); let openAndWaitForEmojiMenu;
let awardsHandler = null;
const urlRoot = gon.relative_url_root;
urlRoot = gon.relative_url_root; const lazyAssert = function(done, assertFn) {
setTimeout(function() {
lazyAssert = function(done, assertFn) {
return setTimeout(function() {
assertFn(); assertFn();
return done(); done();
// Maybe jasmine.clock here? // Maybe jasmine.clock here?
}, 333); }, 333);
}; };
describe('AwardsHandler', function() { describe('AwardsHandler', function() {
preloadFixtures('snippets/show.html.raw'); preloadFixtures('snippets/show.html.raw');
beforeEach(function(done) { beforeEach(function(done) {
loadFixtures('snippets/show.html.raw'); loadFixtures('snippets/show.html.raw');
...@@ -39,7 +32,7 @@ import '~/lib/utils/common_utils'; ...@@ -39,7 +32,7 @@ import '~/lib/utils/common_utils';
let isEmojiMenuBuilt = false; let isEmojiMenuBuilt = false;
openAndWaitForEmojiMenu = function() { openAndWaitForEmojiMenu = function() {
return new Promise((resolve, reject) => { return new Promise(resolve => {
if (isEmojiMenuBuilt) { if (isEmojiMenuBuilt) {
resolve(); resolve();
} else { } else {
...@@ -71,21 +64,20 @@ import '~/lib/utils/common_utils'; ...@@ -71,21 +64,20 @@ import '~/lib/utils/common_utils';
$('.js-add-award') $('.js-add-award')
.eq(0) .eq(0)
.click(); .click();
return lazyAssert(done, function() { lazyAssert(done, function() {
var $emojiMenu; const $emojiMenu = $('.emoji-menu');
$emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
expect($emojiMenu.hasClass('is-visible')).toBe(true); expect($emojiMenu.hasClass('is-visible')).toBe(true);
expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1); expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1);
return 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', function(done) { it('should also show emoji menu for the smiley icon in notes', function(done) {
$('.js-add-award.note-action-button').click(); $('.js-add-award.note-action-button').click();
return lazyAssert(done, function() { lazyAssert(done, function() {
var $emojiMenu = $('.emoji-menu'); const $emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
}); });
...@@ -95,14 +87,13 @@ import '~/lib/utils/common_utils'; ...@@ -95,14 +87,13 @@ import '~/lib/utils/common_utils';
$('.js-add-award') $('.js-add-award')
.eq(0) .eq(0)
.click(); .click();
return lazyAssert(done, function() { lazyAssert(done, function() {
var $emojiMenu; const $emojiMenu = $('.emoji-menu');
$emojiMenu = $('.emoji-menu');
$('body').click(); $('body').click();
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
expect($emojiMenu.hasClass('is-visible')).toBe(false); expect($emojiMenu.hasClass('is-visible')).toBe(false);
return expect($('.js-awards-block.current').length).toBe(0); expect($('.js-awards-block.current').length).toBe(0);
}); });
}); });
...@@ -110,57 +101,53 @@ import '~/lib/utils/common_utils'; ...@@ -110,57 +101,53 @@ import '~/lib/utils/common_utils';
$('.js-add-award') $('.js-add-award')
.eq(0) .eq(0)
.click(); .click();
return lazyAssert(done, function() { lazyAssert(done, function() {
var $emojiMenu; const $emojiMenu = $('.emoji-menu');
$emojiMenu = $('.emoji-menu');
$('.emoji-search').click(); $('.emoji-search').click();
expect($emojiMenu.length).toBe(1); expect($emojiMenu.length).toBe(1);
expect($emojiMenu.hasClass('is-visible')).toBe(true); expect($emojiMenu.hasClass('is-visible')).toBe(true);
return expect($('.js-awards-block.current').length).toBe(1); expect($('.js-awards-block.current').length).toBe(1);
}); });
}); });
}); });
describe('::addAwardToEmojiBar', function() { describe('::addAwardToEmojiBar', function() {
it('should add emoji to votes block', function() { it('should add emoji to votes block', function() {
var $emojiButton, $votesBlock; const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
$emojiButton = $votesBlock.find('[data-name=heart]'); const $emojiButton = $votesBlock.find('[data-name=heart]');
expect($emojiButton.length).toBe(1); expect($emojiButton.length).toBe(1);
expect($emojiButton.next('.js-counter').text()).toBe('1'); expect($emojiButton.next('.js-counter').text()).toBe('1');
return expect($votesBlock.hasClass('hidden')).toBe(false); expect($votesBlock.hasClass('hidden')).toBe(false);
}); });
it('should remove the emoji when we click again', function() { it('should remove the emoji when we click again', function() {
var $emojiButton, $votesBlock; const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
$emojiButton = $votesBlock.find('[data-name=heart]'); const $emojiButton = $votesBlock.find('[data-name=heart]');
expect($emojiButton.length).toBe(0); expect($emojiButton.length).toBe(0);
}); });
return it('should decrement the emoji counter', function() {
var $emojiButton, $votesBlock; it('should decrement the emoji counter', function() {
$votesBlock = $('.js-awards-block').eq(0); const $votesBlock = $('.js-awards-block').eq(0);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
$emojiButton = $votesBlock.find('[data-name=heart]'); const $emojiButton = $votesBlock.find('[data-name=heart]');
$emojiButton.next('.js-counter').text(5); $emojiButton.next('.js-counter').text(5);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
expect($emojiButton.length).toBe(1); expect($emojiButton.length).toBe(1);
return expect($emojiButton.next('.js-counter').text()).toBe('4'); expect($emojiButton.next('.js-counter').text()).toBe('4');
}); });
}); });
describe('::userAuthored', function() { describe('::userAuthored', function() {
it('should update tooltip to user authored title', function() { it('should update tooltip to user authored title', function() {
var $thumbsUpEmoji, $votesBlock; const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'sam'); $thumbsUpEmoji.attr('data-title', 'sam');
awardsHandler.userAuthored($thumbsUpEmoji); awardsHandler.userAuthored($thumbsUpEmoji);
...@@ -170,10 +157,9 @@ import '~/lib/utils/common_utils'; ...@@ -170,10 +157,9 @@ import '~/lib/utils/common_utils';
}); });
it('should restore tooltip back to initial vote list', function() { it('should restore tooltip back to initial vote list', function() {
var $thumbsUpEmoji, $votesBlock;
jasmine.clock().install(); jasmine.clock().install();
$votesBlock = $('.js-awards-block').eq(0); const $votesBlock = $('.js-awards-block').eq(0);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'sam'); $thumbsUpEmoji.attr('data-title', 'sam');
awardsHandler.userAuthored($thumbsUpEmoji); awardsHandler.userAuthored($thumbsUpEmoji);
jasmine.clock().tick(2801); jasmine.clock().tick(2801);
...@@ -184,21 +170,17 @@ import '~/lib/utils/common_utils'; ...@@ -184,21 +170,17 @@ import '~/lib/utils/common_utils';
}); });
describe('::getAwardUrl', function() { describe('::getAwardUrl', function() {
return it('returns the url for request', function() { it('returns the url for request', function() {
expect(awardsHandler.getAwardUrl()).toBe('http://test.host/snippets/1/toggle_award_emoji');
expect(awardsHandler.getAwardUrl()).toBe(
'http://test.host/snippets/1/toggle_award_emoji',
);
}); });
}); });
describe('::addAward and ::checkMutuality', function() { describe('::addAward and ::checkMutuality', function() {
return it('should handle :+1: and :-1: mutuality', function() { it('should handle :+1: and :-1: mutuality', function() {
var $thumbsDownEmoji, $thumbsUpEmoji, $votesBlock, awardUrl; const awardUrl = awardsHandler.getAwardUrl();
awardUrl = awardsHandler.getAwardUrl(); const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); const $thumbsDownEmoji = $votesBlock.find('[data-name=thumbsdown]').parent();
$thumbsDownEmoji = $votesBlock.find('[data-name=thumbsdown]').parent();
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
expect($thumbsUpEmoji.hasClass('active')).toBe(true); expect($thumbsUpEmoji.hasClass('active')).toBe(true);
...@@ -208,15 +190,14 @@ import '~/lib/utils/common_utils'; ...@@ -208,15 +190,14 @@ import '~/lib/utils/common_utils';
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsdown', true); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsdown', true);
expect($thumbsUpEmoji.hasClass('active')).toBe(false); expect($thumbsUpEmoji.hasClass('active')).toBe(false);
return expect($thumbsDownEmoji.hasClass('active')).toBe(true); expect($thumbsDownEmoji.hasClass('active')).toBe(true);
}); });
}); });
describe('::removeEmoji', function() { describe('::removeEmoji', function() {
return it('should remove emoji', function() { it('should remove emoji', function() {
var $votesBlock, awardUrl; const awardUrl = awardsHandler.getAwardUrl();
awardUrl = awardsHandler.getAwardUrl(); const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0);
awardsHandler.addAward($votesBlock, awardUrl, 'fire', false); awardsHandler.addAward($votesBlock, awardUrl, 'fire', false);
expect($votesBlock.find('[data-name=fire]').length).toBe(1); expect($votesBlock.find('[data-name=fire]').length).toBe(1);
...@@ -228,21 +209,20 @@ import '~/lib/utils/common_utils'; ...@@ -228,21 +209,20 @@ import '~/lib/utils/common_utils';
describe('::addYouToUserList', function() { describe('::addYouToUserList', function() {
it('should prepend "You" to the award tooltip', function() { it('should prepend "You" to the award tooltip', function() {
var $thumbsUpEmoji, $votesBlock, awardUrl; const awardUrl = awardsHandler.getAwardUrl();
awardUrl = awardsHandler.getAwardUrl(); const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'sam, jerry, max, and andy'); $thumbsUpEmoji.attr('data-title', 'sam, jerry, max, and andy');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip(); $thumbsUpEmoji.tooltip();
expect($thumbsUpEmoji.data('originalTitle')).toBe('You, sam, jerry, max, and andy'); expect($thumbsUpEmoji.data('originalTitle')).toBe('You, sam, jerry, max, and andy');
}); });
return it('handles the special case where "You" is not cleanly comma seperated', function() {
var $thumbsUpEmoji, $votesBlock, awardUrl; it('handles the special case where "You" is not cleanly comma seperated', function() {
awardUrl = awardsHandler.getAwardUrl(); const awardUrl = awardsHandler.getAwardUrl();
$votesBlock = $('.js-awards-block').eq(0); const $votesBlock = $('.js-awards-block').eq(0);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'sam'); $thumbsUpEmoji.attr('data-title', 'sam');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip(); $thumbsUpEmoji.tooltip();
...@@ -253,10 +233,9 @@ import '~/lib/utils/common_utils'; ...@@ -253,10 +233,9 @@ import '~/lib/utils/common_utils';
describe('::removeYouToUserList', function() { describe('::removeYouToUserList', function() {
it('removes "You" from the front of the tooltip', function() { it('removes "You" from the front of the tooltip', function() {
var $thumbsUpEmoji, $votesBlock, awardUrl; const awardUrl = awardsHandler.getAwardUrl();
awardUrl = awardsHandler.getAwardUrl(); const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'You, sam, jerry, max, and andy'); $thumbsUpEmoji.attr('data-title', 'You, sam, jerry, max, and andy');
$thumbsUpEmoji.addClass('active'); $thumbsUpEmoji.addClass('active');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
...@@ -264,11 +243,11 @@ import '~/lib/utils/common_utils'; ...@@ -264,11 +243,11 @@ import '~/lib/utils/common_utils';
expect($thumbsUpEmoji.data('originalTitle')).toBe('sam, jerry, max, and andy'); expect($thumbsUpEmoji.data('originalTitle')).toBe('sam, jerry, max, and andy');
}); });
return it('handles the special case where "You" is not cleanly comma seperated', function() {
var $thumbsUpEmoji, $votesBlock, awardUrl; it('handles the special case where "You" is not cleanly comma seperated', function() {
awardUrl = awardsHandler.getAwardUrl(); const awardUrl = awardsHandler.getAwardUrl();
$votesBlock = $('.js-awards-block').eq(0); const $votesBlock = $('.js-awards-block').eq(0);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'You and sam'); $thumbsUpEmoji.attr('data-title', 'You and sam');
$thumbsUpEmoji.addClass('active'); $thumbsUpEmoji.addClass('active');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
...@@ -280,7 +259,7 @@ import '~/lib/utils/common_utils'; ...@@ -280,7 +259,7 @@ import '~/lib/utils/common_utils';
describe('::searchEmojis', () => { describe('::searchEmojis', () => {
it('should filter the emoji', function(done) { it('should filter the emoji', function(done) {
return openAndWaitForEmojiMenu() openAndWaitForEmojiMenu()
.then(() => { .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);
...@@ -298,7 +277,7 @@ import '~/lib/utils/common_utils'; ...@@ -298,7 +277,7 @@ import '~/lib/utils/common_utils';
}); });
it('should clear the search when searching for nothing', function(done) { it('should clear the search when searching for nothing', function(done) {
return openAndWaitForEmojiMenu() openAndWaitForEmojiMenu()
.then(() => { .then(() => {
awardsHandler.searchEmojis('ali'); awardsHandler.searchEmojis('ali');
...@@ -325,7 +304,7 @@ import '~/lib/utils/common_utils'; ...@@ -325,7 +304,7 @@ import '~/lib/utils/common_utils';
return openAndWaitForEmojiMenu().then(() => { return openAndWaitForEmojiMenu().then(() => {
const $menu = $('.emoji-menu'); const $menu = $('.emoji-menu');
const $block = $('.js-awards-block'); const $block = $('.js-awards-block');
const $emoji = $menu.find('.emoji-menu-list:not(.frequent-emojis) ' + emojiSelector); const $emoji = $menu.find(`.emoji-menu-list:not(.frequent-emojis) ${emojiSelector}`);
expect($emoji.length).toBe(1); expect($emoji.length).toBe(1);
expect($block.find(emojiSelector).length).toBe(0); expect($block.find(emojiSelector).length).toBe(0);
...@@ -335,8 +314,9 @@ import '~/lib/utils/common_utils'; ...@@ -335,8 +314,9 @@ import '~/lib/utils/common_utils';
expect($block.find(emojiSelector).length).toBe(1); expect($block.find(emojiSelector).length).toBe(1);
}); });
}; };
it('should add selected emoji to awards block', function(done) { it('should add selected emoji to awards block', function(done) {
return openEmojiMenuAndAddEmoji() openEmojiMenuAndAddEmoji()
.then(done) .then(done)
.catch(err => { .catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`); done.fail(`Failed to open and build emoji menu: ${err.message}`);
...@@ -344,7 +324,7 @@ import '~/lib/utils/common_utils'; ...@@ -344,7 +324,7 @@ import '~/lib/utils/common_utils';
}); });
it('should remove already selected emoji', function(done) { it('should remove already selected emoji', function(done) {
return openEmojiMenuAndAddEmoji() openEmojiMenuAndAddEmoji()
.then(() => { .then(() => {
$('.js-add-award') $('.js-add-award')
.eq(0) .eq(0)
...@@ -417,5 +397,4 @@ import '~/lib/utils/common_utils'; ...@@ -417,5 +397,4 @@ import '~/lib/utils/common_utils';
expect(awardsHandler.getFrequentlyUsedEmojis()).toEqual(['8ball', 'grinning']); expect(awardsHandler.getFrequentlyUsedEmojis()).toEqual(['8ball', 'grinning']);
}); });
}); });
}); });
}.call(window));
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