PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/javascripts/issue_show/components/description_spec.js

https://gitlab.com/wolfgang42/gitlab-ce
JavaScript | 210 lines | 168 code | 41 blank | 1 comment | 2 complexity | b599a664cddf685e399f5af7dc192616 MD5 | raw file
  1. import $ from 'jquery';
  2. import Vue from 'vue';
  3. import '~/behaviors/markdown/render_gfm';
  4. import Description from '~/issue_show/components/description.vue';
  5. import mountComponent from 'spec/helpers/vue_mount_component_helper';
  6. describe('Description component', () => {
  7. let vm;
  8. let DescriptionComponent;
  9. const props = {
  10. canUpdate: true,
  11. descriptionHtml: 'test',
  12. descriptionText: 'test',
  13. updatedAt: new Date().toString(),
  14. taskStatus: '',
  15. updateUrl: gl.TEST_HOST,
  16. };
  17. beforeEach(() => {
  18. DescriptionComponent = Vue.extend(Description);
  19. if (!document.querySelector('.issuable-meta')) {
  20. const metaData = document.createElement('div');
  21. metaData.classList.add('issuable-meta');
  22. metaData.innerHTML =
  23. '<div class="flash-container"></div><span id="task_status"></span><span id="task_status_short"></span>';
  24. document.body.appendChild(metaData);
  25. }
  26. vm = mountComponent(DescriptionComponent, props);
  27. });
  28. afterEach(() => {
  29. vm.$destroy();
  30. });
  31. afterAll(() => {
  32. $('.issuable-meta .flash-container').remove();
  33. });
  34. it('animates description changes', done => {
  35. vm.descriptionHtml = 'changed';
  36. Vue.nextTick(() => {
  37. expect(
  38. vm.$el.querySelector('.md').classList.contains('issue-realtime-pre-pulse'),
  39. ).toBeTruthy();
  40. setTimeout(() => {
  41. expect(
  42. vm.$el.querySelector('.md').classList.contains('issue-realtime-trigger-pulse'),
  43. ).toBeTruthy();
  44. done();
  45. });
  46. });
  47. });
  48. it('opens recaptcha dialog if update rejected as spam', done => {
  49. let modal;
  50. const recaptchaChild = vm.$children.find(
  51. // eslint-disable-next-line no-underscore-dangle
  52. child => child.$options._componentTag === 'recaptcha-modal',
  53. );
  54. recaptchaChild.scriptSrc = '//scriptsrc';
  55. vm.taskListUpdateSuccess({
  56. recaptcha_html: '<div class="g-recaptcha">recaptcha_html</div>',
  57. });
  58. vm.$nextTick()
  59. .then(() => {
  60. modal = vm.$el.querySelector('.js-recaptcha-modal');
  61. expect(modal.style.display).not.toEqual('none');
  62. expect(modal.querySelector('.g-recaptcha').textContent).toEqual('recaptcha_html');
  63. expect(document.body.querySelector('.js-recaptcha-script').src).toMatch('//scriptsrc');
  64. })
  65. .then(() => modal.querySelector('.close').click())
  66. .then(() => vm.$nextTick())
  67. .then(() => {
  68. expect(modal.style.display).toEqual('none');
  69. expect(document.body.querySelector('.js-recaptcha-script')).toBeNull();
  70. })
  71. .then(done)
  72. .catch(done.fail);
  73. });
  74. describe('TaskList', () => {
  75. let TaskList;
  76. beforeEach(() => {
  77. vm.$destroy();
  78. vm = mountComponent(
  79. DescriptionComponent,
  80. Object.assign({}, props, {
  81. issuableType: 'issuableType',
  82. }),
  83. );
  84. TaskList = spyOnDependency(Description, 'TaskList');
  85. });
  86. it('re-inits the TaskList when description changed', done => {
  87. vm.descriptionHtml = 'changed';
  88. setTimeout(() => {
  89. expect(TaskList).toHaveBeenCalled();
  90. done();
  91. });
  92. });
  93. it('does not re-init the TaskList when canUpdate is false', done => {
  94. vm.canUpdate = false;
  95. vm.descriptionHtml = 'changed';
  96. setTimeout(() => {
  97. expect(TaskList).not.toHaveBeenCalled();
  98. done();
  99. });
  100. });
  101. it('calls with issuableType dataType', done => {
  102. vm.descriptionHtml = 'changed';
  103. setTimeout(() => {
  104. expect(TaskList).toHaveBeenCalledWith({
  105. dataType: 'issuableType',
  106. fieldName: 'description',
  107. selector: '.detail-page-description',
  108. onSuccess: jasmine.any(Function),
  109. onError: jasmine.any(Function),
  110. lockVersion: 0,
  111. });
  112. done();
  113. });
  114. });
  115. });
  116. describe('taskStatus', () => {
  117. it('adds full taskStatus', done => {
  118. vm.taskStatus = '1 of 1';
  119. setTimeout(() => {
  120. expect(document.querySelector('.issuable-meta #task_status').textContent.trim()).toBe(
  121. '1 of 1',
  122. );
  123. done();
  124. });
  125. });
  126. it('adds short taskStatus', done => {
  127. vm.taskStatus = '1 of 1';
  128. setTimeout(() => {
  129. expect(document.querySelector('.issuable-meta #task_status_short').textContent.trim()).toBe(
  130. '1/1 task',
  131. );
  132. done();
  133. });
  134. });
  135. it('clears task status text when no tasks are present', done => {
  136. vm.taskStatus = '0 of 0';
  137. setTimeout(() => {
  138. expect(document.querySelector('.issuable-meta #task_status').textContent.trim()).toBe('');
  139. done();
  140. });
  141. });
  142. });
  143. it('applies syntax highlighting and math when description changed', done => {
  144. spyOn(vm, 'renderGFM').and.callThrough();
  145. spyOn($.prototype, 'renderGFM').and.callThrough();
  146. vm.descriptionHtml = 'changed';
  147. Vue.nextTick(() => {
  148. setTimeout(() => {
  149. expect(vm.$refs['gfm-content']).toBeDefined();
  150. expect(vm.renderGFM).toHaveBeenCalled();
  151. expect($.prototype.renderGFM).toHaveBeenCalled();
  152. done();
  153. });
  154. });
  155. });
  156. it('sets data-update-url', () => {
  157. expect(vm.$el.querySelector('textarea').dataset.updateUrl).toEqual(gl.TEST_HOST);
  158. });
  159. describe('taskListUpdateError', () => {
  160. it('should create flash notification and emit an event to parent', () => {
  161. const msg =
  162. 'Someone edited this issue at the same time you did. The description has been updated and you will need to make your changes again.';
  163. spyOn(vm, '$emit');
  164. vm.taskListUpdateError();
  165. expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(msg);
  166. expect(vm.$emit).toHaveBeenCalledWith('taskListUpdateFailed');
  167. });
  168. });
  169. });