2
0

ExplorerPanel.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import ExplorerPanel from './ExplorerPanel';
  4. const mockProps = {
  5. page: {
  6. children: {
  7. items: [],
  8. },
  9. meta: {
  10. parent: null,
  11. },
  12. },
  13. depth: 1,
  14. onClose: jest.fn(),
  15. gotoPage: jest.fn(),
  16. nodes: {},
  17. };
  18. describe('ExplorerPanel', () => {
  19. describe('general rendering', () => {
  20. beforeEach(() => {
  21. document.body.innerHTML = '<div data-explorer-menu-item></div>';
  22. });
  23. it('exists', () => {
  24. expect(ExplorerPanel).toBeDefined();
  25. });
  26. it('renders', () => {
  27. expect(shallow(<ExplorerPanel {...mockProps} />)).toMatchSnapshot();
  28. });
  29. it('#isFetching', () => {
  30. expect(
  31. shallow(
  32. <ExplorerPanel
  33. {...mockProps}
  34. page={Object.assign({ isFetching: true }, mockProps.page)}
  35. />,
  36. ),
  37. ).toMatchSnapshot();
  38. });
  39. it('#isError', () => {
  40. expect(
  41. shallow(
  42. <ExplorerPanel
  43. {...mockProps}
  44. page={Object.assign({ isError: true }, mockProps.page)}
  45. />,
  46. ),
  47. ).toMatchSnapshot();
  48. });
  49. it('no children', () => {
  50. expect(
  51. shallow(<ExplorerPanel {...mockProps} page={{ children: {} }} />),
  52. ).toMatchSnapshot();
  53. });
  54. it('#items', () => {
  55. expect(
  56. shallow(
  57. <ExplorerPanel
  58. {...mockProps}
  59. page={{ children: { items: [1, 2] } }}
  60. nodes={{
  61. 1: {
  62. id: 1,
  63. admin_display_title: 'Test',
  64. meta: { status: {}, type: 'test' },
  65. },
  66. 2: {
  67. id: 2,
  68. admin_display_title: 'Foo',
  69. meta: { status: {}, type: 'foo' },
  70. },
  71. }}
  72. />,
  73. ),
  74. ).toMatchSnapshot();
  75. });
  76. });
  77. describe('onHeaderClick', () => {
  78. beforeEach(() => {
  79. mockProps.gotoPage.mockReset();
  80. });
  81. it('calls gotoPage', () => {
  82. shallow(
  83. <ExplorerPanel
  84. {...mockProps}
  85. depth={2}
  86. page={{ children: { items: [] }, meta: { parent: { id: 1 } } }}
  87. />,
  88. )
  89. .find('ExplorerHeader')
  90. .prop('onClick')({
  91. preventDefault() {},
  92. stopPropagation() {},
  93. });
  94. expect(mockProps.gotoPage).toHaveBeenCalled();
  95. });
  96. it('does not call gotoPage for first page', () => {
  97. shallow(
  98. <ExplorerPanel
  99. {...mockProps}
  100. depth={0}
  101. page={{ children: { items: [] }, meta: { parent: { id: 1 } } }}
  102. />,
  103. )
  104. .find('ExplorerHeader')
  105. .prop('onClick')({
  106. preventDefault() {},
  107. stopPropagation() {},
  108. });
  109. expect(mockProps.gotoPage).not.toHaveBeenCalled();
  110. });
  111. });
  112. describe('onItemClick', () => {
  113. beforeEach(() => {
  114. mockProps.gotoPage.mockReset();
  115. });
  116. it('calls gotoPage', () => {
  117. shallow(
  118. <ExplorerPanel
  119. {...mockProps}
  120. path={[1]}
  121. page={{ children: { items: [1] } }}
  122. nodes={{
  123. 1: {
  124. id: 1,
  125. admin_display_title: 'Test',
  126. meta: { status: {}, type: 'test' },
  127. },
  128. }}
  129. />,
  130. )
  131. .find('ExplorerItem')
  132. .prop('onClick')({
  133. preventDefault() {},
  134. stopPropagation() {},
  135. });
  136. expect(mockProps.gotoPage).toHaveBeenCalled();
  137. });
  138. });
  139. describe('hooks', () => {
  140. it('componentWillReceiveProps push', () => {
  141. const wrapper = shallow(<ExplorerPanel {...mockProps} />);
  142. expect(wrapper.setProps({ depth: 2 }).state('transition')).toBe('push');
  143. });
  144. it('componentWillReceiveProps pop', () => {
  145. const wrapper = shallow(<ExplorerPanel {...mockProps} />);
  146. expect(wrapper.setProps({ depth: 0 }).state('transition')).toBe('pop');
  147. });
  148. it('componentDidMount', () => {
  149. document.body.innerHTML = '<div data-explorer-menu-item></div>';
  150. const wrapper = shallow(<ExplorerPanel {...mockProps} />);
  151. wrapper.instance().componentDidMount();
  152. expect(
  153. document
  154. .querySelector('[data-explorer-menu-item]')
  155. .classList.contains('submenu-active'),
  156. ).toBe(true);
  157. expect(document.body.classList.contains('explorer-open')).toBe(true);
  158. });
  159. it('componentWillUnmount', () => {
  160. document.body.innerHTML =
  161. '<div class="submenu-active" data-explorer-menu-item></div>';
  162. const wrapper = shallow(<ExplorerPanel {...mockProps} />);
  163. wrapper.instance().componentWillUnmount();
  164. expect(
  165. document
  166. .querySelector('[data-explorer-menu-item]')
  167. .classList.contains('submenu-active'),
  168. ).toBe(false);
  169. expect(document.body.classList.contains('explorer-open')).toBe(false);
  170. });
  171. });
  172. describe('clickOutside', () => {
  173. afterEach(() => {
  174. mockProps.onClose.mockReset();
  175. });
  176. it('triggers onClose when click is outside', () => {
  177. document.body.innerHTML =
  178. '<div data-explorer-menu-item></div><div data-explorer-menu></div><div id="t"></div>';
  179. const wrapper = shallow(<ExplorerPanel {...mockProps} />);
  180. wrapper.instance().clickOutside({
  181. target: document.querySelector('#t'),
  182. });
  183. expect(mockProps.onClose).toHaveBeenCalled();
  184. });
  185. it('does not trigger onClose when click is inside', () => {
  186. document.body.innerHTML =
  187. '<div data-explorer-menu-item></div><div data-explorer-menu><div id="t"></div></div>';
  188. const wrapper = shallow(<ExplorerPanel {...mockProps} />);
  189. wrapper.instance().clickOutside({
  190. target: document.querySelector('#t'),
  191. });
  192. expect(mockProps.onClose).not.toHaveBeenCalled();
  193. });
  194. it('pauses focus trap inside toggle', () => {
  195. document.body.innerHTML =
  196. '<div data-explorer-menu-item><div id="t"></div></div><div data-explorer-menu></div>';
  197. const wrapper = shallow(<ExplorerPanel {...mockProps} />);
  198. wrapper.instance().clickOutside({
  199. target: document.querySelector('#t'),
  200. });
  201. expect(wrapper.state('paused')).toEqual(true);
  202. });
  203. });
  204. });