message-box.spec.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import MessageBox from 'packages/message-box';
  2. describe('MessageBox', () => {
  3. let hasPromise = true;
  4. before(() => {
  5. if (!window.Promise) {
  6. hasPromise = false;
  7. window.Promise = require('es6-promise').Promise;
  8. }
  9. });
  10. after(() => {
  11. if (!hasPromise) {
  12. window.Promise = undefined;
  13. }
  14. });
  15. afterEach(() => {
  16. const el = document.querySelector('.el-message-box__wrapper');
  17. if (!el) return;
  18. if (el.parentNode) {
  19. el.parentNode.removeChild(el);
  20. }
  21. MessageBox.close();
  22. });
  23. it('create and close', done => {
  24. MessageBox({
  25. type: 'success',
  26. title: '消息',
  27. message: '这是一段内容'
  28. });
  29. setTimeout(() => {
  30. const msgbox = document.querySelector('.el-message-box__wrapper');
  31. expect(msgbox.__vue__.$parent.visible).to.true;
  32. expect(msgbox.querySelector('.el-message-box__title').textContent).to.equal('消息');
  33. expect(msgbox.querySelector('.el-message-box__message')
  34. .querySelector('p').textContent).to.equal('这是一段内容');
  35. MessageBox.close();
  36. expect(msgbox.__vue__.$parent.visible).to.false;
  37. done();
  38. }, 300);
  39. });
  40. it('invoke with strings', done => {
  41. MessageBox('消息', '这是一段内容', 'success');
  42. setTimeout(() => {
  43. expect(document.querySelector('.el-message-box__wrapper')).to.exist;
  44. done();
  45. }, 300);
  46. });
  47. it('html string', done => {
  48. MessageBox({
  49. title: 'html string',
  50. dangerouslyUseHTMLString: true,
  51. message: '<strong>html string</strong>'
  52. });
  53. setTimeout(() => {
  54. const message = document.querySelector('.el-message-box__message strong');
  55. expect(message.textContent).to.equal('html string');
  56. done();
  57. }, 300);
  58. });
  59. it('alert', done => {
  60. MessageBox.alert('这是一段内容', {
  61. title: '标题名称',
  62. type: 'warning'
  63. });
  64. setTimeout(() => {
  65. document.querySelector('.v-modal').click();
  66. expect(document.querySelector('.el-message-box__wrapper')
  67. .__vue__.$parent.visible).to.true;
  68. expect(document.querySelector('.el-message-box__wrapper')
  69. .__vue__.$parent.type).to.equal('warning');
  70. done();
  71. }, 300);
  72. });
  73. it('confirm', done => {
  74. MessageBox.confirm('这是一段内容', {
  75. title: '标题名称',
  76. type: 'warning'
  77. });
  78. setTimeout(() => {
  79. document.querySelector('.el-message-box__wrapper')
  80. .querySelector('.el-button--primary').click();
  81. expect(document.querySelector('.el-message-box__wrapper')
  82. .__vue__.$parent.visible).to.false;
  83. done();
  84. }, 200);
  85. });
  86. it('prompt', done => {
  87. MessageBox.prompt('这是一段内容', {
  88. title: '标题名称',
  89. inputPattern: /test/,
  90. inputErrorMessage: 'validation failed'
  91. });
  92. setTimeout(() => {
  93. expect(document.querySelector('.el-message-box__input')).to.exist;
  94. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  95. messageBox.inputValue = 'no';
  96. setTimeout(() => {
  97. expect(document.querySelector('.el-message-box__errormsg')
  98. .textContent).to.equal('validation failed');
  99. done();
  100. }, 100);
  101. }, 200);
  102. });
  103. describe('custom validator', () => {
  104. it('returns a string', done => {
  105. const validator = value => {
  106. if (value !== 'test') return 'wrong';
  107. return true;
  108. };
  109. MessageBox.prompt('这是一段内容', {
  110. title: '标题名称',
  111. inputValidator: validator
  112. });
  113. setTimeout(() => {
  114. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  115. messageBox.inputValue = 'no';
  116. setTimeout(() => {
  117. expect(document.querySelector('.el-message-box__errormsg')
  118. .textContent).to.equal('wrong');
  119. messageBox.inputValue = 'test';
  120. setTimeout(() => {
  121. expect(document.querySelector('.el-message-box__errormsg')
  122. .textContent).to.equal('');
  123. done();
  124. }, 100);
  125. }, 100);
  126. }, 200);
  127. });
  128. it('returns false', done => {
  129. const validator = value => false;
  130. MessageBox.prompt('这是一段内容', {
  131. title: '标题名称',
  132. inputValidator: validator
  133. });
  134. setTimeout(() => {
  135. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  136. messageBox.inputValue = 'no';
  137. setTimeout(() => {
  138. expect(document.querySelector('.el-message-box__errormsg')
  139. .textContent).to.equal('输入的数据不合法!');
  140. done();
  141. }, 100);
  142. }, 200);
  143. });
  144. });
  145. it('callback', done => {
  146. let msgAction = '';
  147. MessageBox({
  148. title: '消息',
  149. message: '这是一段内容'
  150. }, action => {
  151. msgAction = action;
  152. });
  153. setTimeout(() => {
  154. document.querySelector('.el-message-box__close').click();
  155. setTimeout(() => {
  156. expect(msgAction).to.equal('cancel');
  157. done();
  158. }, 10);
  159. }, 10);
  160. });
  161. it('beforeClose', done => {
  162. let msgAction = '';
  163. MessageBox({
  164. title: '消息',
  165. message: '这是一段内容',
  166. beforeClose: (action, instance) => {
  167. instance.close();
  168. }
  169. }, action => {
  170. msgAction = action;
  171. });
  172. setTimeout(() => {
  173. document.querySelector('.el-message-box__wrapper .el-button--primary').click();
  174. setTimeout(() => {
  175. expect(msgAction).to.equal('confirm');
  176. done();
  177. }, 10);
  178. }, 10);
  179. });
  180. describe('promise', () => {
  181. it('resolve', done => {
  182. MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
  183. .then(action => {
  184. expect(action).to.equal('confirm');
  185. done();
  186. });
  187. setTimeout(() => {
  188. document.querySelector('.el-message-box__wrapper .el-button--primary').click();
  189. }, 50);
  190. });
  191. it('reject', done => {
  192. MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
  193. .catch(action => {
  194. expect(action).to.equal('cancel');
  195. done();
  196. });
  197. setTimeout(() => {
  198. document.querySelector('.el-message-box__wrapper .el-button').click();
  199. }, 50);
  200. });
  201. });
  202. });