description_spec.js 4.9 KB
Newer Older
1 2
import Vue from 'vue';
import descriptionComponent from '~/issue_show/components/description.vue';
C
Clement Ho 已提交
3
import * as taskList from '~/task_list';
4
import mountComponent from 'spec/helpers/vue_mount_component_helper';
5 6 7

describe('Description component', () => {
  let vm;
C
Clement Ho 已提交
8 9 10 11 12 13 14 15 16
  let DescriptionComponent;
  const props = {
    canUpdate: true,
    descriptionHtml: 'test',
    descriptionText: 'test',
    updatedAt: new Date().toString(),
    taskStatus: '',
    updateUrl: gl.TEST_HOST,
  };
17 18

  beforeEach(() => {
C
Clement Ho 已提交
19
    DescriptionComponent = Vue.extend(descriptionComponent);
20 21 22 23

    if (!document.querySelector('.issuable-meta')) {
      const metaData = document.createElement('div');
      metaData.classList.add('issuable-meta');
24
      metaData.innerHTML = '<span id="task_status"></span><span id="task_status_short"></span>';
25 26 27 28

      document.body.appendChild(metaData);
    }

C
Clement Ho 已提交
29 30 31 32 33
    vm = mountComponent(DescriptionComponent, props);
  });

  afterEach(() => {
    vm.$destroy();
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  });

  it('animates description changes', (done) => {
    vm.descriptionHtml = 'changed';

    Vue.nextTick(() => {
      expect(
        vm.$el.querySelector('.wiki').classList.contains('issue-realtime-pre-pulse'),
      ).toBeTruthy();

      setTimeout(() => {
        expect(
          vm.$el.querySelector('.wiki').classList.contains('issue-realtime-trigger-pulse'),
        ).toBeTruthy();

        done();
      });
    });
  });

54 55 56
  it('opens recaptcha dialog if update rejected as spam', (done) => {
    let modal;
    const recaptchaChild = vm.$children
57
      .find(child => child.$options._componentTag === 'recaptcha-modal'); // eslint-disable-line no-underscore-dangle
58 59 60 61 62 63 64 65 66

    recaptchaChild.scriptSrc = '//scriptsrc';

    vm.taskListUpdateSuccess({
      recaptcha_html: '<div class="g-recaptcha">recaptcha_html</div>',
    });

    vm.$nextTick()
      .then(() => {
67
        modal = vm.$el.querySelector('.js-recaptcha-modal');
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

        expect(modal.style.display).not.toEqual('none');
        expect(modal.querySelector('.g-recaptcha').textContent).toEqual('recaptcha_html');
        expect(document.body.querySelector('.js-recaptcha-script').src).toMatch('//scriptsrc');
      })
      .then(() => modal.querySelector('.close').click())
      .then(() => vm.$nextTick())
      .then(() => {
        expect(modal.style.display).toEqual('none');
        expect(document.body.querySelector('.js-recaptcha-script')).toBeNull();
      })
      .then(done)
      .catch(done.fail);
  });

C
Clement Ho 已提交
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
  describe('TaskList', () => {
    beforeEach(() => {
      vm = mountComponent(DescriptionComponent, Object.assign({}, props, {
        issuableType: 'issuableType',
      }));
      spyOn(taskList, 'default');
    });

    it('re-inits the TaskList when description changed', (done) => {
      vm.descriptionHtml = 'changed';

      setTimeout(() => {
        expect(taskList.default).toHaveBeenCalled();
        done();
      });
    });

    it('does not re-init the TaskList when canUpdate is false', (done) => {
      vm.canUpdate = false;
      vm.descriptionHtml = 'changed';

      setTimeout(() => {
        expect(taskList.default).not.toHaveBeenCalled();
        done();
      });
    });

    it('calls with issuableType dataType', (done) => {
      vm.descriptionHtml = 'changed';

      setTimeout(() => {
        expect(taskList.default).toHaveBeenCalledWith({
          dataType: 'issuableType',
          fieldName: 'description',
          selector: '.detail-page-description',
118
          onSuccess: jasmine.any(Function),
C
Clement Ho 已提交
119 120 121 122 123
        });
        done();
      });
    });
  });
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

  describe('taskStatus', () => {
    it('adds full taskStatus', (done) => {
      vm.taskStatus = '1 of 1';

      setTimeout(() => {
        expect(
          document.querySelector('.issuable-meta #task_status').textContent.trim(),
        ).toBe('1 of 1');

        done();
      });
    });

    it('adds short taskStatus', (done) => {
      vm.taskStatus = '1 of 1';

      setTimeout(() => {
        expect(
          document.querySelector('.issuable-meta #task_status_short').textContent.trim(),
        ).toBe('1/1 task');

        done();
      });
    });
149 150 151 152 153 154 155 156 157 158 159 160

    it('clears task status text when no tasks are present', (done) => {
      vm.taskStatus = '0 of 0';

      setTimeout(() => {
        expect(
          document.querySelector('.issuable-meta #task_status').textContent.trim(),
        ).toBe('');

        done();
      });
    });
161
  });
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

  it('applies syntax highlighting and math when description changed', (done) => {
    spyOn(vm, 'renderGFM').and.callThrough();
    spyOn($.prototype, 'renderGFM').and.callThrough();
    vm.descriptionHtml = 'changed';

    Vue.nextTick(() => {
      setTimeout(() => {
        expect(vm.$refs['gfm-content']).toBeDefined();
        expect(vm.renderGFM).toHaveBeenCalled();
        expect($.prototype.renderGFM).toHaveBeenCalled();

        done();
      });
    });
  });
C
Clement Ho 已提交
178 179 180 181

  it('sets data-update-url', () => {
    expect(vm.$el.querySelector('textarea').dataset.updateUrl).toEqual(gl.TEST_HOST);
  });
182
});