Skip to content

Commit

Permalink
Add path to EventContext
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmurray-codecov committed Jan 21, 2025
1 parent ac2f3d5 commit 398493b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/services/events/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ describe('useEventContext', () => {
}

describe('when called on owner page', () => {
it('sets event context with ownerid and no repoid', async () => {
it('sets event context with path, ownerid, and no repoid', async () => {
setup({})
renderHook(() => useEventContext(), {
wrapper: ownerWrapper,
})

await waitFor(() => {
expect(mockedSetContext).toHaveBeenCalledWith({
path: '/:provider/:owner',
owner: {
id: mockOwnerContext.owner.ownerid,
},
Expand All @@ -138,14 +139,15 @@ describe('useEventContext', () => {
})

describe('when called on repo page', () => {
it('sets event context with ownerid and repoid', async () => {
it('sets event context with path, ownerid, and repoid', async () => {
setup({})
renderHook(useEventContext, {
wrapper: repoWrapper,
})

await waitFor(() => {
expect(mockedSetContext).toHaveBeenCalledWith({
path: '/:provider/:owner/:repo',
owner: {
id: mockOwnerContext.owner.ownerid,
},
Expand All @@ -167,6 +169,7 @@ describe('useEventContext', () => {

await waitFor(() => {
expect(mockedSetContext).toHaveBeenCalledWith({
path: '/:provider/:owner',
owner: undefined,
repo: undefined,
})
Expand Down
5 changes: 4 additions & 1 deletion src/services/events/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
useQuery as useQueryV5,
} from '@tanstack/react-queryV5'
import { useRef } from 'react'
import { useParams } from 'react-router'
import { useParams, useRouteMatch } from 'react-router'
import { z } from 'zod'

import {
Expand All @@ -26,6 +26,7 @@ export function useEventContext() {
}>()
const context = useRef<EventContext>({})

const { path } = useRouteMatch()
const { data: ownerData } = useQueryV5(
OwnerContextQueryOpts({ provider, owner })
)
Expand All @@ -34,11 +35,13 @@ export function useEventContext() {
)

if (
path !== context.current.path ||
ownerData?.ownerid !== context.current.owner?.id ||
repoData?.repoid !== context.current.repo?.id
) {
// only update if this is a new owner or repo
const newContext: EventContext = {
path,
owner: ownerData?.ownerid
? {
id: ownerData?.ownerid,
Expand Down
1 change: 1 addition & 0 deletions src/services/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type EventContext = {
id: number
isPrivate?: boolean
}
path?: string
}

export abstract class EventTracker {
Expand Down

0 comments on commit 398493b

Please sign in to comment.