Portal.test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React from 'react';
  2. import { shallow, mount } from 'enzyme';
  3. import Portal from './Portal';
  4. const func = expect.any(Function);
  5. describe('Portal', () => {
  6. beforeEach(() => {
  7. document.body.innerHTML = '';
  8. });
  9. it('empty', () => {
  10. expect(mount(<Portal onClose={() => {}} />)).toMatchSnapshot();
  11. });
  12. it('#children', () => {
  13. expect(mount(<Portal onClose={() => {}}>Test!</Portal>)).toMatchSnapshot();
  14. });
  15. it('component lifecycle', () => {
  16. document.removeEventListener = jest.fn();
  17. window.removeEventListener = jest.fn();
  18. const wrapper = mount(<Portal onClose={() => {}}>Test!</Portal>);
  19. expect(document.body.innerHTML).toMatchSnapshot();
  20. wrapper.instance().componentWillUnmount();
  21. expect(document.body.innerHTML).toBe('');
  22. expect(document.removeEventListener).toHaveBeenCalledWith('mouseup', func);
  23. expect(document.removeEventListener).toHaveBeenCalledWith('keyup', func);
  24. expect(window.removeEventListener).toHaveBeenCalledWith('resize', func);
  25. document.removeEventListener.mockRestore();
  26. window.removeEventListener.mockRestore();
  27. });
  28. describe('#onClose', () => {
  29. beforeEach(() => {
  30. jest.spyOn(document, 'addEventListener');
  31. jest.spyOn(window, 'addEventListener');
  32. });
  33. afterEach(() => {
  34. document.removeEventListener.mockRestore();
  35. window.removeEventListener.mockRestore();
  36. });
  37. it('#closeOnClick', () => {
  38. const onClose = jest.fn();
  39. shallow(
  40. <Portal onClose={onClose} closeOnClick>
  41. Test!
  42. </Portal>
  43. );
  44. expect(document.addEventListener).toHaveBeenCalledWith('mouseup', func);
  45. });
  46. it('#closeOnType', () => {
  47. const onClose = jest.fn();
  48. shallow(
  49. <Portal onClose={onClose} closeOnType>
  50. Test!
  51. </Portal>
  52. );
  53. expect(document.addEventListener).toHaveBeenCalledWith('keyup', func);
  54. });
  55. it('#closeOnResize', () => {
  56. const onClose = jest.fn();
  57. shallow(
  58. <Portal onClose={onClose} closeOnResize>
  59. Test!
  60. </Portal>
  61. );
  62. expect(window.addEventListener).toHaveBeenCalledWith('resize', func);
  63. });
  64. });
  65. describe('onCloseEvent', () => {
  66. it('should close', () => {
  67. const onClose = jest.fn();
  68. const wrapper = mount(<Portal onClose={onClose}>Test!</Portal>);
  69. const target = document.createElement('div');
  70. wrapper.instance().onCloseEvent({ target });
  71. expect(onClose).toHaveBeenCalled();
  72. });
  73. it('not should close', () => {
  74. const onClose = jest.fn();
  75. const wrapper = mount(
  76. <Portal onClose={onClose}>
  77. <div id="test">Test</div>
  78. </Portal>
  79. );
  80. const target = document.querySelector('#test');
  81. wrapper.instance().onCloseEvent({ target });
  82. expect(onClose).not.toHaveBeenCalled();
  83. });
  84. });
  85. });