Skip to content

Commit

Permalink
Add tests for events
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmurray-codecov committed Jan 28, 2025
1 parent 086ff66 commit 1afed28
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/pages/LoginPage/LoginButton.test.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { render, screen } from '@testing-library/react'
import { act, render, screen } from '@testing-library/react'
import { MemoryRouter, Route } from 'react-router-dom'

import { eventTracker } from 'services/events/events'
import { ThemeContextProvider } from 'shared/ThemeContext'

import LoginButton from './LoginButton'

vi.mock('services/events/events')

const wrapper =
({ initialEntries, path }) =>
({ children }) => (
Expand Down Expand Up @@ -73,4 +76,27 @@ describe('LoginButton', () => {
expect(sentry).toBeInTheDocument()
})
})

it('emits event on click', () => {
render(<LoginButton provider="gh" />, {
wrapper: wrapper({
initialEntries: '/login/gh',
path: '/login/:provider',
}),
})

const github = screen.getByText(/Login with GitHub/i)
expect(github).toBeInTheDocument()

act(() => github.click())

expect(eventTracker().track).toHaveBeenCalledWith({
type: 'Button Clicked',
properties: {
buttonName: 'Login',
buttonLocation: 'Login Page',
loginProvider: 'GitHub',
},
})
})
})
24 changes: 23 additions & 1 deletion src/pages/SyncProviderPage/SyncButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { render, screen } from '@testing-library/react'
import { act, render, screen } from '@testing-library/react'
import { MemoryRouter, Route } from 'react-router-dom'

import { eventTracker } from 'services/events/events'

import SyncButton from './SyncButton'

vi.mock('services/events/events')

const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<MemoryRouter initialEntries={['/sync']}>
<Route path="/sync">{children}</Route>
Expand Down Expand Up @@ -87,4 +91,22 @@ describe('SyncButton', () => {
expect(link).toHaveAttribute('href', `/login/bbs?to=${expectedRedirect}`)
})
})

it('emits event on click', () => {
console.error = () => {} // silence error about navigation on click
render(<SyncButton provider="gh" />, { wrapper })

const link = screen.getByRole('link', { name: /Sync with GitHub/ })

act(() => link.click())

expect(eventTracker().track).toHaveBeenCalledWith({
type: 'Button Clicked',
properties: {
buttonName: 'Sync',
buttonLocation: 'Sync Provider Page',
loginProvider: 'GitHub',
},
})
})
})
12 changes: 11 additions & 1 deletion src/pages/TermsOfService/TermsOfService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import config from 'config'

import { SentryBugReporter } from 'sentry'

import { eventTracker } from 'services/events/events'
import { InternalUserData } from 'services/user/useInternalUser'

import TermsOfService from './TermsOfService'

vi.mock('config')
vi.mock('services/events/events')

const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
Expand Down Expand Up @@ -228,7 +230,7 @@ describe('TermsOfService', () => {

// Into the realm of testing implementation details, but I want to make sure
// that the correct inputs are being sent to the server.
it('Sign TOS, sends the correct inputs to the server', async () => {
it('Sign TOS, sends the correct inputs to the server, emits event', async () => {
const { user, mockMutationVariables } = setup({
internalUserData: {
email: '[email protected]',
Expand Down Expand Up @@ -264,6 +266,14 @@ describe('TermsOfService', () => {
},
})
)

expect(eventTracker().track).toHaveBeenCalledWith({
type: 'Button Clicked',
properties: {
buttonName: 'Continue',
buttonLocation: 'Terms of Service',
},
})
})
})

Expand Down

0 comments on commit 1afed28

Please sign in to comment.