mr_widget_related_links_spec.js 4.1 KB
Newer Older
F
Fatih Acet 已提交
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
import Vue from 'vue';
import relatedLinksComponent from '~/vue_merge_request_widget/components/mr_widget_related_links';

const createComponent = (data) => {
  const Component = Vue.extend(relatedLinksComponent);

  return new Component({
    el: document.createElement('div'),
    propsData: data,
  });
};

describe('MRWidgetRelatedLinks', () => {
  describe('props', () => {
    it('should have props', () => {
      const { relatedLinks } = relatedLinksComponent.props;

      expect(relatedLinks).toBeDefined();
      expect(relatedLinks.type instanceof Object).toBeTruthy();
      expect(relatedLinks.required).toBeTruthy();
    });
  });

  describe('computed', () => {
    describe('hasLinks', () => {
      it('should return correct value when we have links reference', () => {
        const data = {
          relatedLinks: {
            closing: '/foo',
            mentioned: '/foo',
            assignToMe: '/foo',
          },
        };
        const vm = createComponent(data);
        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();
      });
    });
  });

  describe('methods', () => {
    const data = {
      relatedLinks: {
        closing: '<a href="#">#23</a> and <a>#42</a>',
        mentioned: '<a href="#">#7</a>',
      },
    };
    const vm = createComponent(data);

    describe('hasMultipleIssues', () => {
      it('should return true if the given text has multiple issues', () => {
        expect(vm.hasMultipleIssues(data.relatedLinks.closing)).toBeTruthy();
      });

      it('should return false if the given text has one issue', () => {
        expect(vm.hasMultipleIssues(data.relatedLinks.mentioned)).toBeFalsy();
      });
    });

    describe('issueLabel', () => {
      it('should return true if the given text has multiple issues', () => {
        expect(vm.issueLabel('closing')).toEqual('issues');
      });

      it('should return false if the given text has one issue', () => {
        expect(vm.issueLabel('mentioned')).toEqual('issue');
      });
    });

    describe('verbLabel', () => {
      it('should return true if the given text has multiple issues', () => {
        expect(vm.verbLabel('closing')).toEqual('are');
      });

      it('should return false if the given text has one issue', () => {
        expect(vm.verbLabel('mentioned')).toEqual('is');
      });
    });
  });

  describe('template', () => {
    it('should have only have closing issues text', () => {
      const vm = createComponent({
        relatedLinks: {
          closing: '<a href="#">#23</a> and <a>#42</a>',
        },
      });
      const content = vm.$el.textContent.replace(/\n(\s)+/g, ' ').trim();

      expect(content).toContain('Closes issues #23 and #42');
      expect(content).not.toContain('mentioned');
    });

    it('should have only have mentioned issues text', () => {
      const vm = createComponent({
        relatedLinks: {
          mentioned: '<a href="#">#7</a>',
        },
      });

      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');
    });

    it('should have closing and mentioned issues at the same time', () => {
      const vm = createComponent({
        relatedLinks: {
          closing: '<a href="#">#7</a>',
          mentioned: '<a href="#">#23</a> and <a>#42</a>',
        },
      });
      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.');
    });

    it('should have assing issues link', () => {
      const vm = createComponent({
        relatedLinks: {
          assignToMe: '<a href="#">Assign yourself to these issues</a>',
        },
      });

      expect(vm.$el.innerText).toContain('Assign yourself to these issues');
    });
  });
});