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

Commit 9145967

Browse files
authored
feat(TrackingElement): add optional label prop (#172)
1 parent bc1d728 commit 9145967

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
"build": "rollup -c",
1515
"compile": "tsc",
1616
"commit": "git-cz",
17-
"test": "jest",
18-
"test:watch": "jest --watch",
19-
"test:ci": "yarn test --ci --coverage",
17+
"test": "jest --watch",
18+
"test:ci": "jest --ci --coverage",
2019
"lint": "foundry run eslint . --ext .js,.jsx,.ts,.tsx",
2120
"lint:fix": "yarn lint --fix",
2221
"lint:ci": "yarn lint --format junit -o __reports__/eslint-results.xml",

src/components/TrackingElement/TrackingElement.spec.tsx

+38-4
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ const DispatchButton = ({ testId = 'dispatch-btn' }: DispatchButton) => {
4545
};
4646

4747
describe('Element', () => {
48-
it('should attach the element property when dispatching an event', () => {
48+
it('should append the name to the elementTree when dispatching an event', () => {
4949
const dispatch = jest.fn();
5050
const app = '';
5151
const view = 'test';
52-
const element = 'test-element-spec';
52+
const name = 'test-element';
5353
const btn = 'dispatch-btn';
5454
const component = 'button';
5555

5656
const expected = {
5757
app,
5858
view,
59-
elementTree: [element],
59+
elementTree: [name],
6060
event: Events.click,
6161
component,
6262
label: undefined,
@@ -66,7 +66,41 @@ describe('Element', () => {
6666
const { getByTestId } = render(
6767
<TrackingRoot name={app} onDispatch={dispatch}>
6868
<TrackingView name={view}>
69-
<TrackingElement name={element}>
69+
<TrackingElement name={name}>
70+
<DispatchButton />
71+
</TrackingElement>
72+
</TrackingView>
73+
</TrackingRoot>
74+
);
75+
76+
fireEvent.click(getByTestId(btn));
77+
78+
expect(dispatch).toHaveBeenCalledWith(expected);
79+
});
80+
81+
it('should append the name and label to the elementTree when dispatching an event', () => {
82+
const dispatch = jest.fn();
83+
const app = '';
84+
const view = 'test';
85+
const name = 'test-element';
86+
const label = 'test-label';
87+
const btn = 'dispatch-btn';
88+
const component = 'button';
89+
90+
const expected = {
91+
app,
92+
view,
93+
elementTree: ['test-element|test-label'],
94+
event: Events.click,
95+
component,
96+
label: undefined,
97+
timestamp: expect.any(Number)
98+
};
99+
100+
const { getByTestId } = render(
101+
<TrackingRoot name={app} onDispatch={dispatch}>
102+
<TrackingView name={view}>
103+
<TrackingElement name={name} label={label}>
70104
<DispatchButton />
71105
</TrackingElement>
72106
</TrackingView>

src/components/TrackingElement/TrackingElement.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ import * as React from 'react';
1818
import { TrackingProviderProps as ProviderProps } from '../../types';
1919
import TrackingContext from '../TrackingContext';
2020

21-
const TrackingElement = ({ name, children }: ProviderProps) => {
21+
export interface TrackingElementProps extends ProviderProps {
22+
label?: string;
23+
}
24+
25+
const TrackingElement = ({ name, label, children }: TrackingElementProps) => {
2226
const baseContext = React.useContext(TrackingContext);
27+
const elementName = label ? `${name}|${label}` : name;
2328
const contextValue = React.useMemo(
2429
() => ({
2530
...baseContext,
26-
elementTree: [...baseContext.elementTree, name]
31+
elementTree: [...baseContext.elementTree, elementName]
2732
}),
28-
[baseContext, name]
33+
[baseContext, elementName]
2934
);
3035

3136
return (

0 commit comments

Comments
 (0)