Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. // Libraries
  2. import React from 'react';
  3. import { configure, mount } from 'enzyme';
  4. import EnzymeAdapter from 'enzyme-adapter-react-16';
  5. import checkProps from 'check-prop-types';
  6. import waait from 'waait';
  7. import { act } from 'react-dom/test-utils';
  8.  
  9. // Components
  10. import { App as UnconnectedApp, listYfunc } from './App';
  11.  
  12. const reactUseState = React.useState;
  13.  
  14. configure({
  15. adapter: new EnzymeAdapter(),
  16. disableLifecycleMethods: true,
  17. });
  18.  
  19. const defaultProps = {
  20. listX() {},
  21. listY() {},
  22. xText: undefined,
  23. }
  24.  
  25. /**
  26. * @function setup
  27. * @param initialProps - wrapper initial props
  28. * @returns {ReactWrapper} - Enzyme `mount` ReactWrapper
  29. */
  30. function setup(initialProps = defaultProps) {
  31. const wrapper = mount(
  32. <UnconnectedApp {...initialProps} />
  33. );
  34. console.log('wrapper', wrapper);
  35. console.log('wrapper.debug()', wrapper.debug());
  36. return wrapper;
  37. }
  38.  
  39. /**
  40. * Return ShallowWrapper containing node(s) with the given data-test value.
  41. * @param {ShallowWrapper} wrapper - Enzyme shallow wrapper to search within
  42. * @param {string} val - Value of data-test attribute for search.
  43. * @return {ShallowWrapper}
  44. */
  45. export function findByTestAttr(wrapper, val) {
  46. return wrapper.find(`[data-test="${val}"]`);
  47. }
  48.  
  49. /**
  50. * Takes some expected props and see wether or not they would throw a warning.
  51. * The general idea is to give expected props to be good, and make sure they
  52. * do not throw a warning.
  53. * @param {React.Component} component - React component with propTypes property.
  54. * @param {object} conformingProps - Expected props object.
  55. * @returns {void}
  56. */
  57. export function checkPropTypes(component, conformingProps) {
  58. const propError = checkProps(
  59. component.propTypes,
  60. conformingProps,
  61. 'prop',
  62. component.name
  63. );
  64. expect(propError).toBeUndefined();
  65. }
  66.  
  67. describe('renders', () => {
  68. const xText = 'xText';
  69. let listXMock = jest.fn().mockReturnValue();
  70. let wrapper;
  71.  
  72. beforeEach(() => {
  73. wrapper = setup();
  74. });
  75.  
  76. afterEach(() => {
  77. listXMock.mockClear();
  78. });
  79.  
  80. it('renders app component', () => {
  81. const appComponent = findByTestAttr(wrapper, 'component-app');
  82. expect(appComponent.exists()).toBe(true);
  83. });
  84.  
  85. it('renders loader placeholder', () => {
  86. const loader = findByTestAttr(wrapper, 'loader');
  87. expect(loader.exists()).toBe(true);
  88. });
  89.  
  90. it('renders lazy-load button', () => {
  91. const lazyLoadButton = findByTestAttr(wrapper, 'lazy-load-button');
  92. expect(lazyLoadButton.exists()).toBe(true);
  93. })
  94.  
  95. it('renders image-loader button', () => {
  96. const imageLoader = findByTestAttr(wrapper, 'image-loader');
  97. expect(imageLoader.exists()).toBe(true);
  98. })
  99.  
  100. it('renders without prop type errors', () => {
  101. checkPropTypes(UnconnectedApp, defaultProps);
  102. });
  103.  
  104. it('`listX` is called when mounted', () => {
  105. setup({
  106. ...defaultProps,
  107. listX: listXMock,
  108. });
  109. expect(listXMock).toHaveBeenCalledWith('React');
  110. });
  111.  
  112. it('`listX` is called with xText prop if exists', () => {
  113. listXMock = jest.fn().mockReturnValue(xText);
  114. setup({
  115. ...defaultProps,
  116. listX: listXMock,
  117. xText,
  118. });
  119. expect(listXMock).toHaveBeenCalledWith(xText);
  120. });
  121. });
  122.  
  123. describe('`listY` integration tests with React.useState & React.useEffect', () => {
  124. // Mocking listY & listY parameter
  125. const yText = 'yText';
  126. let listYMock = jest.fn(listYfunc);
  127. // Mocking React.useState
  128. let state;
  129. const setState = jest.fn((payload) => {
  130. state = payload;
  131. });
  132.  
  133. beforeEach(() => {
  134. React.useState = jest.fn().mockReturnValue([state, setState]);
  135. })
  136.  
  137. afterEach(() => {
  138. // Clearing mocks
  139. listYMock.mockClear();
  140. setState.mockClear();
  141. // Resetting module
  142. React.useState = reactUseState;
  143. state = undefined;
  144. });
  145.  
  146. it('calls `listY` inside `React.useEffect`', async () => {
  147. setup({
  148. ...defaultProps,
  149. listY: listYMock
  150. });
  151.  
  152. await act(async () => {
  153. await waait(500);
  154. });
  155.  
  156. expect(listYMock).toHaveBeenCalled();
  157. });
  158.  
  159. it('calls `listY` inside `React.useEffect` with `yText`', async () => {
  160. setup({
  161. ...defaultProps,
  162. listY: listYMock,
  163. yText
  164. });
  165.  
  166. await act(async () => {
  167. await waait(500);
  168. });
  169.  
  170. expect(listYMock.mock.calls[0][0]).toBe(yText);
  171. });
  172.  
  173. it('calls `setState` after executing `listY`', async () => {
  174. setup({
  175. ...defaultProps,
  176. listY: listYMock
  177. });
  178.  
  179. await act(async () => {
  180. await waait(500);
  181. });
  182.  
  183. expect(setState).toHaveBeenCalled();
  184. });
  185.  
  186. it('calls `setState` after executing `listY` with `yText` prop', async () => {
  187. setup({
  188. ...defaultProps,
  189. listY: listYMock,
  190. yText,
  191. });
  192.  
  193. await act(async () => {
  194. await waait(500);
  195. });
  196.  
  197. expect(setState).toHaveBeenCalledWith(yText.split('').reverse().join(''));
  198. });
  199.  
  200. it('sets state correctly without `yText` parameter', async () => {
  201. setup({
  202. ...defaultProps,
  203. listY: listYMock,
  204. });
  205.  
  206. await act(async () => {
  207. await waait(500);
  208. });
  209.  
  210. expect(state).toBe('TypeScript > JavaScript'.split('').reverse().join(''));
  211. });
  212.  
  213. it('sets state correctly with `yText` parameter', async () => {
  214. setup({
  215. ...defaultProps,
  216. listY: listYMock,
  217. yText,
  218. });
  219.  
  220. await act(async () => {
  221. await waait(500);
  222. });
  223.  
  224. expect(state).toBe(yText.split('').reverse().join(''));
  225. });
  226. });
  227.  
  228. describe('`fetchImage` integration test with lazy-load-button', () => {
  229. let wrapper;
  230.  
  231. beforeEach(() => {
  232. wrapper = setup();
  233. });
  234.  
  235. it('image-loader renders right text', () => {
  236. const imageLoader = findByTestAttr(wrapper, 'image-loader');
  237. expect(imageLoader.text()).toContain('Loading');
  238. });
  239.  
  240. it('lazy-image is rendered after clicking the button', async () => {
  241. const lazyLoadButton = findByTestAttr(wrapper, 'lazy-load-button');
  242. expect(lazyLoadButton.exists()).toBe(true);
  243.  
  244. await act(async () => {
  245. await waait(500);
  246. });
  247.  
  248.  
  249. });
  250. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement