summaryrefslogtreecommitdiff
path: root/spec/frontend/jira_connect/index_spec.js
blob: eb54fe6476f2f8abe872e17521487e7714bace56 (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
import waitForPromises from 'helpers/wait_for_promises';
import { initJiraConnect } from '~/jira_connect';
import { removeSubscription } from '~/jira_connect/api';

jest.mock('~/jira_connect/api', () => ({
  removeSubscription: jest.fn().mockResolvedValue(),
  getLocation: jest.fn().mockResolvedValue('test/location'),
}));

describe('initJiraConnect', () => {
  window.AP = {
    navigator: {
      reload: jest.fn(),
    },
  };

  beforeEach(async () => {
    setFixtures(`
      <a class="js-jira-connect-sign-in" href="https://gitlab.com">Sign In</a>
      <a class="js-jira-connect-sign-in" href="https://gitlab.com">Another Sign In</a>

      <a href="https://gitlab.com/sub1" class="js-jira-connect-remove-subscription">Remove</a>
      <a href="https://gitlab.com/sub2" class="js-jira-connect-remove-subscription">Remove</a>
      <a href="https://gitlab.com/sub3" class="js-jira-connect-remove-subscription">Remove</a>
    `);

    await initJiraConnect();
  });

  describe('Sign in links', () => {
    it('have `return_to` query parameter', () => {
      Array.from(document.querySelectorAll('.js-jira-connect-sign-in')).forEach((el) => {
        expect(el.href).toContain('return_to=test/location');
      });
    });
  });

  describe('`remove subscription` buttons', () => {
    describe('on click', () => {
      it('calls `removeSubscription`', () => {
        Array.from(document.querySelectorAll('.js-jira-connect-remove-subscription')).forEach(
          (removeSubscriptionButton) => {
            removeSubscriptionButton.dispatchEvent(new Event('click'));

            waitForPromises();

            expect(removeSubscription).toHaveBeenCalledWith(removeSubscriptionButton.href);
            expect(removeSubscription).toHaveBeenCalledTimes(1);

            removeSubscription.mockClear();
          },
        );
      });
    });
  });
});