-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
567b39f
commit 2514383
Showing
6 changed files
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
opentrons-ai-client/src/resources/hooks/__tests__/useTrackEvent.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { describe, it, vi, expect, afterEach } from 'vitest' | ||
import { trackEvent } from '../../../analytics/mixpanel' | ||
import { useTrackEvent } from '../useTrackEvent' | ||
import { renderHook } from '@testing-library/react' | ||
import { mixpanelAtom } from '../../atoms' | ||
import type { AnalyticsEvent } from '../../../analytics/mixpanel' | ||
import type { Mixpanel } from '../../types' | ||
import { TestProvider } from '../../utils/testUtils' | ||
|
||
vi.mock('../../../analytics/mixpanel', () => ({ | ||
trackEvent: vi.fn(), | ||
})) | ||
|
||
describe('useTrackEvent', () => { | ||
afterEach(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
it('should call trackEvent with the correct arguments when hasOptedIn is true', () => { | ||
const mockMixpanelAtom: Mixpanel = { | ||
analytics: { | ||
hasOptedIn: true, | ||
}, | ||
} | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<TestProvider initialValues={[[mixpanelAtom, mockMixpanelAtom]]}> | ||
{children} | ||
</TestProvider> | ||
) | ||
|
||
const { result } = renderHook(() => useTrackEvent(), { wrapper }) | ||
|
||
const event: AnalyticsEvent = { name: 'test_event', properties: {} } | ||
result.current(event) | ||
|
||
expect(trackEvent).toHaveBeenCalledWith(event, true) | ||
}) | ||
|
||
it('should call trackEvent with the correct arguments when hasOptedIn is false', () => { | ||
const mockMixpanelAtomFalse: Mixpanel = { | ||
analytics: { | ||
hasOptedIn: false, | ||
}, | ||
} | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<TestProvider initialValues={[[mixpanelAtom, mockMixpanelAtomFalse]]}> | ||
{children} | ||
</TestProvider> | ||
) | ||
|
||
const { result } = renderHook(() => useTrackEvent(), { wrapper }) | ||
|
||
const event: AnalyticsEvent = { name: 'test_event', properties: {} } | ||
result.current(event) | ||
|
||
expect(trackEvent).toHaveBeenCalledWith(event, false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Provider } from 'jotai' | ||
import { useHydrateAtoms } from 'jotai/utils' | ||
|
||
interface HydrateAtomsProps { | ||
initialValues: Array<[any, any]> | ||
children: React.ReactNode | ||
} | ||
|
||
interface TestProviderProps { | ||
initialValues: Array<[any, any]> | ||
children: React.ReactNode | ||
} | ||
|
||
export const HydrateAtoms = ({ | ||
initialValues, | ||
children, | ||
}: HydrateAtomsProps): React.ReactNode => { | ||
useHydrateAtoms(initialValues) | ||
return children | ||
} | ||
|
||
export const TestProvider = ({ | ||
initialValues, | ||
children, | ||
}: TestProviderProps): React.ReactNode => ( | ||
<Provider> | ||
<HydrateAtoms initialValues={initialValues}>{children}</HydrateAtoms> | ||
</Provider> | ||
) |