message-box.spec.js 7.5 KB

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