123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {ProjectFixture} from 'sentry-fixture/project';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {makeTestQueryClient} from 'sentry-test/queryClient';
- import {act, renderHook, waitFor} from 'sentry-test/reactTestingLibrary';
- import PageFiltersStore from 'sentry/stores/pageFiltersStore';
- import ProjectsStore from 'sentry/stores/projectsStore';
- import type {Organization} from 'sentry/types/organization';
- import {QueryClientProvider} from 'sentry/utils/queryClient';
- import type {TraceResult} from 'sentry/views/explore/hooks/useTraces';
- import type {SpanResults} from 'sentry/views/explore/hooks/useTraceSpans';
- import {OrganizationContext} from 'sentry/views/organizationContext';
- import {useTraceSpans} from './useTraceSpans';
- function createTraceResult(trace?: Partial<TraceResult>): TraceResult {
- return {
- breakdowns: [],
- duration: 333,
- rootDuration: 333,
- end: 456,
- matchingSpans: 1,
- name: 'name',
- numErrors: 1,
- numOccurrences: 1,
- numSpans: 2,
- project: 'project',
- slices: 10,
- start: 123,
- trace: '00000000000000000000000000000000',
- ...trace,
- };
- }
- function createWrapper(organization: Organization) {
- return function ({children}: {children?: React.ReactNode}) {
- return (
- <QueryClientProvider client={makeTestQueryClient()}>
- <OrganizationContext.Provider value={organization}>
- {children}
- </OrganizationContext.Provider>
- </QueryClientProvider>
- );
- };
- }
- describe('useTraceSpans', function () {
- const project = ProjectFixture();
- const organization = OrganizationFixture();
- const context = initializeOrg({
- organization,
- projects: [project],
- router: {
- location: {
- pathname: '/organizations/org-slug/issues/',
- query: {project: project.id},
- },
- params: {},
- },
- });
- beforeEach(function () {
- MockApiClient.clearMockResponses();
- act(() => {
- ProjectsStore.loadInitialData([project]);
- PageFiltersStore.init();
- PageFiltersStore.onInitializeUrlState(
- {
- projects: [project].map(p => parseInt(p.id, 10)),
- environments: [],
- datetime: {
- period: '3d',
- start: null,
- end: null,
- utc: null,
- },
- },
- new Set()
- );
- });
- });
- it('handles querying the api', async function () {
- const trace = createTraceResult();
- const body: SpanResults<'id'> = {
- data: [{id: '0000000000000000'}],
- meta: {},
- };
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/trace/${trace.trace}/spans/`,
- body,
- match: [
- MockApiClient.matchQuery({
- project: [parseInt(project.id, 10)],
- field: ['id'],
- maxSpansPerTrace: 10,
- query: 'foo:bar',
- statsPeriod: '3d',
- }),
- ],
- });
- const {result} = renderHook(useTraceSpans, {
- ...context,
- wrapper: createWrapper(organization),
- initialProps: {
- fields: ['id'],
- trace,
- datetime: {
- end: null,
- period: '3d',
- start: null,
- utc: null,
- },
- query: 'foo:bar',
- },
- });
- await waitFor(() => result.current.isSuccess);
- expect(result.current.data).toEqual(body);
- });
- });
|