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 || {};
let openAndWaitForEmojiMenu;
let awardsHandler = null;
const urlRoot = gon.relative_url_root;
const lazyAssert = function(done, assertFn) {
setTimeout(function() {
assertFn();
done();
// Maybe jasmine.clock here?
}, 333);
};
describe('AwardsHandler', function() {
preloadFixtures('snippets/show.html.raw');
beforeEach(function(done) {
loadFixtures('snippets/show.html.raw');
loadAwardsHandler(true)
.then(obj => {
awardsHandler = obj;
spyOn(awardsHandler, 'postEmoji').and.callFake((button, url, emoji, cb) => cb());
done();
})
.catch(fail);
let isEmojiMenuBuilt = false;
openAndWaitForEmojiMenu = function() {
return new Promise(resolve => {
if (isEmojiMenuBuilt) {
resolve();
} else {
$('.js-add-award')
.eq(0)
.click();
const $menu = $('.emoji-menu');
$menu.one('build-emoji-menu-finish', () => {
isEmojiMenuBuilt = true;
resolve();
});
}
});
};
});
awardsHandler = null; afterEach(function() {
// restore original url root value
gon.relative_url_root = urlRoot;
window.gl || (window.gl = {}); // Undo what we did to the shared <body>
$('body').removeAttr('data-page');
window.gon || (window.gon = {}); awardsHandler.destroy();
});
urlRoot = gon.relative_url_root; describe('::showEmojiMenu', function() {
it('should show emoji menu when Add emoji button clicked', function(done) {
$('.js-add-award')
.eq(0)
.click();
lazyAssert(done, function() {
const $emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1);
expect($emojiMenu.hasClass('is-visible')).toBe(true);
expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1);
expect($('.js-awards-block.current').length).toBe(1);
});
});
lazyAssert = function(done, assertFn) { it('should also show emoji menu for the smiley icon in notes', function(done) {
return setTimeout(function() { $('.js-add-award.note-action-button').click();
assertFn(); lazyAssert(done, function() {
return done(); const $emojiMenu = $('.emoji-menu');
// Maybe jasmine.clock here?
}, 333);
};
describe('AwardsHandler', function() { expect($emojiMenu.length).toBe(1);
preloadFixtures('snippets/show.html.raw'); });
beforeEach(function(done) { });
loadFixtures('snippets/show.html.raw');
loadAwardsHandler(true)
.then(obj => {
awardsHandler = obj;
spyOn(awardsHandler, 'postEmoji').and.callFake((button, url, emoji, cb) => cb());
done();
})
.catch(fail);
let isEmojiMenuBuilt = false; it('should remove emoji menu when body is clicked', function(done) {
openAndWaitForEmojiMenu = function() { $('.js-add-award')
return new Promise((resolve, reject) => { .eq(0)
if (isEmojiMenuBuilt) { .click();
resolve(); lazyAssert(done, function() {
} else { const $emojiMenu = $('.emoji-menu');
$('.js-add-award') $('body').click();
.eq(0)
.click(); expect($emojiMenu.length).toBe(1);
const $menu = $('.emoji-menu'); expect($emojiMenu.hasClass('is-visible')).toBe(false);
$menu.one('build-emoji-menu-finish', () => { expect($('.js-awards-block.current').length).toBe(0);
isEmojiMenuBuilt = true; });
resolve();
});
}
});
};
}); });
afterEach(function() { it('should not remove emoji menu when search is clicked', function(done) {
// restore original url root value $('.js-add-award')
gon.relative_url_root = urlRoot; .eq(0)
.click();
lazyAssert(done, function() {
const $emojiMenu = $('.emoji-menu');
$('.emoji-search').click();
expect($emojiMenu.length).toBe(1);
expect($emojiMenu.hasClass('is-visible')).toBe(true);
expect($('.js-awards-block.current').length).toBe(1);
});
});
});
// Undo what we did to the shared <body> describe('::addAwardToEmojiBar', function() {
$('body').removeAttr('data-page'); it('should add emoji to votes block', function() {
const $votesBlock = $('.js-awards-block').eq(0);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
const $emojiButton = $votesBlock.find('[data-name=heart]');
awardsHandler.destroy(); expect($emojiButton.length).toBe(1);
expect($emojiButton.next('.js-counter').text()).toBe('1');
expect($votesBlock.hasClass('hidden')).toBe(false);
}); });
describe('::showEmojiMenu', function() { it('should remove the emoji when we click again', function() {
it('should show emoji menu when Add emoji button clicked', function(done) { const $votesBlock = $('.js-awards-block').eq(0);
$('.js-add-award') awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
.eq(0) awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
.click(); const $emojiButton = $votesBlock.find('[data-name=heart]');
return lazyAssert(done, function() {
var $emojiMenu;
$emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1);
expect($emojiMenu.hasClass('is-visible')).toBe(true);
expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1);
return expect($('.js-awards-block.current').length).toBe(1);
});
});
it('should also show emoji menu for the smiley icon in notes', function(done) { expect($emojiButton.length).toBe(0);
$('.js-add-award.note-action-button').click(); });
return lazyAssert(done, function() {
var $emojiMenu = $('.emoji-menu');
expect($emojiMenu.length).toBe(1); it('should decrement the emoji counter', function() {
}); const $votesBlock = $('.js-awards-block').eq(0);
}); awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
const $emojiButton = $votesBlock.find('[data-name=heart]');
$emojiButton.next('.js-counter').text(5);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
it('should remove emoji menu when body is clicked', function(done) { expect($emojiButton.length).toBe(1);
$('.js-add-award') expect($emojiButton.next('.js-counter').text()).toBe('4');
.eq(0) });
.click(); });
return lazyAssert(done, function() {
var $emojiMenu;
$emojiMenu = $('.emoji-menu');
$('body').click();
expect($emojiMenu.length).toBe(1);
expect($emojiMenu.hasClass('is-visible')).toBe(false);
return expect($('.js-awards-block.current').length).toBe(0);
});
});
it('should not remove emoji menu when search is clicked', function(done) { describe('::userAuthored', function() {
$('.js-add-award') it('should update tooltip to user authored title', function() {
.eq(0) const $votesBlock = $('.js-awards-block').eq(0);
.click(); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
return lazyAssert(done, function() { $thumbsUpEmoji.attr('data-title', 'sam');
var $emojiMenu; awardsHandler.userAuthored($thumbsUpEmoji);
$emojiMenu = $('.emoji-menu');
$('.emoji-search').click(); expect($thumbsUpEmoji.data('originalTitle')).toBe(
'You cannot vote on your own issue, MR and note',
expect($emojiMenu.length).toBe(1); );
expect($emojiMenu.hasClass('is-visible')).toBe(true);
return expect($('.js-awards-block.current').length).toBe(1);
});
});
}); });
describe('::addAwardToEmojiBar', function() { it('should restore tooltip back to initial vote list', function() {
it('should add emoji to votes block', function() { jasmine.clock().install();
var $emojiButton, $votesBlock; const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); $thumbsUpEmoji.attr('data-title', 'sam');
$emojiButton = $votesBlock.find('[data-name=heart]'); awardsHandler.userAuthored($thumbsUpEmoji);
jasmine.clock().tick(2801);
jasmine.clock().uninstall();
expect($emojiButton.length).toBe(1); expect($thumbsUpEmoji.data('originalTitle')).toBe('sam');
expect($emojiButton.next('.js-counter').text()).toBe('1'); });
return expect($votesBlock.hasClass('hidden')).toBe(false); });
});
it('should remove the emoji when we click again', function() { describe('::getAwardUrl', function() {
var $emojiButton, $votesBlock; it('returns the url for request', function() {
$votesBlock = $('.js-awards-block').eq(0); expect(awardsHandler.getAwardUrl()).toBe('http://test.host/snippets/1/toggle_award_emoji');
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); });
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); });
$emojiButton = $votesBlock.find('[data-name=heart]');
expect($emojiButton.length).toBe(0); describe('::addAward and ::checkMutuality', function() {
}); it('should handle :+1: and :-1: mutuality', function() {
return it('should decrement the emoji counter', function() { const awardUrl = awardsHandler.getAwardUrl();
var $emojiButton, $votesBlock; const $votesBlock = $('.js-awards-block').eq(0);
$votesBlock = $('.js-awards-block').eq(0); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); const $thumbsDownEmoji = $votesBlock.find('[data-name=thumbsdown]').parent();
$emojiButton = $votesBlock.find('[data-name=heart]'); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$emojiButton.next('.js-counter').text(5);
awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false); expect($thumbsUpEmoji.hasClass('active')).toBe(true);
expect($thumbsDownEmoji.hasClass('active')).toBe(false);
expect($emojiButton.length).toBe(1); $thumbsUpEmoji.tooltip();
return expect($emojiButton.next('.js-counter').text()).toBe('4'); $thumbsDownEmoji.tooltip();
}); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsdown', true);
expect($thumbsUpEmoji.hasClass('active')).toBe(false);
expect($thumbsDownEmoji.hasClass('active')).toBe(true);
}); });
});
describe('::userAuthored', function() { describe('::removeEmoji', function() {
it('should update tooltip to user authored title', function() { it('should remove emoji', function() {
var $thumbsUpEmoji, $votesBlock; const awardUrl = awardsHandler.getAwardUrl();
$votesBlock = $('.js-awards-block').eq(0); const $votesBlock = $('.js-awards-block').eq(0);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); awardsHandler.addAward($votesBlock, awardUrl, 'fire', false);
$thumbsUpEmoji.attr('data-title', 'sam');
awardsHandler.userAuthored($thumbsUpEmoji);
expect($thumbsUpEmoji.data('originalTitle')).toBe(
'You cannot vote on your own issue, MR and note',
);
});
it('should restore tooltip back to initial vote list', function() { expect($votesBlock.find('[data-name=fire]').length).toBe(1);
var $thumbsUpEmoji, $votesBlock; awardsHandler.removeEmoji($votesBlock.find('[data-name=fire]').closest('button'));
jasmine.clock().install();
$votesBlock = $('.js-awards-block').eq(0); expect($votesBlock.find('[data-name=fire]').length).toBe(0);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'sam');
awardsHandler.userAuthored($thumbsUpEmoji);
jasmine.clock().tick(2801);
jasmine.clock().uninstall();
expect($thumbsUpEmoji.data('originalTitle')).toBe('sam');
});
}); });
});
describe('::getAwardUrl', function() { describe('::addYouToUserList', function() {
return it('returns the url for request', function() { it('should prepend "You" to the award tooltip', function() {
const awardUrl = awardsHandler.getAwardUrl();
const $votesBlock = $('.js-awards-block').eq(0);
const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'sam, jerry, max, and andy');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip();
expect(awardsHandler.getAwardUrl()).toBe( expect($thumbsUpEmoji.data('originalTitle')).toBe('You, sam, jerry, max, and andy');
'http://test.host/snippets/1/toggle_award_emoji',
);
});
}); });
describe('::addAward and ::checkMutuality', function() { it('handles the special case where "You" is not cleanly comma seperated', function() {
return it('should handle :+1: and :-1: mutuality', function() { const awardUrl = awardsHandler.getAwardUrl();
var $thumbsDownEmoji, $thumbsUpEmoji, $votesBlock, awardUrl; const $votesBlock = $('.js-awards-block').eq(0);
awardUrl = awardsHandler.getAwardUrl(); const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$votesBlock = $('.js-awards-block').eq(0); $thumbsUpEmoji.attr('data-title', 'sam');
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsDownEmoji = $votesBlock.find('[data-name=thumbsdown]').parent(); $thumbsUpEmoji.tooltip();
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
expect($thumbsUpEmoji.data('originalTitle')).toBe('You and sam');
expect($thumbsUpEmoji.hasClass('active')).toBe(true);
expect($thumbsDownEmoji.hasClass('active')).toBe(false);
$thumbsUpEmoji.tooltip();
$thumbsDownEmoji.tooltip();
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsdown', true);
expect($thumbsUpEmoji.hasClass('active')).toBe(false);
return expect($thumbsDownEmoji.hasClass('active')).toBe(true);
});
}); });
});
describe('::removeEmoji', function() { describe('::removeYouToUserList', function() {
return it('should remove emoji', function() { it('removes "You" from the front of the tooltip', function() {
var $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();
awardsHandler.addAward($votesBlock, awardUrl, 'fire', false); $thumbsUpEmoji.attr('data-title', 'You, sam, jerry, max, and andy');
$thumbsUpEmoji.addClass('active');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip();
expect($thumbsUpEmoji.data('originalTitle')).toBe('sam, jerry, max, and andy');
});
expect($votesBlock.find('[data-name=fire]').length).toBe(1); it('handles the special case where "You" is not cleanly comma seperated', function() {
awardsHandler.removeEmoji($votesBlock.find('[data-name=fire]').closest('button')); const awardUrl = awardsHandler.getAwardUrl();
const $votesBlock = $('.js-awards-block').eq(0);
const $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
$thumbsUpEmoji.attr('data-title', 'You and sam');
$thumbsUpEmoji.addClass('active');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip();
expect($votesBlock.find('[data-name=fire]').length).toBe(0); expect($thumbsUpEmoji.data('originalTitle')).toBe('sam');
});
}); });
});
describe('::addYouToUserList', function() { describe('::searchEmojis', () => {
it('should prepend "You" to the award tooltip', function() { it('should filter the emoji', function(done) {
var $thumbsUpEmoji, $votesBlock, awardUrl; openAndWaitForEmojiMenu()
awardUrl = awardsHandler.getAwardUrl(); .then(() => {
$votesBlock = $('.js-awards-block').eq(0); expect($('[data-name=angel]').is(':visible')).toBe(true);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); expect($('[data-name=anger]').is(':visible')).toBe(true);
$thumbsUpEmoji.attr('data-title', 'sam, jerry, max, and andy'); awardsHandler.searchEmojis('ali');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip(); expect($('[data-name=angel]').is(':visible')).toBe(false);
expect($('[data-name=anger]').is(':visible')).toBe(false);
expect($thumbsUpEmoji.data('originalTitle')).toBe('You, sam, jerry, max, and andy'); expect($('[data-name=alien]').is(':visible')).toBe(true);
}); expect($('.js-emoji-menu-search').val()).toBe('ali');
return it('handles the special case where "You" is not cleanly comma seperated', function() { })
var $thumbsUpEmoji, $votesBlock, awardUrl; .then(done)
awardUrl = awardsHandler.getAwardUrl(); .catch(err => {
$votesBlock = $('.js-awards-block').eq(0); done.fail(`Failed to open and build emoji menu: ${err.message}`);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); });
$thumbsUpEmoji.attr('data-title', 'sam');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip();
expect($thumbsUpEmoji.data('originalTitle')).toBe('You and sam');
});
}); });
describe('::removeYouToUserList', function() { it('should clear the search when searching for nothing', function(done) {
it('removes "You" from the front of the tooltip', function() { openAndWaitForEmojiMenu()
var $thumbsUpEmoji, $votesBlock, awardUrl; .then(() => {
awardUrl = awardsHandler.getAwardUrl(); awardsHandler.searchEmojis('ali');
$votesBlock = $('.js-awards-block').eq(0);
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); expect($('[data-name=angel]').is(':visible')).toBe(false);
$thumbsUpEmoji.attr('data-title', 'You, sam, jerry, max, and andy'); expect($('[data-name=anger]').is(':visible')).toBe(false);
$thumbsUpEmoji.addClass('active'); expect($('[data-name=alien]').is(':visible')).toBe(true);
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false); awardsHandler.searchEmojis('');
$thumbsUpEmoji.tooltip();
expect($('[data-name=angel]').is(':visible')).toBe(true);
expect($thumbsUpEmoji.data('originalTitle')).toBe('sam, jerry, max, and andy'); expect($('[data-name=anger]').is(':visible')).toBe(true);
}); expect($('[data-name=alien]').is(':visible')).toBe(true);
return it('handles the special case where "You" is not cleanly comma seperated', function() { expect($('.js-emoji-menu-search').val()).toBe('');
var $thumbsUpEmoji, $votesBlock, awardUrl; })
awardUrl = awardsHandler.getAwardUrl(); .then(done)
$votesBlock = $('.js-awards-block').eq(0); .catch(err => {
$thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent(); done.fail(`Failed to open and build emoji menu: ${err.message}`);
$thumbsUpEmoji.attr('data-title', 'You and sam'); });
$thumbsUpEmoji.addClass('active');
awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
$thumbsUpEmoji.tooltip();
expect($thumbsUpEmoji.data('originalTitle')).toBe('sam');
});
}); });
});
describe('::searchEmojis', () => { describe('emoji menu', function() {
it('should filter the emoji', function(done) { const emojiSelector = '[data-name="sunglasses"]';
return openAndWaitForEmojiMenu() const openEmojiMenuAndAddEmoji = function() {
.then(() => { return openAndWaitForEmojiMenu().then(() => {
expect($('[data-name=angel]').is(':visible')).toBe(true); const $menu = $('.emoji-menu');
expect($('[data-name=anger]').is(':visible')).toBe(true); const $block = $('.js-awards-block');
awardsHandler.searchEmojis('ali'); const $emoji = $menu.find(`.emoji-menu-list:not(.frequent-emojis) ${emojiSelector}`);
expect($('[data-name=angel]').is(':visible')).toBe(false);
expect($('[data-name=anger]').is(':visible')).toBe(false);
expect($('[data-name=alien]').is(':visible')).toBe(true);
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', function(done) { expect($emoji.length).toBe(1);
return openAndWaitForEmojiMenu() expect($block.find(emojiSelector).length).toBe(0);
.then(() => { $emoji.click();
awardsHandler.searchEmojis('ali');
expect($menu.hasClass('.is-visible')).toBe(false);
expect($('[data-name=angel]').is(':visible')).toBe(false); expect($block.find(emojiSelector).length).toBe(1);
expect($('[data-name=anger]').is(':visible')).toBe(false);
expect($('[data-name=alien]').is(':visible')).toBe(true);
awardsHandler.searchEmojis('');
expect($('[data-name=angel]').is(':visible')).toBe(true);
expect($('[data-name=anger]').is(':visible')).toBe(true);
expect($('[data-name=alien]').is(':visible')).toBe(true);
expect($('.js-emoji-menu-search').val()).toBe('');
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
};
it('should add selected emoji to awards block', function(done) {
openEmojiMenuAndAddEmoji()
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
describe('emoji menu', function() { it('should remove already selected emoji', function(done) {
const emojiSelector = '[data-name="sunglasses"]'; openEmojiMenuAndAddEmoji()
const openEmojiMenuAndAddEmoji = function() { .then(() => {
return openAndWaitForEmojiMenu().then(() => { $('.js-add-award')
const $menu = $('.emoji-menu'); .eq(0)
.click();
const $block = $('.js-awards-block'); const $block = $('.js-awards-block');
const $emoji = $menu.find('.emoji-menu-list:not(.frequent-emojis) ' + emojiSelector); const $emoji = $('.emoji-menu').find(
`.emoji-menu-list:not(.frequent-emojis) ${emojiSelector}`,
expect($emoji.length).toBe(1); );
expect($block.find(emojiSelector).length).toBe(0);
$emoji.click(); $emoji.click();
expect($menu.hasClass('.is-visible')).toBe(false); expect($block.find(emojiSelector).length).toBe(0);
expect($block.find(emojiSelector).length).toBe(1); })
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
}); });
}; });
it('should add selected emoji to awards block', function(done) { });
return openEmojiMenuAndAddEmoji()
.then(done) describe('frequently used emojis', function() {
.catch(err => { beforeEach(() => {
done.fail(`Failed to open and build emoji menu: ${err.message}`); // Clear it out
}); Cookies.set('frequently_used_emojis', '');
}); });
it('should remove already selected emoji', function(done) { it('shouldn\'t have any "Frequently used" heading if no frequently used emojis', function(done) {
return openEmojiMenuAndAddEmoji() return openAndWaitForEmojiMenu()
.then(() => { .then(() => {
$('.js-add-award') const emojiMenu = document.querySelector('.emoji-menu');
.eq(0) Array.prototype.forEach.call(emojiMenu.querySelectorAll('.emoji-menu-title'), title => {
.click(); expect(title.textContent.trim().toLowerCase()).not.toBe('frequently used');
const $block = $('.js-awards-block');
const $emoji = $('.emoji-menu').find(
`.emoji-menu-list:not(.frequent-emojis) ${emojiSelector}`,
);
$emoji.click();
expect($block.find(emojiSelector).length).toBe(0);
})
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
}); });
}); })
.then(done)
.catch(err => {
done.fail(`Failed to open and build emoji menu: ${err.message}`);
});
}); });
describe('frequently used emojis', function() { it('should have any frequently used section when there are frequently used emojis', function(done) {
beforeEach(() => { awardsHandler.addEmojiToFrequentlyUsedList('8ball');
// Clear it out
Cookies.set('frequently_used_emojis', '');
});
it('shouldn\'t have any "Frequently used" heading if no frequently used emojis', function(done) { return openAndWaitForEmojiMenu()
return openAndWaitForEmojiMenu() .then(() => {
.then(() => { const emojiMenu = document.querySelector('.emoji-menu');
const emojiMenu = document.querySelector('.emoji-menu'); const hasFrequentlyUsedHeading = Array.prototype.some.call(
Array.prototype.forEach.call(emojiMenu.querySelectorAll('.emoji-menu-title'), title => { emojiMenu.querySelectorAll('.emoji-menu-title'),
expect(title.textContent.trim().toLowerCase()).not.toBe('frequently used'); title => title.textContent.trim().toLowerCase() === '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', function(done) { expect(hasFrequentlyUsedHeading).toBe(true);
awardsHandler.addEmojiToFrequentlyUsedList('8ball'); })
.then(done)
return openAndWaitForEmojiMenu() .catch(err => {
.then(() => { done.fail(`Failed to open and build emoji menu: ${err.message}`);
const emojiMenu = document.querySelector('.emoji-menu'); });
const hasFrequentlyUsedHeading = Array.prototype.some.call( });
emojiMenu.querySelectorAll('.emoji-menu-title'),
title => title.textContent.trim().toLowerCase() === 'frequently used',
);
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', function() { it('should disregard invalid frequently used emoji that are being attempted to be added', function() {
awardsHandler.addEmojiToFrequentlyUsedList('8ball'); awardsHandler.addEmojiToFrequentlyUsedList('8ball');
awardsHandler.addEmojiToFrequentlyUsedList('invalid_emoji'); awardsHandler.addEmojiToFrequentlyUsedList('invalid_emoji');
awardsHandler.addEmojiToFrequentlyUsedList('grinning'); awardsHandler.addEmojiToFrequentlyUsedList('grinning');
expect(awardsHandler.getFrequentlyUsedEmojis()).toEqual(['8ball', 'grinning']); expect(awardsHandler.getFrequentlyUsedEmojis()).toEqual(['8ball', 'grinning']);
}); });
it('should disregard invalid frequently used emoji already set in cookie', function() { it('should disregard invalid frequently used emoji already set in cookie', function() {
Cookies.set('frequently_used_emojis', '8ball,invalid_emoji,grinning'); Cookies.set('frequently_used_emojis', '8ball,invalid_emoji,grinning');
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