// @ts-check /* eslint-disable react/destructuring-assignment,react/no-multi-comp,react/prop-types,react/prefer-stateless-function */ /* eslint-disable jsx-a11y/control-has-associated-label */ /* eslint-disable global-require */ import React, { useContext } from 'react'; import { mount } from 'enzyme'; import PropTypes from 'prop-types'; import hoistNonReactStatics from 'hoist-non-react-statics'; const dispatchTrackingEvent = jest.fn(); window.dataLayer = []; window.dataLayer.push = dispatchTrackingEvent; const testDataContext = { testDataContext: true }; const testData = { testData: true }; const dispatch = jest.fn(); const testState = { booleanState: true }; const runTests = useBuiltLib => { const { default: track, useTracking } = useBuiltLib ? require('../../build') : require('..'); beforeEach(() => { jest.clearAllMocks(); }); it('defaults mostly everything', () => { @track(null, { process: () => null }) class TestDefaults extends React.Component { render() { return this.props.children; } } @track() class Child extends React.Component { componentDidMount() { this.props.tracking.trackEvent({ test: true }); } render() { return 'hi'; } } mount( ); expect(dispatchTrackingEvent).toHaveBeenCalledTimes(1); expect(dispatchTrackingEvent).toHaveBeenCalledWith({ test: true }); }); it('allows tracking errors', () => { @track(null, { mergeOptions: { isMergeableObject: obj => !(obj instanceof Error), }, }) class TestPage extends React.Component { componentDidMount() { this.props.tracking.trackEvent({ test: new Error('my crazy error') }); } render() { return 'hi'; } } mount(); expect(dispatchTrackingEvent).toHaveBeenCalledTimes(1); expect(dispatchTrackingEvent).toHaveBeenCalledWith({ test: new Error('my crazy error'), }); }); it('defaults to dispatchTrackingEvent when no dispatch function passed in to options', () => { const testPageData = { page: 'TestPage' }; @track(testPageData, { dispatchOnMount: true }) class TestPage extends React.Component { render() { return null; } } mount(); expect(dispatchTrackingEvent).toHaveBeenCalledWith({ ...testPageData, }); }); it('accepts a dispatch function in options', () => { @track(testDataContext, { dispatch }) class TestOptions extends React.Component { @track(testData) blah = () => {}; render() { this.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' }; @track(testDataContext, { dispatch }) class TestOptions extends React.Component { render() { return this.props.children; } } @track(testChildData, { dispatchOnMount: true }) class TestChild extends React.Component { render() { return
; } } mount( ); expect(dispatch).toHaveBeenCalledWith({ ...testDataContext, ...testChildData, }); }); it('will deep-merge tracking data', () => { const testData1 = { key: { x: 1, y: 1 } }; const testData2 = { key: { x: 2, z: 2 }, page: 'TestDeepMerge' }; @track(testData1) class TestData1 extends React.Component { render() { return this.props.children; } } const TestData3 = track( { key: { x: 3, y: 3 } }, { dispatchOnMount: true } )(() =>
); const TestData2 = track(testData2)(() => ); mount( ); expect(dispatchTrackingEvent).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 })); @track(testDispatchOnMount, { dispatch, dispatchOnMount }) class TestComponent extends React.Component { render() { 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 RawApp({ children }) { return
{children}
; } const App = track( { topLevel: true }, { dispatch, process: data => { if (data.page) { return { event: 'pageView' }; } return null; }, } )(RawApp); @track({ page: 'Page' }) class Page extends React.Component { render() { return
Page
; } } mount( ); expect(dispatch).toHaveBeenCalledWith({ topLevel: true, event: 'pageView', page: 'Page', }); }); it('will dispatch a pageview event on mount on functional component', () => { function RawApp({ children }) { return
{children}
; } const App = track( { topLevel: true }, { dispatch, process: data => { if (data.page) { return { event: 'pageView' }; } return null; }, } )(RawApp); const Page = track({ page: 'Page' })(() =>
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 RawApp({ children }) { return
{children}
; } const App = track( { topLevel: true }, { dispatch, process: () => null, } )(RawApp); const Page = track({ page: 'Page' })(() =>
Page
); mount( ); expect(dispatch).not.toHaveBeenCalled(); }); it('should not dispatch a pageview event on mount if proccess returns falsy value', () => { function RawApp({ children }) { return
{children}
; } const App = track( { topLevel: true }, { dispatch, process: data => { if (data.page) { return { event: 'pageView' }; } return false; }, } )(RawApp); const Page = track({})(() =>
Page
); mount( ); expect(dispatch).not.toHaveBeenCalled(); }); it('will dispatch a top level pageview event on every page and component specific event on mount', () => { function RawApp({ children }) { return
{children}
; } const App = track( { topLevel: true }, { dispatch, process: data => { if (data.page) { return { event: 'pageView' }; } return null; }, } )(RawApp); @track({ page: 'Page1' }) class Page1 extends React.Component { render() { return
Page
; } } @track( { page: 'Page2' }, { dispatchOnMount: () => ({ page2specific: true }) } ) class Page2 extends React.Component { render() { 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 RawApp({ children }) { return
{children}
; } const App = track( { topLevel: true }, { dispatch, process: data => { if (data.page) { return { event: 'pageView' }; } return null; }, } )(RawApp); @track(({ runtimeData }) => ({ page: 'Page', runtimeData })) class Page extends React.Component { render() { 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 RawApp({ children }) { return
{children}
; } const App = track( { topLevel: true }, { dispatch, process: data => { if (data.page) { return { event: 'pageView' }; } return null; }, } )(RawApp); @track({ page: 'Page' }) class Page extends React.Component { render() { return
{this.props.children}
; } } @track({ view: 'View' }) class Nested extends React.Component { render() { return
{this.props.children}
; } } @track({ region: 'Button' }) class Button extends React.Component { @track({ event: 'buttonClick' }) handleClick = jest.fn(); render() { return ( ); } } const wrappedApp = mount(
); } } 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('does not cause unnecessary updates due to context changes', () => { let innerRenderCount = 0; const OuterComponent = track()(props => props.children); const MiddleComponent = track()( React.memo( props => props.children, (props, prevProps) => props.middleProp === prevProps.middleProp ) ); const InnerComponent = track()(() => { innerRenderCount += 1; return null; }); const App = track()(() => { const [state, setState] = React.useState({}); return (

Extra renders of InnerComponent caused by new context API

); }); const wrapper = mount(); wrapper.find('button').simulate('click'); expect(innerRenderCount).toEqual(1); }); it('does not prevent components using the legacy context API and hoist-non-react-statics < v3.1.0 from receiving updates', () => { const withLegacyContext = DecoratedComponent => { class WithLegacyContext extends React.Component { static contextTypes = { theme: PropTypes.string }; render() { return ( ); } } hoistNonReactStatics(WithLegacyContext, DecoratedComponent); // Explicitly hoist statc contextType to simulate behavior of // hoist-non-react-statics versions older than v3.1.0 WithLegacyContext.contextType = DecoratedComponent.contextType; return WithLegacyContext; }; @track() class Top extends React.Component { render() { return this.props.children; } } @withLegacyContext @track({ page: 'Page' }, { dispatchOnMount: true }) class Page extends React.Component { render() { return {this.props.theme}; } } @track() class App extends React.Component { static childContextTypes = { theme: PropTypes.string }; constructor(props) { super(props); this.state = { theme: 'light' }; } getChildContext() { return { theme: this.state.theme }; } handleUpdateTheme = () => { this.setState({ theme: 'dark' }); }; render() { return (
); } } const wrapper = mount(); expect(wrapper.find('span').text()).toBe('light'); wrapper.find('button').simulate('click'); expect(wrapper.find('span').text()).toBe('dark'); }); it('root context items are accessible to children', () => { const ReactTrackingContext = ( useBuiltLib ? require('../../build/ReactTrackingContext') : require('../ReactTrackingContext') ).default; const App = track()(() => ); function Child() { const trackingContext = useContext(ReactTrackingContext); expect(Object.keys(trackingContext.tracking)).toEqual([ 'dispatch', 'getTrackingData', 'process', ]); 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 ( ); } function App() { const { Track } = useTracking( { page: 'App' }, { dispatch, dispatchOnMount: true } ); return ( ); } const wrappedApp = mount(); wrappedApp.find('button').simulate('click'); expect(dispatch).toHaveBeenCalledTimes(2); // dispatch on mount expect(dispatch).toHaveBeenCalledWith({ page: 'App', }); // simulated button click expect(dispatch).toHaveBeenCalledWith({ page: 'App', event: 'buttonClick', element: 'MyButton', }); }); }; describe('e2e', () => { if (process.env.SKIP_BUILT_LIB_CHECK !== 'true') { describe('with built lib', () => { runTests(true); }); } describe('with source lib', () => { runTests(false); }); });