util.clickoutside.spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import Clickoutside from 'element-ui/src/utils/clickoutside';
  2. const ctx = '@@clickoutsideContext';
  3. describe('Utils:Clickoutside', () => {
  4. it('create', () => {
  5. let count = 0;
  6. const el = document.createElement('div');
  7. const vnode = {
  8. context: {
  9. handleClick: () => ++count
  10. }
  11. };
  12. const binding = {
  13. expression: 'handleClick'
  14. };
  15. Clickoutside.bind(el, binding, vnode);
  16. expect(el[ctx]).to.exist;
  17. });
  18. it('cotext not exist', () => {
  19. const el = document.createElement('div');
  20. const vnode = {};
  21. const binding = {
  22. expression: 'handleClick'
  23. };
  24. Clickoutside.bind(el, binding, vnode);
  25. expect(el[ctx]).to.exist;
  26. });
  27. it('binding expression', () => {
  28. const el = document.createElement('div');
  29. let count = 0;
  30. const vnode = {
  31. context: {
  32. handleClick: () => ++count
  33. }
  34. };
  35. const binding = {
  36. expression: 'handleClick'
  37. };
  38. Clickoutside.bind(el, binding, vnode);
  39. document.body.click();
  40. expect(count).to.equal(1);
  41. });
  42. it('click inside', () => {
  43. const el = document.createElement('div');
  44. const insideElm = document.createElement('div');
  45. let count = 0;
  46. const vnode = {
  47. context: {
  48. handleClick: () => ++count
  49. }
  50. };
  51. const binding = {
  52. expression: 'handleClick'
  53. };
  54. el.appendChild(insideElm);
  55. Clickoutside.bind(el, binding, vnode);
  56. insideElm.click();
  57. expect(count).to.equal(0);
  58. document.body.click();
  59. expect(count).to.equal(1);
  60. });
  61. it('tigger event in popperElm', () => {
  62. const el = document.createElement('div');
  63. const insideElm = document.createElement('div');
  64. let count = 0;
  65. const vnode = {
  66. context: {
  67. handleClick: () => ++count,
  68. popperElm: document.createElement('div')
  69. }
  70. };
  71. const binding = {
  72. expression: 'handleClick'
  73. };
  74. vnode.context.popperElm.appendChild(insideElm);
  75. Clickoutside.bind(el, binding, vnode);
  76. insideElm.click();
  77. expect(count).to.equal(0);
  78. document.body.click();
  79. expect(count).to.equal(1);
  80. });
  81. it('binding value', () => {
  82. const el = document.createElement('div');
  83. let count = 0;
  84. const vnode = {
  85. context: {}
  86. };
  87. const binding = {
  88. value: () => ++count
  89. };
  90. Clickoutside.bind(el, binding, vnode);
  91. expect(count).to.equal(0);
  92. document.body.click();
  93. expect(count).to.equal(1);
  94. });
  95. it('update', () => {
  96. let count = 0;
  97. const el = document.createElement('div');
  98. const vnode = {
  99. context: {
  100. abc: () => ++count,
  101. ddd: () => ++count
  102. }
  103. };
  104. const binding = {
  105. expression: 'abc'
  106. };
  107. const newBinding = {
  108. expression: 'ddd'
  109. };
  110. Clickoutside.bind(el, binding, vnode);
  111. expect(el[ctx].methodName).to.equal('abc');
  112. Clickoutside.update(el, newBinding);
  113. expect(el[ctx].methodName).to.equal('ddd');
  114. });
  115. it('unbind', () => {
  116. const el = document.createElement('div');
  117. let count = 0;
  118. const vnode = {
  119. context: {}
  120. };
  121. const binding = {
  122. value: () => ++count
  123. };
  124. Clickoutside.bind(el, binding, vnode);
  125. document.body.click();
  126. Clickoutside.unbind(el);
  127. document.body.click();
  128. expect(count).to.equal(1);
  129. });
  130. });