loading.spec.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { getStyle } from '../../../src/utils/dom';
  2. import { createVue, destroyVM } from '../util';
  3. import Vue from 'vue';
  4. import LoadingRaw from 'packages/loading';
  5. const Loading = LoadingRaw.service;
  6. describe('Loading', () => {
  7. let vm, loadingInstance, loadingInstance2;
  8. afterEach(() => {
  9. destroyVM(vm);
  10. if (loadingInstance) {
  11. loadingInstance.close();
  12. loadingInstance.$el &&
  13. loadingInstance.$el.parentNode &&
  14. loadingInstance.$el.parentNode.removeChild(loadingInstance.$el);
  15. }
  16. if (loadingInstance2) {
  17. loadingInstance2.close();
  18. loadingInstance2.$el &&
  19. loadingInstance2.$el.parentNode &&
  20. loadingInstance2.$el.parentNode.removeChild(loadingInstance2.$el);
  21. }
  22. });
  23. describe('as a directive', () => {
  24. it('create', done => {
  25. vm = createVue({
  26. template: `
  27. <div v-loading="loading"></div>
  28. `,
  29. data() {
  30. return {
  31. loading: true
  32. };
  33. }
  34. });
  35. Vue.nextTick(() => {
  36. const mask = vm.$el.querySelector('.el-loading-mask');
  37. expect(mask).to.exist;
  38. vm.loading = false;
  39. setTimeout(() => {
  40. expect(mask.style.display).to.equal('none');
  41. done();
  42. }, 100);
  43. });
  44. });
  45. it('unbind', done => {
  46. const vm1 = createVue({
  47. template: `
  48. <div v-if="show" v-loading="loading"></div>
  49. `,
  50. data() {
  51. return {
  52. show: true,
  53. loading: true
  54. };
  55. }
  56. });
  57. const vm2 = createVue({
  58. template: `
  59. <div v-if="show" v-loading.body="loading"></div>
  60. `,
  61. data() {
  62. return {
  63. show: true,
  64. loading: true
  65. };
  66. }
  67. });
  68. Vue.nextTick(() => {
  69. vm1.loading = false;
  70. vm2.loading = false;
  71. Vue.nextTick(() => {
  72. vm1.show = false;
  73. vm2.show = false;
  74. Vue.nextTick(() => {
  75. expect(document.querySelector('.el-loading-mask')).to.not.exist;
  76. done();
  77. });
  78. });
  79. });
  80. });
  81. it('body', done => {
  82. vm = createVue({
  83. template: `
  84. <div v-loading.body="loading"></div>
  85. `,
  86. data() {
  87. return {
  88. loading: true
  89. };
  90. }
  91. }, true);
  92. Vue.nextTick(() => {
  93. const mask = document.querySelector('.el-loading-mask');
  94. expect(mask.parentNode === document.body).to.true;
  95. vm.loading = false;
  96. document.body.removeChild(mask);
  97. document.body.removeChild(vm.$el);
  98. done();
  99. });
  100. });
  101. it('fullscreen', done => {
  102. vm = createVue({
  103. template: `
  104. <div v-loading.fullscreen="loading"></div>
  105. `,
  106. data() {
  107. return {
  108. loading: true
  109. };
  110. }
  111. }, true);
  112. Vue.nextTick(() => {
  113. const mask = document.querySelector('.el-loading-mask');
  114. expect(mask.parentNode === document.body).to.true;
  115. expect(mask.classList.contains('is-fullscreen')).to.true;
  116. vm.loading = false;
  117. document.body.removeChild(mask);
  118. document.body.removeChild(vm.$el);
  119. done();
  120. });
  121. });
  122. it('lock', done => {
  123. vm = createVue({
  124. template: `
  125. <div v-loading.fullscreen.lock="loading"></div>
  126. `,
  127. data() {
  128. return {
  129. loading: true
  130. };
  131. }
  132. }, true);
  133. Vue.nextTick(() => {
  134. expect(getStyle(document.body, 'overflow')).to.equal('hidden');
  135. vm.loading = false;
  136. document.body.removeChild(document.querySelector('.el-loading-mask'));
  137. document.body.removeChild(vm.$el);
  138. done();
  139. });
  140. });
  141. it('text', done => {
  142. vm = createVue({
  143. template: `
  144. <div v-loading="loading" element-loading-text="拼命加载中"></div>
  145. `,
  146. data() {
  147. return {
  148. loading: true
  149. };
  150. }
  151. }, true);
  152. Vue.nextTick(() => {
  153. const mask = document.querySelector('.el-loading-mask');
  154. const text = mask.querySelector('.el-loading-text');
  155. expect(text).to.exist;
  156. expect(text.textContent).to.equal('拼命加载中');
  157. done();
  158. });
  159. });
  160. });
  161. describe('as a service', () => {
  162. it('create', () => {
  163. loadingInstance = Loading();
  164. expect(document.querySelector('.el-loading-mask')).to.exist;
  165. });
  166. it('close', () => {
  167. loadingInstance = Loading();
  168. loadingInstance.close();
  169. expect(loadingInstance.visible).to.false;
  170. });
  171. it('target', done => {
  172. vm = createVue({
  173. template: `
  174. <div class="loading-container"></div>
  175. `
  176. }, true);
  177. loadingInstance = Loading({ target: '.loading-container' });
  178. let mask = document.querySelector('.el-loading-mask');
  179. let container = document.querySelector('.loading-container');
  180. expect(mask).to.exist;
  181. expect(mask.parentNode).to.equal(container);
  182. loadingInstance.close();
  183. setTimeout(() => {
  184. expect(getStyle(container, 'position')).to.equal('relative');
  185. done();
  186. }, 200);
  187. });
  188. it('body', () => {
  189. vm = createVue({
  190. template: `
  191. <div class="loading-container"></div>
  192. `
  193. }, true);
  194. loadingInstance = Loading({
  195. target: '.loading-container',
  196. body: true
  197. });
  198. let mask = document.querySelector('.el-loading-mask');
  199. expect(mask).to.exist;
  200. expect(mask.parentNode).to.equal(document.body);
  201. });
  202. it('fullscreen', () => {
  203. loadingInstance = Loading({ fullScreen: true });
  204. const mask = document.querySelector('.el-loading-mask');
  205. expect(mask.parentNode).to.equal(document.body);
  206. expect(mask.classList.contains('is-fullscreen')).to.true;
  207. });
  208. it('fullscreen singleton', done => {
  209. loadingInstance = Loading({ fullScreen: true });
  210. setTimeout(() => {
  211. loadingInstance2 = Loading({ fullScreen: true });
  212. setTimeout(() => {
  213. let masks = document.querySelectorAll('.el-loading-mask');
  214. expect(masks.length).to.equal(1);
  215. loadingInstance2.close();
  216. setTimeout(() => {
  217. masks = document.querySelectorAll('.el-loading-mask');
  218. expect(masks.length).to.equal(0);
  219. done();
  220. }, 350);
  221. }, 10);
  222. }, 10);
  223. });
  224. it('lock', () => {
  225. loadingInstance = Loading({ lock: true });
  226. expect(getStyle(document.body, 'overflow')).to.equal('hidden');
  227. });
  228. it('text', () => {
  229. loadingInstance = Loading({ text: 'Loading...' });
  230. const text = document.querySelector('.el-loading-text');
  231. expect(text).to.exist;
  232. expect(text.textContent).to.equal('Loading...');
  233. });
  234. });
  235. });