awards_handler_spec.js 15.7 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-var, one-var, one-var-declaration-per-line, no-unused-expressions, comma-dangle, new-parens, no-unused-vars, quotes, jasmine/no-spec-dupes, prefer-template, max-len */
F
Fatih Acet 已提交
2

3
import $ from 'jquery';
4
import Cookies from 'js-cookie';
5
import loadAwardsHandler from '~/awards_handler';
E
Eric Eastwood 已提交
6

7
import '~/lib/utils/common_utils';
B
barthc 已提交
8

F
Fatih Acet 已提交
9
(function() {
E
Eric Eastwood 已提交
10
  var awardsHandler, lazyAssert, urlRoot, openAndWaitForEmojiMenu;
F
Fatih Acet 已提交
11 12 13 14 15 16 17

  awardsHandler = null;

  window.gl || (window.gl = {});

  window.gon || (window.gon = {});

18
  urlRoot = gon.relative_url_root;
F
Fatih Acet 已提交
19 20 21 22 23

  lazyAssert = function(done, assertFn) {
    return setTimeout(function() {
      assertFn();
      return done();
24
    // Maybe jasmine.clock here?
F
Fatih Acet 已提交
25 26 27 28
    }, 333);
  };

  describe('AwardsHandler', function() {
F
Filipa Lacerda 已提交
29
    preloadFixtures('merge_requests/diff_comment.html.raw');
30
    beforeEach(function(done) {
F
Filipa Lacerda 已提交
31
      loadFixtures('merge_requests/diff_comment.html.raw');
32
      $('body').attr('data-page', 'projects:merge_requests:show');
33 34 35 36 37
      loadAwardsHandler(true).then((obj) => {
        awardsHandler = obj;
        spyOn(awardsHandler, 'postEmoji').and.callFake((button, url, emoji, cb) => cb());
        done();
      }).catch(fail);
E
Eric Eastwood 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

      let isEmojiMenuBuilt = false;
      openAndWaitForEmojiMenu = function() {
        return new Promise((resolve, reject) => {
          if (isEmojiMenuBuilt) {
            resolve();
          } else {
            $('.js-add-award').eq(0).click();
            const $menu = $('.emoji-menu');
            $menu.one('build-emoji-menu-finish', () => {
              isEmojiMenuBuilt = true;
              resolve();
            });
          }
        });
      };
54 55 56 57
    });
    afterEach(function() {
      // restore original url root value
      gon.relative_url_root = urlRoot;
E
Eric Eastwood 已提交
58

59 60 61
      // Undo what we did to the shared <body>
      $('body').removeAttr('data-page');

E
Eric Eastwood 已提交
62
      awardsHandler.destroy();
F
Fatih Acet 已提交
63 64 65 66 67 68 69 70 71
    });
    describe('::showEmojiMenu', function() {
      it('should show emoji menu when Add emoji button clicked', function(done) {
        $('.js-add-award').eq(0).click();
        return lazyAssert(done, function() {
          var $emojiMenu;
          $emojiMenu = $('.emoji-menu');
          expect($emojiMenu.length).toBe(1);
          expect($emojiMenu.hasClass('is-visible')).toBe(true);
72
          expect($emojiMenu.find('.js-emoji-menu-search').length).toBe(1);
F
Fatih Acet 已提交
73 74 75 76
          return expect($('.js-awards-block.current').length).toBe(1);
        });
      });
      it('should also show emoji menu for the smiley icon in notes', function(done) {
E
Eric Eastwood 已提交
77
        $('.js-add-award.note-action-button').click();
F
Fatih Acet 已提交
78
        return lazyAssert(done, function() {
E
Eric Eastwood 已提交
79
          var $emojiMenu = $('.emoji-menu');
F
Fatih Acet 已提交
80 81 82
          return expect($emojiMenu.length).toBe(1);
        });
      });
83
      it('should remove emoji menu when body is clicked', function(done) {
F
Fatih Acet 已提交
84 85 86 87 88 89 90 91 92 93
        $('.js-add-award').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);
        });
      });
94 95 96 97 98 99 100 101 102 103 104
      it('should not remove emoji menu when search is clicked', function(done) {
        $('.js-add-award').eq(0).click();
        return lazyAssert(done, function() {
          var $emojiMenu;
          $emojiMenu = $('.emoji-menu');
          $('.emoji-search').click();
          expect($emojiMenu.length).toBe(1);
          expect($emojiMenu.hasClass('is-visible')).toBe(true);
          return expect($('.js-awards-block.current').length).toBe(1);
        });
      });
F
Fatih Acet 已提交
105 106 107 108 109 110
    });
    describe('::addAwardToEmojiBar', function() {
      it('should add emoji to votes block', function() {
        var $emojiButton, $votesBlock;
        $votesBlock = $('.js-awards-block').eq(0);
        awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
E
Eric Eastwood 已提交
111
        $emojiButton = $votesBlock.find('[data-name=heart]');
F
Fatih Acet 已提交
112 113 114 115 116 117 118 119 120
        expect($emojiButton.length).toBe(1);
        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() {
        var $emojiButton, $votesBlock;
        $votesBlock = $('.js-awards-block').eq(0);
        awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
        awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
E
Eric Eastwood 已提交
121
        $emojiButton = $votesBlock.find('[data-name=heart]');
F
Fatih Acet 已提交
122 123 124 125 126 127
        return expect($emojiButton.length).toBe(0);
      });
      return it('should decrement the emoji counter', function() {
        var $emojiButton, $votesBlock;
        $votesBlock = $('.js-awards-block').eq(0);
        awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
E
Eric Eastwood 已提交
128
        $emojiButton = $votesBlock.find('[data-name=heart]');
F
Fatih Acet 已提交
129 130 131 132 133 134
        $emojiButton.next('.js-counter').text(5);
        awardsHandler.addAwardToEmojiBar($votesBlock, 'heart', false);
        expect($emojiButton.length).toBe(1);
        return expect($emojiButton.next('.js-counter').text()).toBe('4');
      });
    });
B
barthc 已提交
135 136 137 138 139 140 141
    describe('::userAuthored', function() {
      it('should update tooltip to user authored title', function() {
        var $thumbsUpEmoji, $votesBlock;
        $votesBlock = $('.js-awards-block').eq(0);
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
        $thumbsUpEmoji.attr('data-title', 'sam');
        awardsHandler.userAuthored($thumbsUpEmoji);
J
Jacob Schatz 已提交
142
        return expect($thumbsUpEmoji.data("originalTitle")).toBe("You cannot vote on your own issue, MR and note");
B
barthc 已提交
143 144 145 146 147 148 149 150 151 152
      });
      it('should restore tooltip back to initial vote list', function() {
        var $thumbsUpEmoji, $votesBlock;
        jasmine.clock().install();
        $votesBlock = $('.js-awards-block').eq(0);
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
        $thumbsUpEmoji.attr('data-title', 'sam');
        awardsHandler.userAuthored($thumbsUpEmoji);
        jasmine.clock().tick(2801);
        jasmine.clock().uninstall();
J
Jacob Schatz 已提交
153
        return expect($thumbsUpEmoji.data("originalTitle")).toBe("sam");
B
barthc 已提交
154 155
      });
    });
F
Fatih Acet 已提交
156
    describe('::getAwardUrl', function() {
V
Valery Sizov 已提交
157
      return it('returns the url for request', function() {
F
Filipa Lacerda 已提交
158
        return expect(awardsHandler.getAwardUrl()).toBe('http://test.host/frontend-fixtures/merge-requests-project/merge_requests/1/toggle_award_emoji');
F
Fatih Acet 已提交
159 160 161 162 163 164 165
      });
    });
    describe('::addAward and ::checkMutuality', function() {
      return it('should handle :+1: and :-1: mutuality', function() {
        var $thumbsDownEmoji, $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
166 167
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
        $thumbsDownEmoji = $votesBlock.find('[data-name=thumbsdown]').parent();
F
Fatih Acet 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        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() {
      return it('should remove emoji', function() {
        var $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
        awardsHandler.addAward($votesBlock, awardUrl, 'fire', false);
E
Eric Eastwood 已提交
184 185 186
        expect($votesBlock.find('[data-name=fire]').length).toBe(1);
        awardsHandler.removeEmoji($votesBlock.find('[data-name=fire]').closest('button'));
        return expect($votesBlock.find('[data-name=fire]').length).toBe(0);
F
Fatih Acet 已提交
187 188
      });
    });
189 190
    describe('::addYouToUserList', function() {
      it('should prepend "You" to the award tooltip', function() {
191 192 193
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
194
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
195
        $thumbsUpEmoji.attr('data-title', 'sam, jerry, max, and andy');
196 197
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
J
Jacob Schatz 已提交
198
        return expect($thumbsUpEmoji.data("originalTitle")).toBe('You, sam, jerry, max, and andy');
199
      });
200
      return it('handles the special case where "You" is not cleanly comma seperated', function() {
201 202 203
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
204
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
205 206 207
        $thumbsUpEmoji.attr('data-title', 'sam');
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
J
Jacob Schatz 已提交
208
        return expect($thumbsUpEmoji.data("originalTitle")).toBe('You and sam');
209 210
      });
    });
211 212
    describe('::removeYouToUserList', function() {
      it('removes "You" from the front of the tooltip', function() {
213 214 215
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
216
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
217
        $thumbsUpEmoji.attr('data-title', 'You, sam, jerry, max, and andy');
218 219 220
        $thumbsUpEmoji.addClass('active');
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
J
Jacob Schatz 已提交
221
        return expect($thumbsUpEmoji.data("originalTitle")).toBe('sam, jerry, max, and andy');
222
      });
223
      return it('handles the special case where "You" is not cleanly comma seperated', function() {
224 225 226
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
227
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
228
        $thumbsUpEmoji.attr('data-title', 'You and sam');
229 230 231
        $thumbsUpEmoji.addClass('active');
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
J
Jacob Schatz 已提交
232
        return expect($thumbsUpEmoji.data("originalTitle")).toBe('sam');
233 234
      });
    });
235 236
    describe('::searchEmojis', () => {
      it('should filter the emoji', function(done) {
E
Eric Eastwood 已提交
237 238 239 240
        return openAndWaitForEmojiMenu()
          .then(() => {
            expect($('[data-name=angel]').is(':visible')).toBe(true);
            expect($('[data-name=anger]').is(':visible')).toBe(true);
241
            awardsHandler.searchEmojis('ali');
E
Eric Eastwood 已提交
242 243 244
            expect($('[data-name=angel]').is(':visible')).toBe(false);
            expect($('[data-name=anger]').is(':visible')).toBe(false);
            expect($('[data-name=alien]').is(':visible')).toBe(true);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
            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) {
        return openAndWaitForEmojiMenu()
          .then(() => {
            awardsHandler.searchEmojis('ali');
            expect($('[data-name=angel]').is(':visible')).toBe(false);
            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('');
E
Eric Eastwood 已提交
264 265
          })
          .then(done)
266 267
          .catch((err) => {
            done.fail(`Failed to open and build emoji menu: ${err.message}`);
E
Eric Eastwood 已提交
268
          });
F
Fatih Acet 已提交
269 270
      });
    });
271

E
Eric Eastwood 已提交
272
    describe('emoji menu', function() {
273
      const emojiSelector = '[data-name="sunglasses"]';
E
Eric Eastwood 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
      const openEmojiMenuAndAddEmoji = function() {
        return openAndWaitForEmojiMenu()
          .then(() => {
            const $menu = $('.emoji-menu');
            const $block = $('.js-awards-block');
            const $emoji = $menu.find('.emoji-menu-list:not(.frequent-emojis) ' + emojiSelector);

            expect($emoji.length).toBe(1);
            expect($block.find(emojiSelector).length).toBe(0);
            $emoji.click();
            expect($menu.hasClass('.is-visible')).toBe(false);
            expect($block.find(emojiSelector).length).toBe(1);
          });
F
Fatih Acet 已提交
287
      };
E
Eric Eastwood 已提交
288 289 290
      it('should add selected emoji to awards block', function(done) {
        return openEmojiMenuAndAddEmoji()
          .then(done)
291 292
          .catch((err) => {
            done.fail(`Failed to open and build emoji menu: ${err.message}`);
E
Eric Eastwood 已提交
293
          });
F
Fatih Acet 已提交
294
      });
E
Eric Eastwood 已提交
295 296 297 298 299 300 301 302 303 304 305
      it('should remove already selected emoji', function(done) {
        return openEmojiMenuAndAddEmoji()
          .then(() => {
            $('.js-add-award').eq(0).click();
            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) => {
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            done.fail(`Failed to open and build emoji menu: ${err.message}`);
          });
      });
    });

    describe('frequently used emojis', function() {
      beforeEach(() => {
        // 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()
          .then(() => {
            const emojiMenu = document.querySelector('.emoji-menu');
            Array.prototype.forEach.call(emojiMenu.querySelectorAll('.emoji-menu-title'), (title) => {
              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', function(done) {
        awardsHandler.addEmojiToFrequentlyUsedList('8ball');

        return openAndWaitForEmojiMenu()
          .then(() => {
            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}`);
E
Eric Eastwood 已提交
346
          });
F
Fatih Acet 已提交
347
      });
348 349 350 351 352 353 354 355 356 357 358 359 360 361

      it('should disregard invalid frequently used emoji that are being attempted to be added', function() {
        awardsHandler.addEmojiToFrequentlyUsedList('8ball');
        awardsHandler.addEmojiToFrequentlyUsedList('invalid_emoji');
        awardsHandler.addEmojiToFrequentlyUsedList('grinning');

        expect(awardsHandler.getFrequentlyUsedEmojis()).toEqual(['8ball', 'grinning']);
      });

      it('should disregard invalid frequently used emoji already set in cookie', function() {
        Cookies.set('frequently_used_emojis', '8ball,invalid_emoji,grinning');

        expect(awardsHandler.getFrequentlyUsedEmojis()).toEqual(['8ball', 'grinning']);
      });
F
Fatih Acet 已提交
362 363
    });
  });
364
}).call(window);