message-box.spec.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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('custom icon', done => {
  48. MessageBox({
  49. type: 'warning',
  50. iconClass: 'el-icon-question',
  51. message: '这是一段内容'
  52. });
  53. setTimeout(() => {
  54. const icon = document.querySelector('.el-message-box__status');
  55. expect(icon.classList.contains('el-icon-question')).to.true;
  56. done();
  57. }, 300);
  58. });
  59. it('html string', done => {
  60. MessageBox({
  61. title: 'html string',
  62. dangerouslyUseHTMLString: true,
  63. message: '<strong>html string</strong>'
  64. });
  65. setTimeout(() => {
  66. const message = document.querySelector('.el-message-box__message strong');
  67. expect(message.textContent).to.equal('html string');
  68. done();
  69. }, 300);
  70. });
  71. it('alert', done => {
  72. MessageBox.alert('这是一段内容', {
  73. title: '标题名称',
  74. type: 'warning'
  75. });
  76. setTimeout(() => {
  77. document.querySelector('.v-modal').click();
  78. expect(document.querySelector('.el-message-box__wrapper')
  79. .__vue__.$parent.visible).to.true;
  80. expect(document.querySelector('.el-message-box__wrapper')
  81. .__vue__.$parent.type).to.equal('warning');
  82. done();
  83. }, 300);
  84. });
  85. it('confirm', done => {
  86. MessageBox.confirm('这是一段内容', {
  87. title: '标题名称',
  88. type: 'warning'
  89. });
  90. setTimeout(() => {
  91. document.querySelector('.el-message-box__wrapper')
  92. .querySelector('.el-button--primary').click();
  93. expect(document.querySelector('.el-message-box__wrapper')
  94. .__vue__.$parent.visible).to.false;
  95. done();
  96. }, 200);
  97. });
  98. it('prompt', done => {
  99. MessageBox.prompt('这是一段内容', {
  100. title: '标题名称',
  101. inputPattern: /test/,
  102. inputErrorMessage: 'validation failed'
  103. });
  104. setTimeout(() => {
  105. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  106. expect(messageBox.$el.querySelector('.el-message-box__input')).to.exist;
  107. const haveFocus = messageBox.$el.querySelector('input').isSameNode(document.activeElement);
  108. expect(haveFocus).to.true;
  109. messageBox.inputValue = 'no';
  110. setTimeout(() => {
  111. expect(messageBox.$el.querySelector('.el-message-box__errormsg')
  112. .textContent).to.equal('validation failed');
  113. done();
  114. }, 100);
  115. }, 700);
  116. });
  117. it('prompt: focus on textarea', done => {
  118. MessageBox.prompt('这是一段内容', {
  119. inputType: 'textarea',
  120. title: '标题名称'
  121. });
  122. setTimeout(() => {
  123. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  124. const haveFocus = messageBox.$el.querySelector('textarea').isSameNode(document.activeElement);
  125. expect(haveFocus).to.true;
  126. done();
  127. }, 700);
  128. });
  129. describe('custom validator', () => {
  130. it('returns a string', done => {
  131. const validator = value => {
  132. if (value !== 'test') return 'wrong';
  133. return true;
  134. };
  135. MessageBox.prompt('这是一段内容', {
  136. title: '标题名称',
  137. inputValidator: validator
  138. });
  139. setTimeout(() => {
  140. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  141. messageBox.inputValue = 'no';
  142. setTimeout(() => {
  143. expect(document.querySelector('.el-message-box__errormsg')
  144. .textContent).to.equal('wrong');
  145. messageBox.inputValue = 'test';
  146. setTimeout(() => {
  147. expect(document.querySelector('.el-message-box__errormsg')
  148. .textContent).to.equal('');
  149. done();
  150. }, 100);
  151. }, 100);
  152. }, 200);
  153. });
  154. it('returns false', done => {
  155. const validator = value => false;
  156. MessageBox.prompt('这是一段内容', {
  157. title: '标题名称',
  158. inputValidator: validator
  159. });
  160. setTimeout(() => {
  161. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  162. messageBox.inputValue = 'no';
  163. setTimeout(() => {
  164. expect(document.querySelector('.el-message-box__errormsg')
  165. .textContent).to.equal('输入的数据不合法!');
  166. done();
  167. }, 100);
  168. }, 200);
  169. });
  170. });
  171. it('callback', done => {
  172. let msgAction = '';
  173. MessageBox({
  174. title: '消息',
  175. message: '这是一段内容'
  176. }, action => {
  177. msgAction = action;
  178. });
  179. setTimeout(() => {
  180. document.querySelector('.el-message-box__close').click();
  181. setTimeout(() => {
  182. expect(msgAction).to.equal('cancel');
  183. done();
  184. }, 10);
  185. }, 10);
  186. });
  187. it('beforeClose', done => {
  188. let msgAction = '';
  189. MessageBox({
  190. title: '消息',
  191. message: '这是一段内容',
  192. beforeClose: (action, instance) => {
  193. instance.close();
  194. }
  195. }, action => {
  196. msgAction = action;
  197. });
  198. setTimeout(() => {
  199. document.querySelector('.el-message-box__wrapper .el-button--primary').click();
  200. setTimeout(() => {
  201. expect(msgAction).to.equal('confirm');
  202. done();
  203. }, 10);
  204. }, 10);
  205. });
  206. describe('promise', () => {
  207. it('resolve', done => {
  208. MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
  209. .then(action => {
  210. expect(action).to.equal('confirm');
  211. done();
  212. });
  213. setTimeout(() => {
  214. document.querySelector('.el-message-box__wrapper .el-button--primary').click();
  215. }, 50);
  216. });
  217. it('reject', done => {
  218. MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
  219. .catch(action => {
  220. expect(action).to.equal('cancel');
  221. done();
  222. });
  223. setTimeout(() => {
  224. document.querySelector('.el-message-box__wrapper .el-button').click();
  225. }, 50);
  226. });
  227. });
  228. });