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

feat(TrackingElement): add optional label prop #172

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
"build": "rollup -c",
"compile": "tsc",
"commit": "git-cz",
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "yarn test --ci --coverage",
"test": "jest --watch",
"test:ci": "jest --ci --coverage",
"lint": "foundry run eslint . --ext .js,.jsx,.ts,.tsx",
"lint:fix": "yarn lint --fix",
"lint:ci": "yarn lint --format junit -o __reports__/eslint-results.xml",
Expand Down
42 changes: 38 additions & 4 deletions src/components/TrackingElement/TrackingElement.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ const DispatchButton = ({ testId = 'dispatch-btn' }: DispatchButton) => {
};

describe('Element', () => {
it('should attach the element property when dispatching an event', () => {
it('should append the name to the elementTree when dispatching an event', () => {
const dispatch = jest.fn();
const app = '';
const view = 'test';
const element = 'test-element-spec';
const name = 'test-element';
const btn = 'dispatch-btn';
const component = 'button';

const expected = {
app,
view,
elementTree: [element],
elementTree: [name],
event: Events.click,
component,
label: undefined,
Expand All @@ -66,7 +66,41 @@ describe('Element', () => {
const { getByTestId } = render(
<TrackingRoot name={app} onDispatch={dispatch}>
<TrackingView name={view}>
<TrackingElement name={element}>
<TrackingElement name={name}>
<DispatchButton />
</TrackingElement>
</TrackingView>
</TrackingRoot>
);

fireEvent.click(getByTestId(btn));

expect(dispatch).toHaveBeenCalledWith(expected);
});

it('should append the name and label to the elementTree when dispatching an event', () => {
const dispatch = jest.fn();
const app = '';
const view = 'test';
const name = 'test-element';
const label = 'test-label';
const btn = 'dispatch-btn';
const component = 'button';

const expected = {
app,
view,
elementTree: ['test-element|test-label'],
event: Events.click,
component,
label: undefined,
timestamp: expect.any(Number)
};

const { getByTestId } = render(
<TrackingRoot name={app} onDispatch={dispatch}>
<TrackingView name={view}>
<TrackingElement name={name} label={label}>
<DispatchButton />
</TrackingElement>
</TrackingView>
Expand Down
11 changes: 8 additions & 3 deletions src/components/TrackingElement/TrackingElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ import * as React from 'react';
import { TrackingProviderProps as ProviderProps } from '../../types';
import TrackingContext from '../TrackingContext';

const TrackingElement = ({ name, children }: ProviderProps) => {
export interface TrackingElementProps extends ProviderProps {
label?: string;
}

const TrackingElement = ({ name, label, children }: TrackingElementProps) => {
const baseContext = React.useContext(TrackingContext);
const elementName = label ? `${name}|${label}` : name;
const contextValue = React.useMemo(
() => ({
...baseContext,
elementTree: [...baseContext.elementTree, name]
elementTree: [...baseContext.elementTree, elementName]
}),
[baseContext, name]
[baseContext, elementName]
);

return (
Expand Down