|
| 1 | +/** |
| 2 | + * Copyright 2019, SumUp Ltd. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import * as React from 'react'; |
| 17 | +import { render, fireEvent } from '@testing-library/react'; |
| 18 | + |
| 19 | +import { Events } from '../../types'; |
| 20 | +import TrackingRoot from '../../components/TrackingRoot'; |
| 21 | + |
| 22 | +import useSubmitTrigger from './useSubmitTrigger'; |
| 23 | + |
| 24 | +const DispatchForm = () => { |
| 25 | + const dispatch = useSubmitTrigger(); |
| 26 | + |
| 27 | + return ( |
| 28 | + <form |
| 29 | + data-testid="dispatch-form" |
| 30 | + onSubmit={(e) => { |
| 31 | + e.preventDefault(); |
| 32 | + |
| 33 | + dispatch({ |
| 34 | + component: 'form' |
| 35 | + }); |
| 36 | + }} |
| 37 | + > |
| 38 | + Dispatch button |
| 39 | + </form> |
| 40 | + ); |
| 41 | +}; |
| 42 | + |
| 43 | +describe('useSubmitTrigger', () => { |
| 44 | + it('should provide a dispatch function that contains the submit event', () => { |
| 45 | + const dispatch = jest.fn(); |
| 46 | + const app = 'test-app-hook'; |
| 47 | + const form = 'dispatch-form'; |
| 48 | + const component = 'form'; |
| 49 | + |
| 50 | + const expected = { |
| 51 | + app, |
| 52 | + view: undefined, |
| 53 | + elementTree: [], |
| 54 | + event: Events.submit, |
| 55 | + component, |
| 56 | + id: undefined, |
| 57 | + timestamp: expect.any(Number) |
| 58 | + }; |
| 59 | + |
| 60 | + const { getByTestId } = render( |
| 61 | + <TrackingRoot name={app} onDispatch={dispatch}> |
| 62 | + <DispatchForm /> |
| 63 | + </TrackingRoot> |
| 64 | + ); |
| 65 | + |
| 66 | + fireEvent.submit(getByTestId(form)); |
| 67 | + |
| 68 | + expect(dispatch).toHaveBeenCalledWith(expected); |
| 69 | + }); |
| 70 | +}); |
0 commit comments