loading.spec.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. it('customClass', done => {
  161. vm = createVue({
  162. template: `
  163. <div v-loading="loading" element-loading-custom-class="loading-custom-class"></div>
  164. `,
  165. data() {
  166. return {
  167. loading: true
  168. };
  169. }
  170. }, true);
  171. Vue.nextTick(() => {
  172. const mask = document.querySelector('.el-loading-mask');
  173. expect(mask.classList.contains('loading-custom-class')).to.true;
  174. done();
  175. });
  176. });
  177. });
  178. describe('as a service', () => {
  179. it('create', () => {
  180. loadingInstance = Loading();
  181. expect(document.querySelector('.el-loading-mask')).to.exist;
  182. });
  183. it('close', () => {
  184. loadingInstance = Loading();
  185. loadingInstance.close();
  186. expect(loadingInstance.visible).to.false;
  187. });
  188. it('target', done => {
  189. vm = createVue({
  190. template: `
  191. <div class="loading-container"></div>
  192. `
  193. }, true);
  194. loadingInstance = Loading({ target: '.loading-container' });
  195. let mask = document.querySelector('.el-loading-mask');
  196. let container = document.querySelector('.loading-container');
  197. expect(mask).to.exist;
  198. expect(mask.parentNode).to.equal(container);
  199. loadingInstance.close();
  200. setTimeout(() => {
  201. expect(getStyle(container, 'position')).to.equal('relative');
  202. done();
  203. }, 200);
  204. });
  205. it('body', () => {
  206. vm = createVue({
  207. template: `
  208. <div class="loading-container"></div>
  209. `
  210. }, true);
  211. loadingInstance = Loading({
  212. target: '.loading-container',
  213. body: true
  214. });
  215. let mask = document.querySelector('.el-loading-mask');
  216. expect(mask).to.exist;
  217. expect(mask.parentNode).to.equal(document.body);
  218. });
  219. it('fullscreen', () => {
  220. loadingInstance = Loading({ fullScreen: true });
  221. const mask = document.querySelector('.el-loading-mask');
  222. expect(mask.parentNode).to.equal(document.body);
  223. expect(mask.classList.contains('is-fullscreen')).to.true;
  224. });
  225. it('fullscreen singleton', done => {
  226. loadingInstance = Loading({ fullScreen: true });
  227. setTimeout(() => {
  228. loadingInstance2 = Loading({ fullScreen: true });
  229. setTimeout(() => {
  230. let masks = document.querySelectorAll('.el-loading-mask');
  231. expect(masks.length).to.equal(1);
  232. loadingInstance2.close();
  233. setTimeout(() => {
  234. masks = document.querySelectorAll('.el-loading-mask');
  235. expect(masks.length).to.equal(0);
  236. done();
  237. }, 350);
  238. }, 10);
  239. }, 10);
  240. });
  241. it('lock', () => {
  242. loadingInstance = Loading({ lock: true });
  243. expect(getStyle(document.body, 'overflow')).to.equal('hidden');
  244. });
  245. it('text', () => {
  246. loadingInstance = Loading({ text: 'Loading...' });
  247. const text = document.querySelector('.el-loading-text');
  248. expect(text).to.exist;
  249. expect(text.textContent).to.equal('Loading...');
  250. });
  251. it('customClass', () => {
  252. loadingInstance = Loading({ customClass: 'el-loading-custom-class' });
  253. const customClass = document.querySelector('.el-loading-custom-class');
  254. expect(customClass).to.exist;
  255. });
  256. });
  257. });