summaryrefslogtreecommitdiff
path: root/spec/frontend/onboarding_issues/index_spec.js
blob: b844caa07aa1e915df99c82cc14f507105b399b9 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
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
import $ from 'jquery';
import { showLearnGitLabIssuesPopover } from '~/onboarding_issues';
import { getCookie, setCookie, removeCookie } from '~/lib/utils/common_utils';
import setWindowLocation from 'helpers/set_window_location_helper';
import Tracking from '~/tracking';

describe('Onboarding Issues Popovers', () => {
  const COOKIE_NAME = 'onboarding_issues_settings';
  const getCookieValue = () => JSON.parse(getCookie(COOKIE_NAME));

  beforeEach(() => {
    jest.spyOn($.fn, 'popover');
  });

  afterEach(() => {
    $.fn.popover.mockRestore();
    document.getElementsByTagName('html')[0].innerHTML = '';
    removeCookie(COOKIE_NAME);
  });

  const setupShowLearnGitLabIssuesPopoverTest = ({
    currentPath = 'group/learn-gitlab',
    isIssuesBoardsLinkShown = true,
    isCookieSet = true,
    cookieValue = true,
  } = {}) => {
    setWindowLocation(`http://example.com/${currentPath}`);

    if (isIssuesBoardsLinkShown) {
      const elem = document.createElement('a');
      elem.setAttribute('data-qa-selector', 'issue_boards_link');
      document.body.appendChild(elem);
    }

    if (isCookieSet) {
      setCookie(COOKIE_NAME, { previous: true, 'issues#index': cookieValue });
    }

    showLearnGitLabIssuesPopover();
  };

  describe('showLearnGitLabIssuesPopover', () => {
    describe('when on another project', () => {
      beforeEach(() => {
        setupShowLearnGitLabIssuesPopoverTest({
          currentPath: 'group/another-project',
        });
      });

      it('does not show a popover', () => {
        expect($.fn.popover).not.toHaveBeenCalled();
      });
    });

    describe('when the issues boards link is not shown', () => {
      beforeEach(() => {
        setupShowLearnGitLabIssuesPopoverTest({
          isIssuesBoardsLinkShown: false,
        });
      });

      it('does not show a popover', () => {
        expect($.fn.popover).not.toHaveBeenCalled();
      });
    });

    describe('when the cookie is not set', () => {
      beforeEach(() => {
        setupShowLearnGitLabIssuesPopoverTest({
          isCookieSet: false,
        });
      });

      it('does not show a popover', () => {
        expect($.fn.popover).not.toHaveBeenCalled();
      });
    });

    describe('when the cookie value is false', () => {
      beforeEach(() => {
        setupShowLearnGitLabIssuesPopoverTest({
          cookieValue: false,
        });
      });

      it('does not show a popover', () => {
        expect($.fn.popover).not.toHaveBeenCalled();
      });
    });

    describe('with all the right conditions', () => {
      beforeEach(() => {
        setupShowLearnGitLabIssuesPopoverTest();
      });

      it('shows a popover', () => {
        expect($.fn.popover).toHaveBeenCalled();
      });

      it('does not change the cookie value', () => {
        expect(getCookieValue()['issues#index']).toBe(true);
      });

      it('disables the previous popover', () => {
        expect(getCookieValue().previous).toBe(false);
      });

      describe('when clicking the issues boards link', () => {
        beforeEach(() => {
          document.querySelector('a[data-qa-selector="issue_boards_link"]').click();
        });

        it('deletes the cookie', () => {
          expect(getCookie(COOKIE_NAME)).toBe(undefined);
        });
      });

      describe('when dismissing the popover', () => {
        beforeEach(() => {
          jest.spyOn(Tracking, 'event');
          document.querySelector('.learn-gitlab.popover .close').click();
        });

        it('deletes the cookie', () => {
          expect(getCookie(COOKIE_NAME)).toBe(undefined);
        });

        it('sends a tracking event', () => {
          expect(Tracking.event).toHaveBeenCalledWith(
            'Growth::Conversion::Experiment::OnboardingIssues',
            'dismiss_popover',
          );
        });
      });
    });
  });
});