This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usesubmittrigger): add useSubmitTrigger (#19)
Add a new trigger called useSubmitTrigger that is responsible for providing submit events for forms
- Loading branch information
1 parent
ad617d6
commit aceab9c
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Copyright 2020, SumUp Ltd. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import useSubmitTrigger from './useSubmitTrigger'; | ||
|
||
export default useSubmitTrigger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Copyright 2019, SumUp Ltd. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
|
||
import { Events } from '../../types'; | ||
import TrackingRoot from '../../components/TrackingRoot'; | ||
|
||
import useSubmitTrigger from './useSubmitTrigger'; | ||
|
||
const DispatchForm = () => { | ||
const dispatch = useSubmitTrigger(); | ||
|
||
return ( | ||
<form | ||
data-testid="dispatch-form" | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
|
||
dispatch({ | ||
component: 'form' | ||
}); | ||
}} | ||
> | ||
Dispatch button | ||
</form> | ||
); | ||
}; | ||
|
||
describe('useSubmitTrigger', () => { | ||
it('should provide a dispatch function that contains the submit event', () => { | ||
const dispatch = jest.fn(); | ||
const app = 'test-app-hook'; | ||
const form = 'dispatch-form'; | ||
const component = 'form'; | ||
|
||
const expected = { | ||
app, | ||
view: undefined, | ||
elementTree: [], | ||
event: Events.submit, | ||
component, | ||
id: undefined, | ||
timestamp: expect.any(Number) | ||
}; | ||
|
||
const { getByTestId } = render( | ||
<TrackingRoot name={app} onDispatch={dispatch}> | ||
<DispatchForm /> | ||
</TrackingRoot> | ||
); | ||
|
||
fireEvent.submit(getByTestId(form)); | ||
|
||
expect(dispatch).toHaveBeenCalledWith(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright 2019, SumUp Ltd. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import useBaseTrigger from '../useBaseTrigger'; | ||
import { Events } from '../../types'; | ||
|
||
const useSubmitTrigger = () => useBaseTrigger(Events.submit); | ||
|
||
export default useSubmitTrigger; |