loading.spec.js 5.5 KB

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