loading.spec.js 6.1 KB

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