Skip to content

Commit 1afed28

Browse files
committed
Add tests for events
1 parent 086ff66 commit 1afed28

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/pages/LoginPage/LoginButton.test.jsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { render, screen } from '@testing-library/react'
1+
import { act, render, screen } from '@testing-library/react'
22
import { MemoryRouter, Route } from 'react-router-dom'
33

4+
import { eventTracker } from 'services/events/events'
45
import { ThemeContextProvider } from 'shared/ThemeContext'
56

67
import LoginButton from './LoginButton'
78

9+
vi.mock('services/events/events')
10+
811
const wrapper =
912
({ initialEntries, path }) =>
1013
({ children }) => (
@@ -73,4 +76,27 @@ describe('LoginButton', () => {
7376
expect(sentry).toBeInTheDocument()
7477
})
7578
})
79+
80+
it('emits event on click', () => {
81+
render(<LoginButton provider="gh" />, {
82+
wrapper: wrapper({
83+
initialEntries: '/login/gh',
84+
path: '/login/:provider',
85+
}),
86+
})
87+
88+
const github = screen.getByText(/Login with GitHub/i)
89+
expect(github).toBeInTheDocument()
90+
91+
act(() => github.click())
92+
93+
expect(eventTracker().track).toHaveBeenCalledWith({
94+
type: 'Button Clicked',
95+
properties: {
96+
buttonName: 'Login',
97+
buttonLocation: 'Login Page',
98+
loginProvider: 'GitHub',
99+
},
100+
})
101+
})
76102
})

src/pages/SyncProviderPage/SyncButton.test.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { render, screen } from '@testing-library/react'
1+
import { act, render, screen } from '@testing-library/react'
22
import { MemoryRouter, Route } from 'react-router-dom'
33

4+
import { eventTracker } from 'services/events/events'
5+
46
import SyncButton from './SyncButton'
57

8+
vi.mock('services/events/events')
9+
610
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
711
<MemoryRouter initialEntries={['/sync']}>
812
<Route path="/sync">{children}</Route>
@@ -87,4 +91,22 @@ describe('SyncButton', () => {
8791
expect(link).toHaveAttribute('href', `/login/bbs?to=${expectedRedirect}`)
8892
})
8993
})
94+
95+
it('emits event on click', () => {
96+
console.error = () => {} // silence error about navigation on click
97+
render(<SyncButton provider="gh" />, { wrapper })
98+
99+
const link = screen.getByRole('link', { name: /Sync with GitHub/ })
100+
101+
act(() => link.click())
102+
103+
expect(eventTracker().track).toHaveBeenCalledWith({
104+
type: 'Button Clicked',
105+
properties: {
106+
buttonName: 'Sync',
107+
buttonLocation: 'Sync Provider Page',
108+
loginProvider: 'GitHub',
109+
},
110+
})
111+
})
90112
})

src/pages/TermsOfService/TermsOfService.test.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import config from 'config'
1111

1212
import { SentryBugReporter } from 'sentry'
1313

14+
import { eventTracker } from 'services/events/events'
1415
import { InternalUserData } from 'services/user/useInternalUser'
1516

1617
import TermsOfService from './TermsOfService'
1718

1819
vi.mock('config')
20+
vi.mock('services/events/events')
1921

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

229231
// Into the realm of testing implementation details, but I want to make sure
230232
// that the correct inputs are being sent to the server.
231-
it('Sign TOS, sends the correct inputs to the server', async () => {
233+
it('Sign TOS, sends the correct inputs to the server, emits event', async () => {
232234
const { user, mockMutationVariables } = setup({
233235
internalUserData: {
234236
@@ -264,6 +266,14 @@ describe('TermsOfService', () => {
264266
},
265267
})
266268
)
269+
270+
expect(eventTracker().track).toHaveBeenCalledWith({
271+
type: 'Button Clicked',
272+
properties: {
273+
buttonName: 'Continue',
274+
buttonLocation: 'Terms of Service',
275+
},
276+
})
267277
})
268278
})
269279

0 commit comments

Comments
 (0)