Skip to content

Commit

Permalink
[HEAP-42725] Return the same component each time from `withHeapNaviga…
Browse files Browse the repository at this point in the history
…tionWrapper` (#406)
  • Loading branch information
bnickel authored Sep 26, 2023
1 parent 22bf5f2 commit 1e1edc3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `Heap.withHeapNavigationWrapper` returns the same object when called
multiple times, preventing app refreshes when used within a
re-evaluated function like `App` with `useEffect`.

## [0.22.4] - 2023-09-05

### Fixed
Expand Down
24 changes: 24 additions & 0 deletions js/__tests__/Heap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,28 @@ describe('The Heap object', () => {
expect(() => Heap.clearEventProperties()).not.toThrow();
});
});

describe('withReactNavigationAutotrack', () => {

it('returns a different HOC for each passed in AppContainer', () => {

function Component1() {}
function Component2() {}

const Wrapped1 = Heap.withReactNavigationAutotrack(Component1);
const Wrapped2 = Heap.withReactNavigationAutotrack(Component2);

expect(Wrapped1).not.toStrictEqual(Wrapped2);
});

it('returns the same HOC when given the same AppContainer', () => {

function Component1() {}

const Wrapped1 = Heap.withReactNavigationAutotrack(Component1);
const Wrapped2 = Heap.withReactNavigationAutotrack(Component1);

expect(Wrapped1).toStrictEqual(Wrapped2);
});
});
});
8 changes: 7 additions & 1 deletion js/autotrack/reactNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const EVENT_TYPE = 'react_navigation_screenview';
const INITIAL_ROUTE_TYPE = 'Heap_Navigation/INITIAL';

export const withReactNavigationAutotrack = track => AppContainer => {

const existingWrapper = AppContainer.__heapWrapper;
if (existingWrapper) {
return existingWrapper;
}

const captureOldNavigationStateChange = bailOnError((prev, next, action) => {
const { screen_path: prevScreenRoute } = NavigationUtil.getActiveRouteProps(
prev
Expand Down Expand Up @@ -153,7 +159,7 @@ export const withReactNavigationAutotrack = track => AppContainer => {
AppContainer
)})`;

return React.forwardRef((props, ref) => {
return AppContainer.__heapWrapper = React.forwardRef((props, ref) => {
return <HeapNavigationWrapper {...props} forwardedRef={ref} />;
});
};

0 comments on commit 1e1edc3

Please sign in to comment.