BigW Consortium Gitlab

project_select_combo_button_spec.js 4.05 KB
Newer Older
1 2 3 4
import ProjectSelectComboButton from '~/project_select_combo_button';

const fixturePath = 'static/project_select_combo_button.html.raw';

5
describe('Project Select Combo Button', function () {
6 7
  preloadFixtures(fixturePath);

8
  beforeEach(function () {
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    this.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',
      },
      localStorageKey: 'group-12345-new-issue-recent-project',
      relativePath: 'issues/new',
    };

    loadFixtures(fixturePath);

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

30 31
  describe('on page load when localStorage is empty', function () {
    beforeEach(function () {
32 33 34
      this.comboButton = new ProjectSelectComboButton(this.projectSelectInput);
    });

35
    it('newItemBtn href is null', function () {
36 37 38
      expect(this.newItemBtn.getAttribute('href')).toBe('');
    });

39
    it('newItemBtn text is the plain default label', function () {
40 41 42 43
      expect(this.newItemBtn.textContent).toBe(this.defaults.label);
    });
  });

44 45
  describe('on page load when localStorage is filled', function () {
    beforeEach(function () {
46 47 48 49 50
      window.localStorage
        .setItem(this.defaults.localStorageKey, JSON.stringify(this.defaults.projectMeta));
      this.comboButton = new ProjectSelectComboButton(this.projectSelectInput);
    });

51
    it('newItemBtn href is correctly set', function () {
52 53 54
      expect(this.newItemBtn.getAttribute('href')).toBe(this.defaults.projectMeta.url);
    });

55
    it('newItemBtn text is the cached label', function () {
56 57 58 59
      expect(this.newItemBtn.textContent)
        .toBe(`New issue in ${this.defaults.projectMeta.name}`);
    });

60
    afterEach(function () {
61 62 63 64
      window.localStorage.clear();
    });
  });

65 66
  describe('after selecting a new project', function () {
    beforeEach(function () {
67 68 69 70 71 72 73 74
      this.comboButton = new ProjectSelectComboButton(this.projectSelectInput);

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

75
    it('newItemBtn href is correctly set', function () {
76 77 78 79
      expect(this.newItemBtn.getAttribute('href'))
        .toBe('http://myothercoolproject.com/issues/new');
    });

80
    it('newItemBtn text is the selected project label', function () {
81 82 83 84
      expect(this.newItemBtn.textContent)
        .toBe(`New issue in ${this.defaults.newProjectMeta.name}`);
    });

85
    afterEach(function () {
86 87 88
      window.localStorage.clear();
    });
  });
89

90 91
  describe('deriveTextVariants', function () {
    beforeEach(function () {
92 93 94 95 96 97 98 99 100 101
      this.mockExecutionContext = {
        resourceType: '',
        resourceLabel: '',
      };

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

      this.method = this.comboButton.deriveTextVariants.bind(this.mockExecutionContext);
    });

102
    it('correctly derives test variants for merge requests', function () {
103 104 105 106 107 108 109 110 111 112
      this.mockExecutionContext.resourceType = 'merge_requests';
      this.mockExecutionContext.resourceLabel = 'New merge request';

      const returnedVariants = this.method();

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

113
    it('correctly derives text variants for issues', function () {
114 115 116 117 118 119 120 121 122 123
      this.mockExecutionContext.resourceType = 'issues';
      this.mockExecutionContext.resourceLabel = 'New issue';

      const returnedVariants = this.method();

      expect(returnedVariants.localStorageItemType).toBe('new-issue');
      expect(returnedVariants.defaultTextPrefix).toBe('New issue');
      expect(returnedVariants.presetTextSuffix).toBe('issue');
    });
  });
124 125
});