-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
test(logs): Remove useLocation mocks, custom contexts #94837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
instead of mocking useLocation directly, use our test helpers that setup a test router. This will help with our continued transition from react router 3 to 6. Also noticed some other duplicate contexts being setup. part of getsentry/frontend-tsc#78
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Test Fails Due to Missing Router Context
The test logsTableRow.spec.tsx
removed the useLocation
mock and router setup without adding initialRouterConfig
to the render
call, unlike other tests in this commit. This omission prevents the LogRowContent
component from correctly generating trace links, as it relies on router context for href
attributes, leading to test failures.
static/app/views/explore/logs/tables/logsTableRow.spec.tsx#L1-L105
sentry/static/app/views/explore/logs/tables/logsTableRow.spec.tsx
Lines 1 to 105 in dba2d79
import {LogFixture} from 'sentry-fixture/log'; | |
import {OrganizationFixture} from 'sentry-fixture/organization'; | |
import {ProjectFixture} from 'sentry-fixture/project'; | |
import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; | |
import useStacktraceLink from 'sentry/components/events/interfaces/frame/useStacktraceLink'; | |
import ProjectsStore from 'sentry/stores/projectsStore'; | |
import {LogsAnalyticsPageSource} from 'sentry/utils/analytics/logsAnalyticsEvent'; | |
import {LogsPageParamsProvider} from 'sentry/views/explore/contexts/logs/logsPageParams'; | |
import type {TraceItemResponseAttribute} from 'sentry/views/explore/hooks/useTraceItemDetails'; | |
import {LogRowContent} from 'sentry/views/explore/logs/tables/logsTableRow'; | |
import {OurLogKnownFieldKey} from 'sentry/views/explore/logs/types'; | |
import {useExploreLogsTableRow} from 'sentry/views/explore/logs/useLogsQuery'; | |
jest.mock('sentry/views/explore/logs/useLogsQuery', () => ({ | |
useExploreLogsTableRow: jest.fn(), | |
usePrefetchLogTableRowOnHover: jest.fn().mockReturnValue({}), | |
})); | |
jest.mock('sentry/components/events/interfaces/frame/useStacktraceLink', () => ({ | |
__esModule: true, | |
default: jest.fn(), | |
})); | |
const mockedUseStacktraceLink = jest.mocked(useStacktraceLink); | |
jest.mock('sentry/utils/useRelease', () => ({ | |
useRelease: jest.fn().mockReturnValue({ | |
data: { | |
id: 10, | |
lastCommit: { | |
id: '1e5a9462e6ac23908299b218e18377837297bda1', | |
}, | |
}, | |
}), | |
})); | |
const mockedUseExploreLogsTableRow = jest.mocked(useExploreLogsTableRow); | |
function ProviderWrapper({children}: {children?: React.ReactNode}) { | |
return ( | |
<LogsPageParamsProvider analyticsPageSource={LogsAnalyticsPageSource.EXPLORE_LOGS}> | |
{children} | |
</LogsPageParamsProvider> | |
); | |
} | |
describe('logsTableRow', () => { | |
const organization = OrganizationFixture({ | |
features: ['trace-view-v1'], | |
}); | |
const projects = [ProjectFixture()]; | |
ProjectsStore.loadInitialData(projects); | |
// These are the values in the actual row - e.g., the ones loaded before you click the row | |
const rowData = LogFixture({ | |
[OurLogKnownFieldKey.ID]: '1', | |
[OurLogKnownFieldKey.PROJECT_ID]: String(projects[0]!.id), | |
[OurLogKnownFieldKey.ORGANIZATION_ID]: Number(organization.id), | |
[OurLogKnownFieldKey.MESSAGE]: 'test log body', | |
[OurLogKnownFieldKey.SEVERITY_NUMBER]: 456, | |
[OurLogKnownFieldKey.TRACE_ID]: '7b91699f', | |
}); | |
// These are the detailed attributes of the row - only displayed when you click the row. | |
const rowDetails = [ | |
...Object.entries(rowData), | |
...Object.entries({ | |
[OurLogKnownFieldKey.CODE_FUNCTION_NAME]: 'derp', | |
[OurLogKnownFieldKey.CODE_LINE_NUMBER]: '10', | |
[OurLogKnownFieldKey.CODE_FILE_PATH]: 'herp/merp/derp.py', | |
[OurLogKnownFieldKey.SDK_NAME]: 'sentry.python', | |
[OurLogKnownFieldKey.SDK_VERSION]: '2.27.0', | |
}), | |
].map( | |
([k, v]) => | |
({ | |
name: k, | |
value: v, | |
type: typeof v === 'string' ? 'str' : 'float', | |
}) as TraceItemResponseAttribute | |
); | |
it('renders row details', async () => { | |
jest.spyOn(console, 'error').mockImplementation(() => {}); | |
mockedUseExploreLogsTableRow.mockReturnValue({ | |
data: { | |
attributes: rowDetails, | |
}, | |
isPending: false, | |
} as unknown as ReturnType<typeof useExploreLogsTableRow>); | |
mockedUseStacktraceLink.mockReturnValue({ | |
data: { | |
sourceUrl: 'https://some-stacktrace-link', | |
integrations: [], | |
}, | |
error: null, | |
isPending: false, | |
} as unknown as ReturnType<typeof useStacktraceLink>); | |
render( | |
<ProviderWrapper> |
Bug: Missing Organization Context in Test Renders
The render
calls for LogsAggregateTable
tests are missing the organization
context. Previously provided by OrganizationContext.Provider
, the render
calls now only pass {initialRouterConfig}
instead of {initialRouterConfig, organization}
. This is inconsistent with other test files in the PR and may cause test failures due to the component lacking necessary context.
static/app/views/explore/logs/tables/logsAggregateTable.spec.tsx#L80-L120
sentry/static/app/views/explore/logs/tables/logsAggregateTable.spec.tsx
Lines 80 to 120 in dba2d79
}); | |
render(<LogsAggregateTableWithParamsProvider />, {initialRouterConfig}); | |
expect(screen.getByLabelText('Aggregates')).toBeInTheDocument(); | |
}); | |
it('renders error state', () => { | |
(useLogsQueryModule.useLogsAggregatesQuery as jest.Mock).mockReturnValue({ | |
isLoading: false, | |
error: 'Error!', | |
data: null, | |
pageLinks: undefined, | |
}); | |
render(<LogsAggregateTableWithParamsProvider />, {initialRouterConfig}); | |
expect(screen.getByTestId('error-indicator')).toBeInTheDocument(); | |
}); | |
it('renders data rows', () => { | |
(useLogsQueryModule.useLogsAggregatesQuery as jest.Mock).mockReturnValue({ | |
isLoading: false, | |
error: null, | |
data: { | |
data: [ | |
{ | |
'message.template': 'Fetching the latest item id failed.', | |
'p99(severity_number)': 17.0, | |
}, | |
{ | |
'message.template': | |
'/usr/src/sentry/src/sentry/db/models/manager/base.py:282: derp', | |
'p99(severity_number)': 13.0, | |
}, | |
{ | |
'message.template': | |
'/usr/src/sentry/src/sentry/db/models/manager/base.py:282: herp', | |
'p99(severity_number)': 12.0, | |
}, | |
], | |
}, | |
pageLinks: undefined, | |
}); | |
render(<LogsAggregateTableWithParamsProvider />, {initialRouterConfig}); |
Was this report helpful? Give feedback by reacting with 👍 or 👎
instead of mocking useLocation directly, use our test helpers that setup a test router. This will help with our continued transition from react router 3 to 6.
Also noticed some other duplicate contexts being setup.
part of https://github.com/getsentry/frontend-tsc/issues/78