Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 131341b

Browse files
authored
feat: Implement useSectionExpandedTrigger hook (#151)
* Implement useExpandTrigger hook * feat: rename useExpandTrigger to useSectionExpandedTrigger * feat: add documentation and unit tests
1 parent ce02645 commit 131341b

File tree

6 files changed

+150
-3
lines changed

6 files changed

+150
-3
lines changed

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Collector is a library of React components and hooks that facilitates contextual
2121
- [TrackingView](#trackingview)
2222
- [TrackingElement](#trackingelement)
2323
- [useClickTrigger](#useclicktrigger)
24+
- [useSectionExpandedTrigger](#usesectionexpandedtrigger)
2425
- [useSubmitTrigger](#usesubmittrigger)
2526
- [usePageViewTrigger](#usepageviewtrigger)
2627
- [Plugin](#plugin)
@@ -115,7 +116,8 @@ interface Event {
115116
| 'page-view'
116117
| 'page-reactivated'
117118
| 'submit'
118-
| 'browser-back'; // This property is added internally based on the kind of event you dispatched.
119+
| 'browser-back'
120+
| 'section-expanded'; // This property is added internally based on the kind of event you dispatched.
119121
timestamp: number; // This property is added internally when the dispatch function is called
120122
customParameters?: {
121123
[key: string]: any;
@@ -274,6 +276,42 @@ function Button({ onClick, 'tracking-label': label, children }) {
274276
return <button onClick={handler}>{children}</button>;
275277
}
276278
```
279+
### useSectionExpandedTrigger
280+
281+
`useSectionExpandedTrigger` provides you a dispatch function for a section expanded event.
282+
283+
The dispatch function accepts the following interface:
284+
285+
```jsx
286+
interface Options {
287+
component?: string;
288+
label?: string;
289+
customParameters?: {
290+
[key: string]: any
291+
};
292+
event: 'section-expanded'; // Added internally by the hook
293+
timestamp: number; // Added internally when the dispatch function is called
294+
}
295+
```
296+
297+
```jsx
298+
import React from 'react';
299+
import { useSectionExpandedTrigger } from '@sumup/collector';
300+
301+
function Section({ onClick, 'tracking-label': label, children }) {
302+
const dispatch = useSectionExpandedTrigger();
303+
let expandHandler = onClick;
304+
305+
if (label) {
306+
expandHandler = (e) => {
307+
dispatch({ label, component: 'section' });
308+
onClick && onClick(e);
309+
};
310+
}
311+
312+
return <div onClick={expandHandler}>{children}</div>;
313+
}
314+
```
277315

278316
### useSubmitTrigger
279317

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2020, 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 useSectionExpandedTrigger from './useSectionExpandedTrigger';
17+
18+
export default useSectionExpandedTrigger;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 useSectionExpandedTrigger from './useSectionExpandedTrigger';
23+
24+
const DispatchButton = () => {
25+
const dispatch = useSectionExpandedTrigger();
26+
27+
return (
28+
<button
29+
data-testid="section"
30+
onClick={() =>
31+
dispatch({
32+
component: 'section'
33+
})
34+
}
35+
>
36+
Dispatch button
37+
</button>
38+
);
39+
};
40+
41+
describe('useSectionExpandedTrigger', () => {
42+
it('should provide a dispatch function that contains the section-expanded event', () => {
43+
const dispatch = jest.fn();
44+
const app = 'test-app-hook';
45+
const btn = 'section';
46+
const component = 'section';
47+
48+
const expected = {
49+
app,
50+
view: undefined,
51+
elementTree: [],
52+
event: Events.sectionExpanded,
53+
component,
54+
id: undefined,
55+
timestamp: expect.any(Number)
56+
};
57+
58+
const { getByTestId } = render(
59+
<TrackingRoot name={app} onDispatch={dispatch}>
60+
<DispatchButton />
61+
</TrackingRoot>
62+
);
63+
64+
fireEvent.click(getByTestId(btn));
65+
66+
expect(dispatch).toHaveBeenCalledWith(expected);
67+
});
68+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 useBaseTrigger from '../useBaseTrigger';
17+
import { Events } from '../../types';
18+
19+
const useSectionExpandedTrigger = () => useBaseTrigger(Events.sectionExpanded);
20+
21+
export default useSectionExpandedTrigger;

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import useClickTrigger from './hooks/useClickTrigger';
2020
import useSubmitTrigger from './hooks/useSubmitTrigger';
2121
import usePageViewTrigger from './hooks/usePageViewTrigger';
2222
import usePageActiveTrigger from './hooks/usePageActiveTrigger';
23+
import useSectionExpandedTrigger from './hooks/useSectionExpandedTrigger';
2324
import getFlushedPayload from './plugins/getFlushedPayload';
2425
import * as Types from './types';
2526

@@ -31,6 +32,7 @@ export {
3132
useSubmitTrigger,
3233
usePageViewTrigger,
3334
usePageActiveTrigger,
35+
useSectionExpandedTrigger,
3436
getFlushedPayload
3537
};
3638

src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export enum Events {
2222
pageView = 'page-view',
2323
pageReactivated = 'page-reactivated',
2424
submit = 'submit',
25-
browserBack = 'browser-back'
25+
browserBack = 'browser-back',
26+
sectionExpanded = 'section-expanded'
2627
}
2728

2829
export interface Payload {
@@ -45,7 +46,6 @@ export interface Dispatch {
4546
[key: string]: unknown;
4647
};
4748
}
48-
4949
export interface TrackingContextKeys {
5050
app?: string;
5151
view?: string;

0 commit comments

Comments
 (0)