message-box.spec.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  94. expect(messageBox.$el.querySelector('.el-message-box__input')).to.exist;
  95. const haveFocus = messageBox.$el.querySelector('input').isSameNode(document.activeElement);
  96. expect(haveFocus).to.true;
  97. messageBox.inputValue = 'no';
  98. setTimeout(() => {
  99. expect(messageBox.$el.querySelector('.el-message-box__errormsg')
  100. .textContent).to.equal('validation failed');
  101. done();
  102. }, 100);
  103. }, 700);
  104. });
  105. it('prompt: focus on textarea', done => {
  106. MessageBox.prompt('这是一段内容', {
  107. inputType: 'textarea',
  108. title: '标题名称'
  109. });
  110. setTimeout(() => {
  111. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  112. const haveFocus = messageBox.$el.querySelector('textarea').isSameNode(document.activeElement);
  113. expect(haveFocus).to.true;
  114. done();
  115. }, 700);
  116. });
  117. describe('custom validator', () => {
  118. it('returns a string', done => {
  119. const validator = value => {
  120. if (value !== 'test') return 'wrong';
  121. return true;
  122. };
  123. MessageBox.prompt('这是一段内容', {
  124. title: '标题名称',
  125. inputValidator: validator
  126. });
  127. setTimeout(() => {
  128. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  129. messageBox.inputValue = 'no';
  130. setTimeout(() => {
  131. expect(document.querySelector('.el-message-box__errormsg')
  132. .textContent).to.equal('wrong');
  133. messageBox.inputValue = 'test';
  134. setTimeout(() => {
  135. expect(document.querySelector('.el-message-box__errormsg')
  136. .textContent).to.equal('');
  137. done();
  138. }, 100);
  139. }, 100);
  140. }, 200);
  141. });
  142. it('returns false', done => {
  143. const validator = value => false;
  144. MessageBox.prompt('这是一段内容', {
  145. title: '标题名称',
  146. inputValidator: validator
  147. });
  148. setTimeout(() => {
  149. const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
  150. messageBox.inputValue = 'no';
  151. setTimeout(() => {
  152. expect(document.querySelector('.el-message-box__errormsg')
  153. .textContent).to.equal('输入的数据不合法!');
  154. done();
  155. }, 100);
  156. }, 200);
  157. });
  158. });
  159. it('callback', done => {
  160. let msgAction = '';
  161. MessageBox({
  162. title: '消息',
  163. message: '这是一段内容'
  164. }, action => {
  165. msgAction = action;
  166. });
  167. setTimeout(() => {
  168. document.querySelector('.el-message-box__close').click();
  169. setTimeout(() => {
  170. expect(msgAction).to.equal('cancel');
  171. done();
  172. }, 10);
  173. }, 10);
  174. });
  175. it('beforeClose', done => {
  176. let msgAction = '';
  177. MessageBox({
  178. title: '消息',
  179. message: '这是一段内容',
  180. beforeClose: (action, instance) => {
  181. instance.close();
  182. }
  183. }, action => {
  184. msgAction = action;
  185. });
  186. setTimeout(() => {
  187. document.querySelector('.el-message-box__wrapper .el-button--primary').click();
  188. setTimeout(() => {
  189. expect(msgAction).to.equal('confirm');
  190. done();
  191. }, 10);
  192. }, 10);
  193. });
  194. describe('promise', () => {
  195. it('resolve', done => {
  196. MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
  197. .then(action => {
  198. expect(action).to.equal('confirm');
  199. done();
  200. });
  201. setTimeout(() => {
  202. document.querySelector('.el-message-box__wrapper .el-button--primary').click();
  203. }, 50);
  204. });
  205. it('reject', done => {
  206. MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
  207. .catch(action => {
  208. expect(action).to.equal('cancel');
  209. done();
  210. });
  211. setTimeout(() => {
  212. document.querySelector('.el-message-box__wrapper .el-button').click();
  213. }, 50);
  214. });
  215. });
  216. });