Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. import 'jest-dom/extend-expect';
  2. import React from 'react';
  3. import { render, fireEvent, cleanup } from '@testing-library/react';
  4. import Form from './form';
  5.  
  6. window.FormData = undefined;
  7. require('formdata-polyfill');
  8.  
  9. describe('Form container', () => {
  10. afterEach(cleanup);
  11.  
  12. it('passes an object with the form data to the onSubmit callback when the form is submitted', async () => {
  13. interface Response {
  14. 'test-input': string;
  15. 'test-input-2': string;
  16. }
  17. const handleSubmit = jest.fn();
  18. const form = render(
  19. <Form<Response>
  20. onSubmit={handleSubmit}
  21. render={() => (
  22. <fieldset>
  23. <label htmlFor="test-input">
  24. Test input
  25. <input type="text" name="test-input" id="test-input" />
  26. </label>
  27. <label htmlFor="test-input-2">
  28. Test input 2
  29. <input type="text" name="test-input-2" id="test-input-2" />
  30. </label>
  31. <button type="submit">Submit</button>
  32. </fieldset>
  33. )}
  34. />,
  35. );
  36.  
  37. const testInput = form.getByLabelText('Test input');
  38. const submitButton = form.getByText('Submit');
  39.  
  40. fireEvent.change(testInput, { target: { value: 'Test value' } });
  41. fireEvent.click(submitButton);
  42.  
  43. expect(handleSubmit).toHaveBeenCalledWith({
  44. 'test-input': 'Test value',
  45. 'test-input-2': '',
  46. });
  47. });
  48.  
  49. it('resolves promises returned by onSubmit', async () => {
  50. let submitPromise;
  51. const handleSubmit = () => {
  52. submitPromise = Promise.resolve({ a: 1 });
  53. return submitPromise;
  54. };
  55.  
  56. const renderForm = jest.fn(() => <button type="submit">Submit</button>);
  57.  
  58. const form = render(<Form onSubmit={handleSubmit} render={renderForm} />);
  59.  
  60. const submitButton = form.getByText('Submit');
  61. fireEvent.click(submitButton);
  62.  
  63. expect(renderForm.mock.calls[0]).toMatchObject([
  64. {
  65. data: {},
  66. formState: 'IDLE',
  67. },
  68. ]);
  69.  
  70. expect(renderForm.mock.calls[1]).toMatchObject([
  71. {
  72. data: {},
  73. formState: 'LOADING',
  74. },
  75. ]);
  76.  
  77. await submitPromise;
  78.  
  79. expect(renderForm.mock.calls[2]).toMatchObject([
  80. {
  81. data: {},
  82. response: { a: 1 },
  83. formState: 'SUBMITTED',
  84. },
  85. ]);
  86. });
  87.  
  88. it('passes error message to render', async () => {
  89. const error = new Error('Something went wrong');
  90. let submitPromise;
  91. const handleSubmit = () => {
  92. submitPromise = Promise.reject(new Error('Something went wrong'));
  93. return submitPromise;
  94. };
  95.  
  96. const renderForm = jest.fn(() => <button type="submit">Submit</button>);
  97.  
  98. const form = render(<Form onSubmit={handleSubmit} render={renderForm} />);
  99.  
  100. const submitButton = form.getByText('Submit');
  101. fireEvent.click(submitButton);
  102.  
  103. expect(renderForm.mock.calls[1]).toMatchObject([
  104. {
  105. data: {},
  106. formState: 'LOADING',
  107. },
  108. ]);
  109.  
  110. // @ts-ignore
  111. await submitPromise.catch(() => {});
  112.  
  113. expect(renderForm.mock.calls[2]).toMatchObject([
  114. {
  115. data: {},
  116. error,
  117. formState: 'FAILED',
  118. },
  119. ]);
  120. });
  121.  
  122. it('tracks controlled components', () => {
  123. interface Response {
  124. uncontrolledInput: string;
  125. controlledInput1: string;
  126. controlledInput2: string;
  127. }
  128. const handleSubmit = jest.fn();
  129. const form = render(
  130. <Form<Response>
  131. onSubmit={handleSubmit}
  132. render={({ trackInput, trackedValues }) => (
  133. <fieldset>
  134. <label htmlFor="uncontrolledInput">
  135. Uncontrolled Input
  136. <input
  137. type="text"
  138. name="uncontrolledInput"
  139. id="uncontrolledInput"
  140. defaultValue="Default Value"
  141. />
  142. </label>
  143. <label htmlFor="controlledInput1">
  144. Controlled Input 1
  145. <input
  146. type="text"
  147. id="controlledInput1"
  148. value={trackedValues.controlledInput1}
  149. onChange={trackInput('controlledInput1')}
  150. />
  151. </label>
  152. <label htmlFor="controlledInput2">
  153. Controlled Input 2
  154. <input
  155. type="text"
  156. id="controlledInput2"
  157. value={trackedValues.controlledInput2}
  158. onChange={e => trackInput('controlledInput2')(e.target.value)}
  159. />
  160. </label>
  161. <button type="submit">Submit</button>
  162. </fieldset>
  163. )}
  164. />,
  165. );
  166.  
  167. const uncontrolledInput = form.getByLabelText('Uncontrolled Input');
  168. const controlledInput1 = form.getByLabelText('Controlled Input 1');
  169. const controlledInput2 = form.getByLabelText('Controlled Input 2');
  170. const submitButton = form.getByText('Submit');
  171.  
  172. fireEvent.change(uncontrolledInput, { target: { value: 'Test value 1' } });
  173. fireEvent.change(controlledInput1, { target: { value: 'Test value 2' } });
  174. fireEvent.change(controlledInput2, { target: { value: 'Test value 3' } });
  175. fireEvent.click(submitButton);
  176.  
  177. expect(handleSubmit).toHaveBeenCalledWith({
  178. uncontrolledInput: 'Test value 1',
  179. controlledInput1: 'Test value 2',
  180. controlledInput2: 'Test value 3',
  181. });
  182. });
  183.  
  184. it('sets default values on tracked inputs', () => {
  185. interface Response {
  186. uncontrolledInput: string;
  187. controlledInput1: string;
  188. controlledInput2: string;
  189. }
  190. const handleSubmit = jest.fn();
  191. const form = render(
  192. <Form<Response>
  193. onSubmit={handleSubmit}
  194. fields={{ controlledInput1: 'default value' }}
  195. render={({ fields: { controlledInput1, controlledInput2 } }) => (
  196. <fieldset>
  197. <label htmlFor="uncontrolledInput">
  198. Uncontrolled Input
  199. <input
  200. type="text"
  201. name="uncontrolledInput"
  202. id="uncontrolledInput"
  203. defaultValue="Default value 0"
  204. />
  205. </label>
  206. <label htmlFor="controlledInput1">
  207. Controlled Input 1
  208. <input
  209. type="text"
  210. id="controlledInput1"
  211. value={controlledInput1.value}
  212. onChange={controlledInput1.handler}
  213. />
  214. </label>
  215. <label htmlFor="controlledInput2">
  216. Controlled Input 2
  217. <input
  218. type="text"
  219. id="controlledInput2"
  220. value={controlledInput2.value}
  221. onChange={controlledInput2.handler}
  222. />
  223. </label>
  224. <button type="submit">Submit</button>
  225. </fieldset>
  226. )}
  227. />,
  228. );
  229.  
  230. const submitButton = form.getByText('Submit');
  231.  
  232. fireEvent.click(submitButton);
  233.  
  234. expect(handleSubmit).toHaveBeenCalledWith({
  235. uncontrolledInput: 'Default value 0',
  236. controlledInput1: 'Default value 1',
  237. controlledInput2: '',
  238. });
  239. });
  240.  
  241. it('does not override empty inputs with default values', () => {
  242. const handleSubmit = jest.fn();
  243. const form = render(
  244. <Form
  245. onSubmit={handleSubmit}
  246. defaultTrackedValues={{ controlledInput: 'Default value' }}
  247. render={({ trackedValues, trackInput }) => (
  248. <fieldset>
  249. <label htmlFor="controlledInput">
  250. Controlled Input 1
  251. <input
  252. type="text"
  253. id="controlledInput"
  254. value={trackedValues.controlledInput}
  255. onChange={trackInput('controlledInput')}
  256. />
  257. </label>
  258. <button type="submit">Submit</button>
  259. </fieldset>
  260. )}
  261. />,
  262. );
  263.  
  264. const controlledInput = form.getByLabelText('Controlled Input 1');
  265. const submitButton = form.getByText('Submit');
  266.  
  267. fireEvent.change(controlledInput, { target: { value: '' } });
  268. fireEvent.click(submitButton);
  269.  
  270. expect(handleSubmit).toHaveBeenLastCalledWith({
  271. controlledInput: '',
  272. });
  273. });
  274.  
  275. it('has attributes to fall back on when JS fails', () => {
  276. const formWrapper = render(
  277. <Form
  278. action="someurl"
  279. method="POST"
  280. onSubmit={() => {}}
  281. render={() => <input type="text" name="test-input" />}
  282. />,
  283. );
  284.  
  285. const form = formWrapper.getByTestId('form');
  286.  
  287. expect(form).toHaveAttribute('action', 'someurl');
  288. expect(form).toHaveAttribute('method', 'POST');
  289. });
  290. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement