mr_widget_related_links_spec.js 6.6 KB
Newer Older
F
Fatih Acet 已提交
1
import Vue from 'vue';
2
import MRWidgetRelatedLinks from '~/vue_merge_request_widget/components/mr_widget_related_links';
F
Fatih Acet 已提交
3

4 5 6 7 8 9 10 11 12 13 14 15 16
describe('MRWidgetRelatedLinks', () => {
  let vm;

  beforeEach(() => {
    const Component = Vue.extend(MRWidgetRelatedLinks);
    vm = new Component({
      el: document.createElement('div'),
      propsData: {
        isMerged: false,
        relatedLinks: {},
      },
    });
  });
F
Fatih Acet 已提交
17

18 19
  afterEach(() => {
    vm.$destroy();
F
Fatih Acet 已提交
20 21 22 23
  });

  describe('props', () => {
    it('should have props', () => {
24
      const { isMerged, relatedLinks } = MRWidgetRelatedLinks.props;
F
Fatih Acet 已提交
25

26 27 28
      expect(isMerged).toBeDefined();
      expect(isMerged.type).toBe(Boolean);
      expect(isMerged.required).toBeTruthy();
F
Fatih Acet 已提交
29 30 31 32 33 34 35
      expect(relatedLinks).toBeDefined();
      expect(relatedLinks.type instanceof Object).toBeTruthy();
      expect(relatedLinks.required).toBeTruthy();
    });
  });

  describe('computed', () => {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    describe('closingText', () => {
      const dummyIssueLabel = 'dummy label';

      beforeEach(() => {
        spyOn(vm, 'issueLabel').and.returnValue(dummyIssueLabel);
      });

      it('outputs text for closing issues', () => {
        vm.isMerged = false;

        const text = vm.closingText;

        expect(text).toBe(`Closes ${dummyIssueLabel}`);
      });

      it('outputs text for closed issues', () => {
        vm.isMerged = true;

        const text = vm.closingText;

        expect(text).toBe(`Closed ${dummyIssueLabel}`);
      });
    });

F
Fatih Acet 已提交
60 61
    describe('hasLinks', () => {
      it('should return correct value when we have links reference', () => {
62 63 64 65
        vm.relatedLinks = {
          closing: '/foo',
          mentioned: '/foo',
          assignToMe: '/foo',
F
Fatih Acet 已提交
66
        };
67

F
Fatih Acet 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
        expect(vm.hasLinks).toBeTruthy();

        vm.relatedLinks.closing = null;
        expect(vm.hasLinks).toBeTruthy();

        vm.relatedLinks.mentioned = null;
        expect(vm.hasLinks).toBeTruthy();

        vm.relatedLinks.assignToMe = null;
        expect(vm.hasLinks).toBeFalsy();
      });
    });

81 82 83 84
    describe('mentionedText', () => {
      it('outputs text for one mentioned issue before merging', () => {
        vm.isMerged = false;
        spyOn(vm, 'hasMultipleIssues').and.returnValue(false);
F
Fatih Acet 已提交
85

86 87 88
        const text = vm.mentionedText;

        expect(text).toBe('is mentioned but will not be closed');
F
Fatih Acet 已提交
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
      it('outputs text for one mentioned issue after merging', () => {
        vm.isMerged = true;
        spyOn(vm, 'hasMultipleIssues').and.returnValue(false);

        const text = vm.mentionedText;

        expect(text).toBe('is mentioned but was not closed');
      });

      it('outputs text for multiple mentioned issue before merging', () => {
        vm.isMerged = false;
        spyOn(vm, 'hasMultipleIssues').and.returnValue(true);

        const text = vm.mentionedText;

        expect(text).toBe('are mentioned but will not be closed');
      });

      it('outputs text for multiple mentioned issue after merging', () => {
        vm.isMerged = true;
        spyOn(vm, 'hasMultipleIssues').and.returnValue(true);

        const text = vm.mentionedText;

        expect(text).toBe('are mentioned but were not closed');
F
Fatih Acet 已提交
116 117
      });
    });
118
  });
F
Fatih Acet 已提交
119

120 121 122 123 124 125 126 127 128 129 130 131
  describe('methods', () => {
    const relatedLinks = {
      oneIssue: '<a href="#">#7</a>',
      twoIssues: '<a href="#">#23</a> and <a>#42</a>',
      threeIssues: '<a href="#">#1</a>, <a>#2</a>, and <a>#3</a>',
    };

    beforeEach(() => {
      vm.relatedLinks = relatedLinks;
    });

    describe('hasMultipleIssues', () => {
F
Fatih Acet 已提交
132
      it('should return true if the given text has multiple issues', () => {
133 134
        expect(vm.hasMultipleIssues(relatedLinks.twoIssues)).toBeTruthy();
        expect(vm.hasMultipleIssues(relatedLinks.threeIssues)).toBeTruthy();
F
Fatih Acet 已提交
135 136 137
      });

      it('should return false if the given text has one issue', () => {
138
        expect(vm.hasMultipleIssues(relatedLinks.oneIssue)).toBeFalsy();
F
Fatih Acet 已提交
139 140 141
      });
    });

142
    describe('issueLabel', () => {
F
Fatih Acet 已提交
143
      it('should return true if the given text has multiple issues', () => {
144 145
        expect(vm.issueLabel('twoIssues')).toEqual('issues');
        expect(vm.issueLabel('threeIssues')).toEqual('issues');
F
Fatih Acet 已提交
146 147 148
      });

      it('should return false if the given text has one issue', () => {
149
        expect(vm.issueLabel('oneIssue')).toEqual('issue');
F
Fatih Acet 已提交
150 151 152 153 154
      });
    });
  });

  describe('template', () => {
155 156 157 158
    it('should have only have closing issues text', (done) => {
      vm.relatedLinks = {
        closing: '<a href="#">#23</a> and <a>#42</a>',
      };
F
Fatih Acet 已提交
159

160 161 162
      Vue.nextTick()
      .then(() => {
        const content = vm.$el.textContent.replace(/\n(\s)+/g, ' ').trim();
F
Fatih Acet 已提交
163

164 165 166 167 168
        expect(content).toContain('Closes issues #23 and #42');
        expect(content).not.toContain('mentioned');
      })
      .then(done)
      .catch(done.fail);
F
Fatih Acet 已提交
169 170
    });

171 172 173 174 175 176 177 178 179 180 181 182 183 184
    it('should have only have mentioned issues text', (done) => {
      vm.relatedLinks = {
        mentioned: '<a href="#">#7</a>',
      };

      Vue.nextTick()
      .then(() => {
        expect(vm.$el.innerText).toContain('issue #7');
        expect(vm.$el.innerText).toContain('is mentioned but will not be closed');
        expect(vm.$el.innerText).not.toContain('Closes');
      })
      .then(done)
      .catch(done.fail);
    });
F
Fatih Acet 已提交
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    it('should have closing and mentioned issues at the same time', (done) => {
      vm.relatedLinks = {
        closing: '<a href="#">#7</a>',
        mentioned: '<a href="#">#23</a> and <a>#42</a>',
      };

      Vue.nextTick()
      .then(() => {
        const content = vm.$el.textContent.replace(/\n(\s)+/g, ' ').trim();

        expect(content).toContain('Closes issue #7.');
        expect(content).toContain('issues #23 and #42');
        expect(content).toContain('are mentioned but will not be closed');
      })
      .then(done)
      .catch(done.fail);
F
Fatih Acet 已提交
202 203
    });

204 205 206 207 208 209 210 211 212 213 214 215
    it('should have assing issues link', (done) => {
      vm.relatedLinks = {
        assignToMe: '<a href="#">Assign yourself to these issues</a>',
      };

      Vue.nextTick()
      .then(() => {
        expect(vm.$el.innerText).toContain('Assign yourself to these issues');
      })
      .then(done)
      .catch(done.fail);
    });
F
Fatih Acet 已提交
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    it('should use different wording after merging', (done) => {
      vm.isMerged = true;
      vm.relatedLinks = {
        closing: '<a href="#">#7</a>',
        mentioned: '<a href="#">#23</a> and <a>#42</a>',
      };

      Vue.nextTick()
      .then(() => {
        const content = vm.$el.textContent.replace(/\n(\s)+/g, ' ').trim();

        expect(content).toContain('Closed issue #7.');
        expect(content).toContain('issues #23 and #42');
        expect(content).toContain('are mentioned but were not closed');
      })
      .then(done)
      .catch(done.fail);
F
Fatih Acet 已提交
234 235 236
    });
  });
});