feature_highlight_spec.js 5.37 KB
Newer Older
1 2
import * as featureHighlightHelper from '~/feature_highlight/feature_highlight_helper';
import * as featureHighlight from '~/feature_highlight/feature_highlight';
Jacob Schatz's avatar
Jacob Schatz committed
3 4
import axios from '~/lib/utils/axios_utils';
import MockAdapter from 'axios-mock-adapter';
5 6 7 8 9

describe('feature highlight', () => {
  beforeEach(() => {
    setFixtures(`
      <div>
Jacob Schatz's avatar
Jacob Schatz committed
10
        <div class="js-feature-highlight" data-highlight="test" data-highlight-priority="10" data-dismiss-endpoint="/test" disabled>
11 12 13 14 15 16 17 18 19 20 21 22 23
          Trigger
        </div>
      </div>
      <div class="feature-highlight-popover-content">
        Content
        <div class="dismiss-feature-highlight">
          Dismiss
        </div>
      </div>
    `);
  });

  describe('setupFeatureHighlightPopover', () => {
Jacob Schatz's avatar
Jacob Schatz committed
24
    let mock;
25
    const selector = '.js-feature-highlight[data-highlight=test]';
Jacob Schatz's avatar
Jacob Schatz committed
26

27
    beforeEach(() => {
Jacob Schatz's avatar
Jacob Schatz committed
28 29
      mock = new MockAdapter(axios);
      mock.onGet('/test').reply(200);
30 31 32 33 34
      spyOn(window, 'addEventListener');
      spyOn(window, 'removeEventListener');
      featureHighlight.setupFeatureHighlightPopover('test', 0);
    });

Jacob Schatz's avatar
Jacob Schatz committed
35 36 37 38
    afterEach(() => {
      mock.restore();
    });

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    it('setup popover content', () => {
      const $popoverContent = $('.feature-highlight-popover-content');
      const outerHTML = $popoverContent.prop('outerHTML');

      expect($(selector).data('content')).toEqual(outerHTML);
    });

    it('setup mouseenter', () => {
      const toggleSpy = spyOn(featureHighlightHelper.togglePopover, 'call');
      $(selector).trigger('mouseenter');

      expect(toggleSpy).toHaveBeenCalledWith(jasmine.any(Object), true);
    });

    it('setup debounced mouseleave', (done) => {
      const toggleSpy = spyOn(featureHighlightHelper.togglePopover, 'call');
      $(selector).trigger('mouseleave');

      // Even though we've set the debounce to 0ms, setTimeout is needed for the debounce
      setTimeout(() => {
        expect(toggleSpy).toHaveBeenCalledWith(jasmine.any(Object), false);
        done();
      }, 0);
    });

    it('setup show.bs.popover', () => {
      $(selector).trigger('show.bs.popover');
      expect(window.addEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function));
    });

    it('setup hide.bs.popover', () => {
      $(selector).trigger('hide.bs.popover');
      expect(window.removeEventListener).toHaveBeenCalledWith('scroll', jasmine.any(Function));
    });

    it('removes disabled attribute', () => {
      expect($('.js-feature-highlight').is(':disabled')).toEqual(false);
    });

    it('displays popover', () => {
Jacob Schatz's avatar
Jacob Schatz committed
79
      expect(document.querySelector(selector).getAttribute('aria-describedby')).toBeFalsy();
80
      $(selector).trigger('mouseenter');
Jacob Schatz's avatar
Jacob Schatz committed
81 82 83 84 85 86 87 88 89 90 91
      expect(document.querySelector(selector).getAttribute('aria-describedby')).toBeTruthy();
    });

    it('toggles when clicked', () => {
      $(selector).trigger('mouseenter');
      const popoverId = $(selector).attr('aria-describedby');
      const toggleSpy = spyOn(featureHighlightHelper.togglePopover, 'call');

      $(`#${popoverId} .dismiss-feature-highlight`).click();

      expect(toggleSpy).toHaveBeenCalled();
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    });
  });

  describe('findHighestPriorityFeature', () => {
    beforeEach(() => {
      setFixtures(`
        <div class="js-feature-highlight" data-highlight="test" data-highlight-priority="10" disabled></div>
        <div class="js-feature-highlight" data-highlight="test-high-priority" data-highlight-priority="20" disabled></div>
        <div class="js-feature-highlight" data-highlight="test-low-priority" data-highlight-priority="0" disabled></div>
      `);
    });

    it('should pick the highest priority feature highlight', () => {
      setFixtures(`
        <div class="js-feature-highlight" data-highlight="test" data-highlight-priority="10" disabled></div>
        <div class="js-feature-highlight" data-highlight="test-high-priority" data-highlight-priority="20" disabled></div>
        <div class="js-feature-highlight" data-highlight="test-low-priority" data-highlight-priority="0" disabled></div>
      `);

      expect($('.js-feature-highlight').length).toBeGreaterThan(1);
      expect(featureHighlight.findHighestPriorityFeature()).toEqual('test-high-priority');
    });

    it('should work when no priority is set', () => {
      setFixtures(`
        <div class="js-feature-highlight" data-highlight="test" disabled></div>
      `);

      expect(featureHighlight.findHighestPriorityFeature()).toEqual('test');
    });

    it('should pick the highest priority feature highlight when some have no priority set', () => {
      setFixtures(`
        <div class="js-feature-highlight" data-highlight="test-no-priority1" disabled></div>
        <div class="js-feature-highlight" data-highlight="test" data-highlight-priority="10" disabled></div>
        <div class="js-feature-highlight" data-highlight="test-no-priority2" disabled></div>
        <div class="js-feature-highlight" data-highlight="test-high-priority" data-highlight-priority="20" disabled></div>
        <div class="js-feature-highlight" data-highlight="test-low-priority" data-highlight-priority="0" disabled></div>
      `);

      expect($('.js-feature-highlight').length).toBeGreaterThan(1);
      expect(featureHighlight.findHighestPriorityFeature()).toEqual('test-high-priority');
    });
  });

  describe('highlightFeatures', () => {
    it('calls setupFeatureHighlightPopover', () => {
      expect(featureHighlight.highlightFeatures()).toEqual('test');
    });
  });
});