summaryrefslogtreecommitdiff
path: root/spec/frontend/jira_connect/subscriptions/utils_spec.js
blob: 762d9eb34437bf466ecb8b513036b83b3da1c9b3 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
import { ALERT_LOCALSTORAGE_KEY } from '~/jira_connect/subscriptions/constants';
import {
  persistAlert,
  retrieveAlert,
  getJwt,
  getLocation,
  reloadPage,
  sizeToParent,
  getGitlabSignInURL,
} from '~/jira_connect/subscriptions/utils';

describe('JiraConnect utils', () => {
  describe('alert utils', () => {
    useLocalStorageSpy();

    it.each`
      arg                                                                                 | expectedRetrievedValue
      ${{ title: 'error' }}                                                               | ${{ title: 'error' }}
      ${{ title: 'error', randomKey: 'test' }}                                            | ${{ title: 'error' }}
      ${{ title: 'error', message: 'error message', linkUrl: 'link', variant: 'danger' }} | ${{ title: 'error', message: 'error message', linkUrl: 'link', variant: 'danger' }}
      ${undefined}                                                                        | ${{}}
    `(
      'persists and retrieves alert data from localStorage when arg is $arg',
      ({ arg, expectedRetrievedValue }) => {
        persistAlert(arg);

        expect(localStorage.setItem).toHaveBeenCalledWith(
          ALERT_LOCALSTORAGE_KEY,
          JSON.stringify(expectedRetrievedValue),
        );

        const retrievedValue = retrieveAlert();

        expect(localStorage.getItem).toHaveBeenCalledWith(ALERT_LOCALSTORAGE_KEY);
        expect(retrievedValue).toEqual(expectedRetrievedValue);
      },
    );
  });

  describe('AP object utils', () => {
    afterEach(() => {
      global.AP = null;
    });

    describe('getJwt', () => {
      const mockJwt = 'jwt';
      const getTokenSpy = jest.fn((callback) => callback(mockJwt));

      it('resolves to the function call when AP.context.getToken is a function', async () => {
        global.AP = {
          context: {
            getToken: getTokenSpy,
          },
        };

        const jwt = await getJwt();

        expect(getTokenSpy).toHaveBeenCalled();
        expect(jwt).toBe(mockJwt);
      });

      it('resolves to undefined when AP.context.getToken is not a function', async () => {
        const jwt = await getJwt();

        expect(getTokenSpy).not.toHaveBeenCalled();
        expect(jwt).toBeUndefined();
      });
    });

    describe('getLocation', () => {
      const mockLocation = 'test/location';
      const getLocationSpy = jest.fn((callback) => callback(mockLocation));

      it('resolves to the function call when AP.getLocation is a function', async () => {
        global.AP = {
          getLocation: getLocationSpy,
        };

        const location = await getLocation();

        expect(getLocationSpy).toHaveBeenCalled();
        expect(location).toBe(mockLocation);
      });

      it('resolves to undefined when AP.getLocation is not a function', async () => {
        const location = await getLocation();

        expect(getLocationSpy).not.toHaveBeenCalled();
        expect(location).toBeUndefined();
      });
    });

    describe('reloadPage', () => {
      const reloadSpy = jest.fn();

      useMockLocationHelper();

      it('calls the function when AP.navigator.reload is a function', async () => {
        global.AP = {
          navigator: {
            reload: reloadSpy,
          },
        };

        await reloadPage();

        expect(reloadSpy).toHaveBeenCalled();
        expect(window.location.reload).not.toHaveBeenCalled();
      });

      it('calls window.location.reload when AP.navigator.reload is not a function', async () => {
        await reloadPage();

        expect(reloadSpy).not.toHaveBeenCalled();
        expect(window.location.reload).toHaveBeenCalled();
      });
    });

    describe('sizeToParent', () => {
      const sizeToParentSpy = jest.fn();

      it('calls the function when AP.sizeToParent is a function', async () => {
        global.AP = {
          sizeToParent: sizeToParentSpy,
        };

        await sizeToParent();

        expect(sizeToParentSpy).toHaveBeenCalled();
      });

      it('does nothing when AP.navigator.reload is not a function', async () => {
        await sizeToParent();

        expect(sizeToParentSpy).not.toHaveBeenCalled();
      });
    });
  });

  describe('getGitlabSignInURL', () => {
    const mockSignInURL = 'https://gitlab.com/sign_in';

    it.each`
      returnTo            | expectResult
      ${undefined}        | ${mockSignInURL}
      ${''}               | ${mockSignInURL}
      ${'/test/location'} | ${`${mockSignInURL}?return_to=${encodeURIComponent('/test/location')}`}
    `(
      'returns `$expectResult` when `AP.getLocation` resolves to `$returnTo`',
      async ({ returnTo, expectResult }) => {
        global.AP = {
          getLocation: jest.fn().mockImplementation((cb) => cb(returnTo)),
        };

        const url = await getGitlabSignInURL(mockSignInURL);
        expect(url).toBe(expectResult);
      },
    );
  });
});