summaryrefslogtreecommitdiff
path: root/spec/javascripts/feature_highlight/feature_highlight_helper_spec.js
blob: 22f46caa8ea9f710243d2dd44c1d7f2d65edee83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
import $ from 'jquery';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import {
  getSelector,
  dismiss,
  inserted,
} from '~/feature_highlight/feature_highlight_helper';
import { togglePopover } from '~/shared/popover';

import getSetTimeoutPromise from 'spec/helpers/set_timeout_promise_helper';

describe('feature highlight helper', () => {
  describe('getSelector', () => {
    it('returns js-feature-highlight selector', () => {
      const highlightId = 'highlightId';

      expect(getSelector(highlightId)).toEqual(`.js-feature-highlight[data-highlight=${highlightId}]`);
    });
  });

  describe('dismiss', () => {
    let mock;
    const context = {
      hide: () => {},
      attr: () => '/-/callouts/dismiss',
    };

    beforeEach(() => {
      mock = new MockAdapter(axios);

      spyOn(togglePopover, 'call').and.callFake(() => {});
      spyOn(context, 'hide').and.callFake(() => {});
      dismiss.call(context);
    });

    afterEach(() => {
      mock.restore();
    });

    it('calls persistent dismissal endpoint', (done) => {
      const spy = jasmine.createSpy('dismiss-endpoint-hit');
      mock.onPost('/-/callouts/dismiss').reply(spy);

      getSetTimeoutPromise()
        .then(() => {
          expect(spy).toHaveBeenCalled();
        })
        .then(done)
        .catch(done.fail);
    });

    it('calls hide popover', () => {
      expect(togglePopover.call).toHaveBeenCalledWith(context, false);
    });

    it('calls hide', () => {
      expect(context.hide).toHaveBeenCalled();
    });
  });

  describe('inserted', () => {
    it('registers click event callback', (done) => {
      const context = {
        getAttribute: () => 'popoverId',
        dataset: {
          highlight: 'some-feature',
        },
      };

      spyOn($.fn, 'on').and.callFake((event) => {
        expect(event).toEqual('click');
        done();
      });
      inserted.call(context);
    });
  });
});