awards_handler_spec.js 10.9 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

E
Eric Eastwood 已提交
3 4 5
require('es6-promise').polyfill();

const AwardsHandler = require('~/awards_handler');
F
Fatih Acet 已提交
6 7

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

  awardsHandler = null;

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

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

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

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

  describe('AwardsHandler', function() {
E
Eric Eastwood 已提交
27
    preloadFixtures('issues/issue_with_comment.html.raw');
F
Fatih Acet 已提交
28
    beforeEach(function() {
E
Eric Eastwood 已提交
29
      loadFixtures('issues/issue_with_comment.html.raw');
F
Fatih Acet 已提交
30 31 32 33 34 35
      awardsHandler = new AwardsHandler;
      spyOn(awardsHandler, 'postEmoji').and.callFake((function(_this) {
        return function(url, emoji, cb) {
          return cb();
        };
      })(this));
E
Eric Eastwood 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

      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();
            });

            // Fail after 1 second
            setTimeout(reject, 1000);
          }
        });
      };
55 56 57 58
    });
    afterEach(function() {
      // restore original url root value
      gon.relative_url_root = urlRoot;
E
Eric Eastwood 已提交
59 60

      awardsHandler.destroy();
F
Fatih Acet 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    });
    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);
          expect($emojiMenu.find('#emoji_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) {
E
Eric Eastwood 已提交
75
        $('.js-add-award.note-action-button').click();
F
Fatih Acet 已提交
76
        return lazyAssert(done, function() {
E
Eric Eastwood 已提交
77
          var $emojiMenu = $('.emoji-menu');
F
Fatih Acet 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
          return expect($emojiMenu.length).toBe(1);
        });
      });
      return it('should remove emoji menu when body is clicked', function(done) {
        $('.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);
        });
      });
    });
    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 已提交
98
        $emojiButton = $votesBlock.find('[data-name=heart]');
F
Fatih Acet 已提交
99 100 101 102 103 104 105 106 107
        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 已提交
108
        $emojiButton = $votesBlock.find('[data-name=heart]');
F
Fatih Acet 已提交
109 110 111 112 113 114
        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 已提交
115
        $emojiButton = $votesBlock.find('[data-name=heart]');
F
Fatih Acet 已提交
116 117 118 119 120 121 122
        $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');
      });
    });
    describe('::getAwardUrl', function() {
V
Valery Sizov 已提交
123
      return it('returns the url for request', function() {
124
        return expect(awardsHandler.getAwardUrl()).toBe('http://test.host/frontend-fixtures/issues-project/issues/1/toggle_award_emoji');
F
Fatih Acet 已提交
125 126 127 128 129 130 131
      });
    });
    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 已提交
132 133
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
        $thumbsDownEmoji = $votesBlock.find('[data-name=thumbsdown]').parent();
F
Fatih Acet 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        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 已提交
150 151 152
        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 已提交
153 154
      });
    });
155 156
    describe('::addYouToUserList', function() {
      it('should prepend "You" to the award tooltip', function() {
157 158 159
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
160
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
161
        $thumbsUpEmoji.attr('data-title', 'sam, jerry, max, and andy');
162 163
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
164
        return expect($thumbsUpEmoji.data("original-title")).toBe('You, sam, jerry, max, and andy');
165
      });
166
      return it('handles the special case where "You" is not cleanly comma seperated', function() {
167 168 169
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
170
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
171 172 173
        $thumbsUpEmoji.attr('data-title', 'sam');
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
174
        return expect($thumbsUpEmoji.data("original-title")).toBe('You and sam');
175 176
      });
    });
177 178
    describe('::removeYouToUserList', function() {
      it('removes "You" from the front of the tooltip', function() {
179 180 181
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
182
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
183
        $thumbsUpEmoji.attr('data-title', 'You, sam, jerry, max, and andy');
184 185 186 187 188
        $thumbsUpEmoji.addClass('active');
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
        return expect($thumbsUpEmoji.data("original-title")).toBe('sam, jerry, max, and andy');
      });
189
      return it('handles the special case where "You" is not cleanly comma seperated', function() {
190 191 192
        var $thumbsUpEmoji, $votesBlock, awardUrl;
        awardUrl = awardsHandler.getAwardUrl();
        $votesBlock = $('.js-awards-block').eq(0);
E
Eric Eastwood 已提交
193
        $thumbsUpEmoji = $votesBlock.find('[data-name=thumbsup]').parent();
194
        $thumbsUpEmoji.attr('data-title', 'You and sam');
195 196 197
        $thumbsUpEmoji.addClass('active');
        awardsHandler.addAward($votesBlock, awardUrl, 'thumbsup', false);
        $thumbsUpEmoji.tooltip();
198
        return expect($thumbsUpEmoji.data("original-title")).toBe('sam');
199 200
      });
    });
F
Fatih Acet 已提交
201
    describe('search', function() {
E
Eric Eastwood 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215
      return it('should filter the emoji', function(done) {
        return openAndWaitForEmojiMenu()
          .then(() => {
            expect($('[data-name=angel]').is(':visible')).toBe(true);
            expect($('[data-name=anger]').is(':visible')).toBe(true);
            $('#emoji_search').val('ali').trigger('input');
            expect($('[data-name=angel]').is(':visible')).toBe(false);
            expect($('[data-name=anger]').is(':visible')).toBe(false);
            expect($('[data-name=alien]').is(':visible')).toBe(true);
          })
          .then(done)
          .catch(() => {
            done.fail('Failed to open and build emoji menu');
          });
F
Fatih Acet 已提交
216 217
      });
    });
E
Eric Eastwood 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    describe('emoji menu', function() {
      const emojiSelector = '[data-name=sunglasses]';
      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 已提交
233
      };
E
Eric Eastwood 已提交
234 235 236 237 238 239
      it('should add selected emoji to awards block', function(done) {
        return openEmojiMenuAndAddEmoji()
          .then(done)
          .catch(() => {
            done.fail('Failed to open and build emoji menu');
          });
F
Fatih Acet 已提交
240
      });
E
Eric Eastwood 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253
      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) => {
            done.fail('Failed to open and build emoji menu');
          });
F
Fatih Acet 已提交
254 255 256
      });
    });
  });
257
}).call(window);