/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
/* eslint-disable react/destructuring-assignment,react/no-multi-comp,react/prop-types,react/prefer-stateless-function */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React, { useContext, useEffect, useState } from 'react';
import { act } from 'react-dom/test-utils';
import { mount } from 'enzyme';
const dispatchTrackingEvent = jest.fn();
jest.setMock('../dispatchTrackingEvent', dispatchTrackingEvent);
const testDataContext = { testDataContext: true };
const testData = { testData: true };
const dispatch = jest.fn();
const testState = { booleanState: true };
describe('hooks', () => {
// eslint-disable-next-line global-require
const { default: track, useTracking } = require('..');
beforeEach(() => {
jest.clearAllMocks();
});
it('defaults mostly everything', () => {
function TestDefaults({ children }) {
const { Track } = useTracking({}, { process: () => null });
return ;
}
const Child = () => {
const { trackEvent } = useTracking();
useEffect(() => {
trackEvent({ test: true });
}, [trackEvent]);
return 'hi';
};
mount(
);
expect(dispatchTrackingEvent).toHaveBeenCalledTimes(1);
expect(dispatchTrackingEvent).toHaveBeenCalledWith({ test: true });
});
it('defaults to dispatchTrackingEvent when no dispatch function passed in to options', () => {
const testPageData = { page: 'TestPage' };
function TestPage() {
useTracking(testPageData, { dispatchOnMount: true });
return null;
}
mount();
expect(dispatchTrackingEvent).toHaveBeenCalledWith({
...testPageData,
});
});
it('accepts a dispatch function in options', () => {
function TestOptions() {
const { trackEvent } = useTracking(testDataContext, { dispatch });
const blah = () => {
trackEvent(testData);
};
blah();
return
;
}
mount();
expect(dispatchTrackingEvent).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
...testDataContext,
...testData,
});
});
it('will use dispatch fn passed in from further up in context', () => {
const testChildData = { page: 'TestChild' };
function TestOptions({ children }) {
const { Track } = useTracking(testDataContext, { dispatch });
return ;
}
function TestChild() {
useTracking(testChildData, { dispatchOnMount: true });
return ;
}
mount(
);
expect(dispatch).toHaveBeenCalledWith({
...testDataContext,
...testChildData,
});
});
it('will deep-merge tracking data', () => {
const mockDispatchTrackingEvent = jest.fn();
const testData1 = { key: { x: 1, y: 1 } };
const testData2 = { key: { x: 2, z: 2 }, page: 'TestDeepMerge' };
function TestData1({ children }) {
const { Track } = useTracking(testData1, {
dispatch: mockDispatchTrackingEvent,
});
return ;
}
function TestData3() {
const { Track } = useTracking(
{ key: { x: 3, y: 3 } },
{ dispatchOnMount: true }
);
return (
);
}
function TestData2() {
const { Track } = useTracking(testData2);
return (
);
}
mount(
);
expect(mockDispatchTrackingEvent).toHaveBeenCalledWith({
page: 'TestDeepMerge',
key: { x: 3, y: 3, z: 2 },
});
});
it('will call dispatchOnMount as a function', () => {
const testDispatchOnMount = { test: true };
const dispatchOnMount = jest.fn(() => ({ dom: true }));
function TestComponent() {
useTracking(testDispatchOnMount, { dispatch, dispatchOnMount });
return null;
}
mount();
expect(dispatchOnMount).toHaveBeenCalledWith(testDispatchOnMount);
expect(dispatch).toHaveBeenCalledWith({ dom: true, test: true });
});
it('will dispatch a pageview event on mount on class component', () => {
function App({ children }) {
const { Track } = useTracking(
{ topLevel: true },
{
dispatch,
process: data => {
if (data.page) {
return { event: 'pageView' };
}
return null;
},
}
);
return ;
}
function Page() {
useTracking({ page: 'Page' });
return Page
;
}
mount(
);
expect(dispatch).toHaveBeenCalledWith({
topLevel: true,
event: 'pageView',
page: 'Page',
});
});
it('will dispatch a pageview event on mount on functional component', () => {
function App({ children }) {
const { Track } = useTracking(
{ topLevel: true },
{
dispatch,
process: data => {
if (data.page) {
return { event: 'pageView' };
}
return null;
},
}
);
return ;
}
function Page() {
useTracking({ page: 'Page' });
return Page
;
}
mount(
);
expect(dispatch).toHaveBeenCalledWith({
topLevel: true,
event: 'pageView',
page: 'Page',
});
});
it("should not dispatch a pageview event on mount if there's no page property on tracking object", () => {
function App({ children }) {
const { Track } = useTracking(
{ topLevel: true },
{
dispatch,
process: () => null,
}
);
return ;
}
function Page() {
useTracking({ page: 'Page' });
return Page
;
}
mount(
);
expect(dispatch).not.toHaveBeenCalled();
});
it('should not dispatch a pageview event on mount if proccess returns falsy value', () => {
function App({ children }) {
const { Track } = useTracking(
{ topLevel: true },
{
dispatch,
process: data => {
if (data.page) {
return { event: 'pageView' };
}
return false;
},
}
);
return ;
}
function Page() {
useTracking({});
return Page
;
}
mount(
);
expect(dispatch).not.toHaveBeenCalled();
});
it('will dispatch a top level pageview event on every page and component specific event on mount', () => {
function App({ children }) {
const { Track } = useTracking(
{ topLevel: true },
{
dispatch,
process: data => {
if (data.page) {
return { event: 'pageView' };
}
return null;
},
}
);
return ;
}
function Page1() {
useTracking({ page: 'Page1' });
return Page
;
}
function Page2() {
useTracking(
{ page: 'Page2' },
{ dispatchOnMount: () => ({ page2specific: true }) }
);
return Page
;
}
mount(
);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenCalledWith({
page: 'Page1',
event: 'pageView',
topLevel: true,
});
expect(dispatch).toHaveBeenCalledWith({
page: 'Page2',
event: 'pageView',
topLevel: true,
page2specific: true,
});
});
it('process works with trackingData as a function', () => {
function App({ children }) {
const { Track } = useTracking(
{ topLevel: true },
{
dispatch,
process: data => {
if (data.page) {
return { event: 'pageView' };
}
return null;
},
}
);
return ;
}
function Page({ runtimeData }) {
useTracking({ page: 'Page', runtimeData });
return Page
;
}
mount(
);
expect(dispatch).toHaveBeenCalledWith({
event: 'pageView',
page: 'Page',
runtimeData: true,
topLevel: true,
});
});
it("doesn't dispatch pageview for nested components without page tracking data", () => {
function App({ children }) {
const { Track } = useTracking(
{ topLevel: true },
{
dispatch,
process: data => {
if (data.page) {
return { event: 'pageView' };
}
return null;
},
}
);
return ;
}
function Page({ children }) {
const { Track } = useTracking({ page: 'Page' });
return ;
}
function Nested({ children }) {
const { Track } = useTracking({ view: 'View' });
return ;
}
function Button() {
const { trackEvent } = useTracking({ region: 'Button' });
const handleClick = () => {
trackEvent({ event: 'buttonClick' });
};
return (
);
}
const wrappedApp = mount(
);
wrappedApp.find('Button').simulate('click');
expect(dispatch).toHaveBeenCalledWith({
event: 'pageView',
page: 'Page',
topLevel: true,
});
expect(dispatch).toHaveBeenCalledWith({
event: 'buttonClick',
page: 'Page',
region: 'Button',
view: 'View',
topLevel: true,
});
expect(dispatch).toHaveBeenCalledTimes(2); // pageview event and simulated button click
});
it('dispatches state data when components contain state', () => {
function TestOptions() {
const [booleanState] = useState(true);
const { trackEvent } = useTracking(testDataContext, { dispatch });
const exampleMethod = () => {
trackEvent({ booleanState });
};
exampleMethod();
return ;
}
mount();
expect(dispatchTrackingEvent).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
...testDataContext,
...testState,
});
});
it('can read tracking data from props.tracking.getTrackingData()', () => {
const mockReader = jest.fn();
function TestOptions({ onProps, child, children }) {
const { Track } = useTracking({ onProps, child, ...testDataContext });
return ;
}
const TestChild = () => {
const { getTrackingData } = useTracking();
mockReader(getTrackingData());
return 'hi';
};
mount(
);
expect(mockReader).toHaveBeenCalledTimes(1);
expect(mockReader).toHaveBeenCalledWith({
child: true,
onProps: 'yes',
...testDataContext,
});
expect(dispatchTrackingEvent).not.toHaveBeenCalled();
});
it('logs a console error when there is already a process defined on context', () => {
const consoleError = jest
.spyOn(global.console, 'error')
.mockImplementation(() => {});
const process = () => {};
function NestedComponent() {
const { Track } = useTracking({}, { process });
return (
);
}
function Intermediate() {
return (
);
}
function TestComponent() {
const { Track } = useTracking({}, { process });
return (
);
}
mount();
expect(consoleError).toHaveBeenCalledTimes(1);
expect(consoleError).toHaveBeenCalledWith(
'[react-tracking] options.process should be defined once on a top-level component'
);
consoleError.mockRestore();
});
it('will dispatch different data if props changed', () => {
function Top({ data, children }) {
const { Track } = useTracking(() => ({ data }));
return ;
}
function Page() {
const { Track, trackEvent } = useTracking({ page: 'Page' });
const handleClick = () => {
trackEvent({ event: 'buttonClick' });
};
return (
);
}
function App() {
const [state, setState] = useState({ data: 1 });
const { Track } = useTracking({}, { dispatch });
return (
);
}
const wrappedApp = mount();
wrappedApp.find('span').simulate('click');
expect(dispatch).toHaveBeenCalledWith({
data: 1,
event: 'buttonClick',
page: 'Page',
});
wrappedApp.find('button').simulate('click');
wrappedApp.find('span').simulate('click');
expect(dispatch).toHaveBeenCalledWith({
data: 2,
event: 'buttonClick',
page: 'Page',
});
});
it('provides passed in tracking data immediately', () => {
function Foo() {
const { getTrackingData } = useTracking({ seeMe: true });
expect(getTrackingData()).toStrictEqual({ seeMe: true });
return null;
}
mount();
});
it('does not cause unnecessary updates due to context changes', () => {
let innerRenderCount = 0;
let getLatestTrackingData;
function OuterComponent({ children, trackedProp }) {
const { Track } = useTracking({ trackedProp });
return ;
}
const MiddleComponent = React.memo(
({ children, middleProp }) => {
const { Track } = useTracking({ middleProp });
return ;
},
(props, prevProps) => props.middleProp === prevProps.middleProp
);
function InnerComponent({ innerProps }) {
const { Track, getTrackingData } = useTracking({ innerProps });
innerRenderCount += 1;
// expose for test assertion later
getLatestTrackingData = getTrackingData;
return ;
}
function App() {
const [count, setCount] = useState(0);
const { Track } = useTracking({ count });
return (
);
}
const wrapper = mount();
wrapper.find('button').simulate('click');
wrapper.find('button').simulate('click');
expect(getLatestTrackingData()).toStrictEqual({
count: 2,
trackedProp: 2,
middleProp: 'middleProp',
innerProps: 'a',
});
expect(innerRenderCount).toEqual(1);
});
it('does not cause unnecessary dispatches due to object literals passed to useTracking', () => {
const trackRenders = jest.fn();
function App() {
// eslint-disable-next-line no-unused-vars
const [clickCount, setClickCount] = useState(0);
useEffect(() => {
// use mock function to ensure that we are counting renders, not clicks
trackRenders();
});
useTracking(
{},
{
dispatch,
dispatchOnMount: () => ({ test: true }),
}
);
return (
Extra dispatches caused by new object literals passed on re-render
);
}
const wrapper = mount();
const button = wrapper.find('button');
button.simulate('click');
button.simulate('click');
button.simulate('click');
expect(trackRenders).toHaveBeenCalledTimes(4);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenLastCalledWith({
test: true,
});
});
it('dispatches the correct data if props change', () => {
function App(props) {
const { trackEvent } = useTracking(
{ data: props.data || '' },
{
dispatch,
dispatchOnMount: true,
}
);
const handleClick = () => {
trackEvent({ event: 'click' });
};
return (
);
}
const wrapper = mount();
expect(dispatch).toHaveBeenCalledWith({ data: '' });
wrapper.setProps({ data: 'Updated data prop' });
wrapper.find('button').simulate('click');
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith({
event: 'click',
data: 'Updated data prop',
});
});
it('can interop with the HoC (where HoC is top-level)', () => {
const mockDispatchTrackingEvent = jest.fn();
const testData1 = { key: { x: 1, y: 1 }, topLevel: 'hoc' };
const testData2 = { key: { x: 2, z: 2 }, page: 'TestDeepMerge' };
// functional wrapper hoc
const TestData1 = track(testData1, { dispatch: mockDispatchTrackingEvent })(
({ children }) => children
);
// hook nested
function TestData3() {
const { Track } = useTracking(
{ key: { x: 3, y: 3 } },
{ dispatchOnMount: true }
);
return (
);
}
// hook nested
function TestData2() {
const { Track } = useTracking(testData2);
return (
);
}
mount(
);
expect(mockDispatchTrackingEvent).toHaveBeenCalledWith({
topLevel: 'hoc',
page: 'TestDeepMerge',
key: { x: 3, y: 3, z: 2 },
});
});
it('can interop with HoC (where Hook is top-level)', () => {
const mockDispatchTrackingEvent = jest.fn();
const testData1 = { key: { x: 1, y: 1 }, topLevel: 'hook' };
const testData2 = { key: { x: 2, z: 2 }, page: 'TestDeepMerge' };
// hook top-level
function TestData1({ children }) {
const { Track } = useTracking(testData1, {
dispatch: mockDispatchTrackingEvent,
});
return ;
}
// functional wrapper hoc
const TestData3 = track(
{ key: { x: 3, y: 3 } },
{ dispatchOnMount: true }
)(() => );
// decorator hoc
@track(testData2)
class TestData2 extends React.Component {
render() {
return ;
}
}
mount(
);
expect(mockDispatchTrackingEvent).toHaveBeenCalledWith({
topLevel: 'hook',
page: 'TestDeepMerge',
key: { x: 3, y: 3, z: 2 },
});
});
it('root context items are accessible to children', () => {
const ReactTrackingContext = require('../ReactTrackingContext').default; // eslint-disable-line global-require
function Child() {
const trackingContext = useContext(ReactTrackingContext);
expect(Object.keys(trackingContext.tracking)).toEqual([
'dispatch',
'getTrackingData',
'process',
]);
return ;
}
function App() {
const { Track } = useTracking();
return (
);
}
mount();
});
it('dispatches tracking events from a useTracking hook tracking object', () => {
const outerTrackingData = {
page: 'Page',
};
const Page = track(outerTrackingData, { dispatch })(
props => props.children
);
function Child() {
const tracking = useTracking();
expect(tracking.getTrackingData()).toEqual(outerTrackingData);
return (