Skip to content

Commit

Permalink
test case for hooks-only usage (#213)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeremy Gayed <[email protected]>
  • Loading branch information
tizmagik and tizmagik authored Jun 1, 2023
1 parent 4d39ef8 commit 5f637a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/__tests__/e2e.test.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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 (
<button
type="button"
onClick={() => {
trackEvent({ event: 'buttonClick' });
}}
>
Click me
</button>
);
}

function App() {
const { Track } = useTracking(
{ page: 'App' },
{ dispatch, dispatchOnMount: true }
);
return (
<Track>
<MyButton />
</Track>
);
}

const wrappedApp = mount(<App />);
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', () => {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"allowJs": true
"allowJs": true,
"jsx": "react-jsx"
},
"exclude": ["node_modules"]
}

0 comments on commit 5f637a1

Please sign in to comment.