summaryrefslogtreecommitdiff
path: root/spec/frontend/project_select_combo_button_spec.js
blob: b8d5a1a61f3b8acea448711be1ae6a9cdd32082d (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
163
164
165
import $ from 'jquery';
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import ProjectSelectComboButton from '~/project_select_combo_button';

const fixturePath = 'static/project_select_combo_button.html';

describe('Project Select Combo Button', () => {
  let testContext;

  beforeEach(() => {
    testContext = {};
  });

  beforeEach(() => {
    testContext.defaults = {
      label: 'Select project to create issue',
      groupId: 12345,
      projectMeta: {
        name: 'My Cool Project',
        url: 'http://mycoolproject.com',
      },
      newProjectMeta: {
        name: 'My Other Cool Project',
        url: 'http://myothercoolproject.com',
      },
      vulnerableProject: {
        name: 'Self XSS',
        // eslint-disable-next-line no-script-url
        url: 'javascript:alert(1)',
      },
      localStorageKey: 'group-12345-new-issue-recent-project',
      relativePath: 'issues/new',
    };

    loadHTMLFixture(fixturePath);

    testContext.newItemBtn = document.querySelector('.js-new-project-item-link');
    testContext.projectSelectInput = document.querySelector('.project-item-select');
  });

  afterEach(() => {
    resetHTMLFixture();
  });

  describe('on page load when localStorage is empty', () => {
    beforeEach(() => {
      testContext.comboButton = new ProjectSelectComboButton(testContext.projectSelectInput);
    });

    it('newItemBtn href is null', () => {
      expect(testContext.newItemBtn.getAttribute('href')).toBe('');
    });

    it('newItemBtn text is the plain default label', () => {
      expect(testContext.newItemBtn.textContent).toBe(testContext.defaults.label);
    });
  });

  describe('on page load when localStorage is filled', () => {
    beforeEach(() => {
      window.localStorage.setItem(
        testContext.defaults.localStorageKey,
        JSON.stringify(testContext.defaults.projectMeta),
      );
      testContext.comboButton = new ProjectSelectComboButton(testContext.projectSelectInput);
    });

    it('newItemBtn href is correctly set', () => {
      expect(testContext.newItemBtn.getAttribute('href')).toBe(
        testContext.defaults.projectMeta.url,
      );
    });

    it('newItemBtn text is the cached label', () => {
      expect(testContext.newItemBtn.textContent).toBe(
        `New issue in ${testContext.defaults.projectMeta.name}`,
      );
    });

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

  describe('after selecting a new project', () => {
    beforeEach(() => {
      testContext.comboButton = new ProjectSelectComboButton(testContext.projectSelectInput);

      // mock the effect of selecting an item from the projects dropdown (select2)
      $('.project-item-select')
        .val(JSON.stringify(testContext.defaults.newProjectMeta))
        .trigger('change');
    });

    it('newItemBtn href is correctly set', () => {
      expect(testContext.newItemBtn.getAttribute('href')).toBe(
        'http://myothercoolproject.com/issues/new',
      );
    });

    it('newItemBtn text is the selected project label', () => {
      expect(testContext.newItemBtn.textContent).toBe(
        `New issue in ${testContext.defaults.newProjectMeta.name}`,
      );
    });

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

  describe('after selecting a vulnerable project', () => {
    beforeEach(() => {
      testContext.comboButton = new ProjectSelectComboButton(testContext.projectSelectInput);

      // mock the effect of selecting an item from the projects dropdown (select2)
      $('.project-item-select')
        .val(JSON.stringify(testContext.defaults.vulnerableProject))
        .trigger('change');
    });

    it('newItemBtn href is correctly sanitized', () => {
      expect(testContext.newItemBtn.getAttribute('href')).toBe('about:blank');
    });

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

  describe('deriveTextVariants', () => {
    beforeEach(() => {
      testContext.mockExecutionContext = {
        resourceType: '',
        resourceLabel: '',
      };

      testContext.comboButton = new ProjectSelectComboButton(testContext.projectSelectInput);

      testContext.method = testContext.comboButton.deriveTextVariants.bind(
        testContext.mockExecutionContext,
      );
    });

    it('correctly derives test variants for merge requests', () => {
      testContext.mockExecutionContext.resourceType = 'merge_requests';
      testContext.mockExecutionContext.resourceLabel = 'New merge request';

      const returnedVariants = testContext.method();

      expect(returnedVariants.localStorageItemType).toBe('new-merge-request');
      expect(returnedVariants.presetTextSuffix).toBe('merge request');
    });

    it('correctly derives text variants for issues', () => {
      testContext.mockExecutionContext.resourceType = 'issues';
      testContext.mockExecutionContext.resourceLabel = 'New issue';

      const returnedVariants = testContext.method();

      expect(returnedVariants.localStorageItemType).toBe('new-issue');
      expect(returnedVariants.presetTextSuffix).toBe('issue');
    });
  });
});