popup.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { mount } from '@vue/test-utils';
  2. import { nextTick } from 'vue';
  3. import PopUp from '../index.vue';
  4. function sleep(delay = 0): Promise<void> {
  5. return new Promise((resolve) => {
  6. setTimeout(resolve, delay);
  7. });
  8. }
  9. test('should change z-index when using z-index prop', async () => {
  10. const wrapper = mount(PopUp, {
  11. props: {
  12. visible: true,
  13. zIndex: 99,
  14. isWrapTeleport: false
  15. }
  16. });
  17. await nextTick();
  18. const pop: any = wrapper.find('.nut-popup');
  19. expect(pop.element.style.zIndex).toEqual('99');
  20. });
  21. test('should change animation duration when using duration prop', () => {
  22. const wrapper = mount(PopUp, {
  23. props: {
  24. visible: true,
  25. isWrapTeleport: false,
  26. duration: 12
  27. }
  28. });
  29. const overlay: any = wrapper.find('.nut-overlay');
  30. expect(overlay.element.style.animationDuration).toEqual('12s');
  31. });
  32. test('prop overlay-class test', async () => {
  33. const wrapper = mount(PopUp, {
  34. props: {
  35. visible: true,
  36. isWrapTeleport: false,
  37. overlayClass: 'testclas'
  38. }
  39. });
  40. const overlay: any = wrapper.find('transition-stub');
  41. expect(overlay.classes()).toContain('testclas');
  42. });
  43. test('prop overlay-style test', async () => {
  44. const wrapper = mount(PopUp, {
  45. props: {
  46. visible: true,
  47. isWrapTeleport: false,
  48. overlayStyle: { color: 'red' }
  49. }
  50. });
  51. const overlay: any = wrapper.find('transition-stub');
  52. expect(overlay.element.style.color).toContain('red');
  53. });
  54. test('should lock scroll when showed', async () => {
  55. const wrapper = mount(PopUp, {
  56. visible: false,
  57. isWrapTeleport: false
  58. });
  59. await wrapper.setProps({ visible: true });
  60. expect(document.body.classList.contains('nut-overflow-hidden')).toBeFalsy();
  61. });
  62. test('should not render overlay when overlay prop is false', () => {
  63. const wrapper = mount(PopUp, {
  64. props: {
  65. visible: true,
  66. isWrapTeleport: false,
  67. overlay: false
  68. }
  69. });
  70. expect(wrapper.find('.nut-overlay').exists()).toBeFalsy();
  71. });
  72. test('prop close-on-click-overlay test', async () => {
  73. const wrapper = mount(PopUp, {
  74. props: {
  75. visible: true,
  76. isWrapTeleport: false,
  77. closeOnClickOverlay: false
  78. }
  79. });
  80. await wrapper.trigger('click');
  81. const overlay: any = wrapper.find('.nut-overlay');
  82. expect(overlay.element.style.display).toEqual('');
  83. });
  84. test('pop from top', async () => {
  85. const wrapper = mount(PopUp, {
  86. props: {
  87. visible: true,
  88. isWrapTeleport: false,
  89. position: 'top'
  90. }
  91. });
  92. await nextTick();
  93. const pop: any = wrapper.find('.popup-top');
  94. expect(pop.exists()).toBeTruthy();
  95. });
  96. test('pop from bottom', async () => {
  97. const wrapper = mount(PopUp, {
  98. props: {
  99. visible: true,
  100. isWrapTeleport: false,
  101. position: 'bottom'
  102. }
  103. });
  104. await nextTick();
  105. const pop: any = wrapper.find('.popup-bottom');
  106. expect(pop.exists()).toBeTruthy();
  107. });
  108. test('pop from left', async () => {
  109. const wrapper = mount(PopUp, {
  110. props: {
  111. visible: true,
  112. isWrapTeleport: false,
  113. position: 'left'
  114. }
  115. });
  116. await nextTick();
  117. const pop: any = wrapper.find('.popup-left');
  118. expect(pop.exists()).toBeTruthy();
  119. });
  120. test('pop from right', async () => {
  121. const wrapper = mount(PopUp, {
  122. props: {
  123. visible: true,
  124. isWrapTeleport: false,
  125. position: 'right'
  126. }
  127. });
  128. await nextTick();
  129. const pop: any = wrapper.find('.popup-right');
  130. expect(pop.exists()).toBeTruthy();
  131. });
  132. test('should render close icon when using closeable prop', async () => {
  133. const wrapper = mount(PopUp, {
  134. props: {
  135. visible: true,
  136. isWrapTeleport: false,
  137. closeable: true
  138. }
  139. });
  140. await nextTick();
  141. const closeIcon = wrapper.find('.nutui-popup__close-icon');
  142. expect(closeIcon.exists()).toBeTruthy();
  143. });
  144. test('should render correct close icon when using close-icon prop', () => {
  145. const wrapper = mount(PopUp, {
  146. props: {
  147. visible: true,
  148. isWrapTeleport: false,
  149. closeable: true,
  150. closeIcon: 'success'
  151. }
  152. });
  153. expect(wrapper.find('.nutui-popup__close-icon').html()).toMatchSnapshot();
  154. });
  155. test('should have "van-popup--round" class when setting the round prop', () => {
  156. const wrapper = mount(PopUp, {
  157. props: {
  158. visible: true,
  159. isWrapTeleport: false,
  160. round: true
  161. }
  162. });
  163. expect(wrapper.find('.round').exists()).toBeTruthy();
  164. });
  165. test('should allow to using teleport prop', async () => {
  166. const wrapper = mount(PopUp);
  167. await nextTick();
  168. expect(document.body.querySelector('.nut-popup')).toBeTruthy();
  169. });
  170. test('event click test', async () => {
  171. const wrapper = mount(PopUp, {
  172. props: {
  173. visible: true,
  174. isWrapTeleport: false,
  175. closeOnClickOverlay: true
  176. }
  177. });
  178. await nextTick();
  179. const popup: any = wrapper.find('.nut-popup');
  180. await popup.trigger('click');
  181. expect(wrapper.emitted('click')).toBeTruthy();
  182. });
  183. test('event click-close-icon test', async () => {
  184. const wrapper = mount(PopUp, {
  185. props: {
  186. visible: true,
  187. isWrapTeleport: false,
  188. closeable: true
  189. }
  190. });
  191. await wrapper.find('.nutui-popup__close-icon').trigger('click');
  192. expect(wrapper.emitted('click-close-icon')).toBeTruthy();
  193. });
  194. test('should emit open event when prop visible is set to true', async () => {
  195. const wrapper = mount(PopUp, {
  196. props: {
  197. visible: false,
  198. isWrapTeleport: false
  199. }
  200. });
  201. await wrapper.setProps({ visible: true });
  202. expect(wrapper.emitted('open')).toBeTruthy();
  203. });
  204. test('event close test', async () => {
  205. const wrapper = mount(PopUp, {
  206. props: {
  207. visible: true,
  208. isWrapTeleport: false
  209. }
  210. });
  211. await wrapper.find('.nut-overlay').trigger('click');
  212. await sleep(2000);
  213. expect(wrapper.emitted('close')).toBeTruthy();
  214. });
  215. test('event click-overlay test', async () => {
  216. const wrapper = mount(PopUp, {
  217. props: {
  218. visible: true,
  219. isWrapTeleport: false
  220. }
  221. });
  222. const overlay: any = wrapper.find('.nut-overlay');
  223. await overlay.trigger('click');
  224. expect(wrapper.emitted('click-overlay')).toBeTruthy();
  225. });