PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/javascripts/vue_mr_widget/components/mr_widget_deployment_spec.js

https://gitlab.com/jlogandavison/gitlab-ce
JavaScript | 188 lines | 162 code | 26 blank | 0 comment | 1 complexity | 4c642cbb1cdc7b63d206348b48e593cc MD5 | raw file
  1. import Vue from 'vue';
  2. import deploymentComponent from '~/vue_merge_request_widget/components/mr_widget_deployment';
  3. import MRWidgetService from '~/vue_merge_request_widget/services/mr_widget_service';
  4. import { statusIconEntityMap } from '~/vue_shared/ci_status_icons';
  5. const deploymentMockData = [
  6. {
  7. id: 15,
  8. name: 'review/diplo',
  9. url: '/root/acets-review-apps/environments/15',
  10. stop_url: '/root/acets-review-apps/environments/15/stop',
  11. metrics_url: '/root/acets-review-apps/environments/15/deployments/1/metrics',
  12. external_url: 'http://diplo.',
  13. external_url_formatted: 'diplo.',
  14. deployed_at: '2017-03-22T22:44:42.258Z',
  15. deployed_at_formatted: 'Mar 22, 2017 10:44pm',
  16. },
  17. ];
  18. const createComponent = () => {
  19. const Component = Vue.extend(deploymentComponent);
  20. const mr = {
  21. deployments: deploymentMockData,
  22. };
  23. const service = {};
  24. return new Component({
  25. el: document.createElement('div'),
  26. propsData: { mr, service },
  27. });
  28. };
  29. describe('MRWidgetDeployment', () => {
  30. describe('props', () => {
  31. it('should have props', () => {
  32. const { mr, service } = deploymentComponent.props;
  33. expect(mr.type instanceof Object).toBeTruthy();
  34. expect(mr.required).toBeTruthy();
  35. expect(service.type instanceof Object).toBeTruthy();
  36. expect(service.required).toBeTruthy();
  37. });
  38. });
  39. describe('computed', () => {
  40. describe('svg', () => {
  41. it('should have the proper SVG icon', () => {
  42. const vm = createComponent(deploymentMockData);
  43. expect(vm.svg).toEqual(statusIconEntityMap.icon_status_success);
  44. });
  45. });
  46. });
  47. describe('methods', () => {
  48. let vm = createComponent();
  49. const deployment = deploymentMockData[0];
  50. describe('formatDate', () => {
  51. it('should work', () => {
  52. const readable = gl.utils.getTimeago().format(deployment.deployed_at);
  53. expect(vm.formatDate(deployment.deployed_at)).toEqual(readable);
  54. });
  55. });
  56. describe('hasExternalUrls', () => {
  57. it('should return true', () => {
  58. expect(vm.hasExternalUrls(deployment)).toBeTruthy();
  59. });
  60. it('should return false when there is not enough information', () => {
  61. expect(vm.hasExternalUrls()).toBeFalsy();
  62. expect(vm.hasExternalUrls({ external_url: 'Diplo' })).toBeFalsy();
  63. expect(vm.hasExternalUrls({ external_url_formatted: 'Diplo' })).toBeFalsy();
  64. });
  65. });
  66. describe('hasDeploymentTime', () => {
  67. it('should return true', () => {
  68. expect(vm.hasDeploymentTime(deployment)).toBeTruthy();
  69. });
  70. it('should return false when there is not enough information', () => {
  71. expect(vm.hasDeploymentTime()).toBeFalsy();
  72. expect(vm.hasDeploymentTime({ deployed_at: 'Diplo' })).toBeFalsy();
  73. expect(vm.hasDeploymentTime({ deployed_at_formatted: 'Diplo' })).toBeFalsy();
  74. });
  75. });
  76. describe('hasDeploymentMeta', () => {
  77. it('should return true', () => {
  78. expect(vm.hasDeploymentMeta(deployment)).toBeTruthy();
  79. });
  80. it('should return false when there is not enough information', () => {
  81. expect(vm.hasDeploymentMeta()).toBeFalsy();
  82. expect(vm.hasDeploymentMeta({ url: 'Diplo' })).toBeFalsy();
  83. expect(vm.hasDeploymentMeta({ name: 'Diplo' })).toBeFalsy();
  84. });
  85. });
  86. describe('stopEnvironment', () => {
  87. const url = '/foo/bar';
  88. const returnPromise = () => new Promise((resolve) => {
  89. resolve({
  90. json() {
  91. return {
  92. redirect_url: url,
  93. };
  94. },
  95. });
  96. });
  97. const mockStopEnvironment = () => {
  98. vm.stopEnvironment(deploymentMockData);
  99. return vm;
  100. };
  101. it('should show a confirm dialog and call service.stopEnvironment when confirmed', (done) => {
  102. spyOn(window, 'confirm').and.returnValue(true);
  103. spyOn(MRWidgetService, 'stopEnvironment').and.returnValue(returnPromise(true));
  104. spyOn(gl.utils, 'visitUrl').and.returnValue(true);
  105. vm = mockStopEnvironment();
  106. expect(window.confirm).toHaveBeenCalled();
  107. expect(MRWidgetService.stopEnvironment).toHaveBeenCalledWith(deploymentMockData.stop_url);
  108. setTimeout(() => {
  109. expect(gl.utils.visitUrl).toHaveBeenCalledWith(url);
  110. done();
  111. }, 333);
  112. });
  113. it('should show a confirm dialog but should not work if the dialog is rejected', () => {
  114. spyOn(window, 'confirm').and.returnValue(false);
  115. spyOn(MRWidgetService, 'stopEnvironment').and.returnValue(returnPromise(false));
  116. vm = mockStopEnvironment();
  117. expect(window.confirm).toHaveBeenCalled();
  118. expect(MRWidgetService.stopEnvironment).not.toHaveBeenCalled();
  119. });
  120. });
  121. });
  122. describe('template', () => {
  123. let vm;
  124. let el;
  125. const [deployment] = deploymentMockData;
  126. beforeEach(() => {
  127. vm = createComponent(deploymentMockData);
  128. el = vm.$el;
  129. });
  130. it('should render template elements correctly', () => {
  131. expect(el.classList.contains('mr-widget-heading')).toBeTruthy();
  132. expect(el.querySelector('.js-icon-link')).toBeDefined();
  133. expect(el.querySelector('.js-deploy-meta').getAttribute('href')).toEqual(deployment.url);
  134. expect(el.querySelector('.js-deploy-meta').innerText).toContain(deployment.name);
  135. expect(el.querySelector('.js-deploy-url').getAttribute('href')).toEqual(deployment.external_url);
  136. expect(el.querySelector('.js-deploy-url').innerText).toContain(deployment.external_url_formatted);
  137. expect(el.querySelector('.js-deploy-time').innerText).toContain(vm.formatDate(deployment.deployed_at));
  138. expect(el.querySelector('.js-mr-memory-usage')).toBeDefined();
  139. expect(el.querySelector('button')).toBeDefined();
  140. });
  141. it('should list multiple deployments', (done) => {
  142. vm.mr.deployments.push(deployment);
  143. vm.mr.deployments.push(deployment);
  144. Vue.nextTick(() => {
  145. expect(el.querySelectorAll('.ci-widget').length).toEqual(3);
  146. expect(el.querySelectorAll('.js-mr-memory-usage').length).toEqual(3);
  147. done();
  148. });
  149. });
  150. it('should not have some elements when there is not enough data', (done) => {
  151. vm.mr.deployments = [{}];
  152. Vue.nextTick(() => {
  153. expect(el.querySelectorAll('.js-deploy-meta').length).toEqual(0);
  154. expect(el.querySelectorAll('.js-deploy-url').length).toEqual(0);
  155. expect(el.querySelectorAll('.js-deploy-time').length).toEqual(0);
  156. expect(el.querySelectorAll('.js-mr-memory-usage').length).toEqual(0);
  157. expect(el.querySelectorAll('.button').length).toEqual(0);
  158. done();
  159. });
  160. });
  161. });
  162. });