From 5f637a10d7d1690b740bd4e0c7b158a05728f8e2 Mon Sep 17 00:00:00 2001 From: Jeremy Gayed <244704+tizmagik@users.noreply.github.com> Date: Wed, 31 May 2023 21:57:45 -0400 Subject: [PATCH] test case for hooks-only usage (#213) Co-authored-by: Jeremy Gayed --- src/__tests__/e2e.test.js | 44 +++++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 ++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/__tests__/e2e.test.js b/src/__tests__/e2e.test.js index 1bef9d8..f68f122 100644 --- a/src/__tests__/e2e.test.js +++ b/src/__tests__/e2e.test.js @@ -1,3 +1,4 @@ +// @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 */ @@ -941,6 +942,49 @@ const runTests = useBuiltLib => { expect(parent.instance().child).not.toBeNull(); expect(focusFn).toHaveBeenCalledTimes(1); }); + + it('can establish tracking context with only hooks', () => { + function MyButton() { + const { trackEvent } = useTracking({ element: 'MyButton' }); + 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', () => { diff --git a/tsconfig.json b/tsconfig.json index 4f8aba6..afa55c8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,8 @@ { "compilerOptions": { "experimentalDecorators": true, - "allowJs": true + "allowJs": true, + "jsx": "react-jsx" }, "exclude": ["node_modules"] }