summaryrefslogtreecommitdiff
path: root/spec/frontend/jira_import/utils/jira_import_utils_spec.js
blob: 9696d95f8c49952910a4f95d9bfcb80d60f90941 (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
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import { JIRA_IMPORT_SUCCESS_ALERT_HIDE_MAP_KEY } from '~/issues_list/constants';
import {
  calculateJiraImportLabel,
  extractJiraProjectsOptions,
  IMPORT_STATE,
  isFinished,
  isInProgress,
  setFinishedAlertHideMap,
  shouldShowFinishedAlert,
} from '~/jira_import/utils/jira_import_utils';

useLocalStorageSpy();

describe('isInProgress', () => {
  it.each`
    state                     | result
    ${IMPORT_STATE.SCHEDULED} | ${true}
    ${IMPORT_STATE.STARTED}   | ${true}
    ${IMPORT_STATE.FAILED}    | ${false}
    ${IMPORT_STATE.FINISHED}  | ${false}
    ${IMPORT_STATE.NONE}      | ${false}
    ${undefined}              | ${false}
  `('returns $result when state is $state', ({ state, result }) => {
    expect(isInProgress(state)).toBe(result);
  });
});

describe('isFinished', () => {
  it.each`
    state                     | result
    ${IMPORT_STATE.SCHEDULED} | ${false}
    ${IMPORT_STATE.STARTED}   | ${false}
    ${IMPORT_STATE.FAILED}    | ${false}
    ${IMPORT_STATE.FINISHED}  | ${true}
    ${IMPORT_STATE.NONE}      | ${false}
    ${undefined}              | ${false}
  `('returns $result when state is $state', ({ state, result }) => {
    expect(isFinished(state)).toBe(result);
  });
});

describe('extractJiraProjectsOptions', () => {
  const jiraProjects = [
    {
      key: 'MJP',
      name: 'My Jira project',
    },
    {
      key: 'MTG',
      name: 'Migrate to GitLab',
    },
  ];

  const expected = [
    {
      text: 'My Jira project (MJP)',
      value: 'MJP',
    },
    {
      text: 'Migrate to GitLab (MTG)',
      value: 'MTG',
    },
  ];

  it('returns a list of Jira projects in a format suitable for GlFormSelect', () => {
    expect(extractJiraProjectsOptions(jiraProjects)).toEqual(expected);
  });
});

describe('calculateJiraImportLabel', () => {
  const jiraImports = [
    { jiraProjectKey: 'MTG' },
    { jiraProjectKey: 'MJP' },
    { jiraProjectKey: 'MTG' },
    { jiraProjectKey: 'MSJP' },
    { jiraProjectKey: 'MTG' },
  ];

  const labels = [
    { color: '#111', title: 'jira-import::MTG-1' },
    { color: '#222', title: 'jira-import::MTG-2' },
    { color: '#333', title: 'jira-import::MTG-3' },
  ];

  it('returns a label with the Jira project key and correct import count in the title', () => {
    const label = calculateJiraImportLabel(jiraImports, labels);

    expect(label.title).toBe('jira-import::MTG-3');
  });

  it('returns a label with the correct color', () => {
    const label = calculateJiraImportLabel(jiraImports, labels);

    expect(label.color).toBe('#333');
  });
});

describe('shouldShowFinishedAlert', () => {
  const labelTitle = 'jira-import::JCP-1';

  afterEach(() => {
    localStorage.clear();
  });

  it('checks localStorage value', () => {
    jest.spyOn(localStorage, 'getItem').mockReturnValue(JSON.stringify({}));

    shouldShowFinishedAlert(labelTitle, IMPORT_STATE.FINISHED);

    expect(localStorage.getItem).toHaveBeenCalledWith(JIRA_IMPORT_SUCCESS_ALERT_HIDE_MAP_KEY);
  });

  it('returns true when an import has finished', () => {
    jest.spyOn(localStorage, 'getItem').mockReturnValue(JSON.stringify({}));

    expect(shouldShowFinishedAlert(labelTitle, IMPORT_STATE.FINISHED)).toBe(true);
  });

  it('returns false when an import has finished but the user chose to hide the alert', () => {
    jest.spyOn(localStorage, 'getItem').mockReturnValue(JSON.stringify({ [labelTitle]: true }));

    expect(shouldShowFinishedAlert(labelTitle, IMPORT_STATE.FINISHED)).toBe(false);
  });

  it('returns false when an import has not finished', () => {
    jest.spyOn(localStorage, 'getItem').mockReturnValue(JSON.stringify({}));

    expect(shouldShowFinishedAlert(labelTitle, IMPORT_STATE.SCHEDULED)).toBe(false);
  });
});

describe('setFinishedAlertHideMap', () => {
  const labelTitle = 'jira-import::ABC-1';
  const newLabelTitle = 'jira-import::JCP-1';

  it('sets item to localStorage correctly', () => {
    jest.spyOn(localStorage, 'getItem').mockReturnValue(JSON.stringify({ [labelTitle]: true }));

    setFinishedAlertHideMap(newLabelTitle);

    expect(localStorage.setItem).toHaveBeenCalledWith(
      JIRA_IMPORT_SUCCESS_ALERT_HIDE_MAP_KEY,
      JSON.stringify({
        [labelTitle]: true,
        [newLabelTitle]: true,
      }),
    );
  });
});