|
| 1 | +import {useRef} from 'react'; |
| 2 | +import {OrganizationFixture} from 'sentry-fixture/organization'; |
| 3 | + |
| 4 | +import {render, screen} from 'sentry-test/reactTestingLibrary'; |
| 5 | + |
| 6 | +import { |
| 7 | + TraceViewLogsDataProvider, |
| 8 | + TraceViewLogsSection, |
| 9 | +} from 'sentry/views/performance/newTraceDetails/traceOurlogs'; |
| 10 | + |
| 11 | +const TRACE_SLUG = '00000000000000000000000000000000'; |
| 12 | + |
| 13 | +function Component({traceSlug}: {traceSlug: string}) { |
| 14 | + const ref = useRef(null); |
| 15 | + return ( |
| 16 | + <TraceViewLogsDataProvider traceSlug={traceSlug}> |
| 17 | + <TraceViewLogsSection scrollContainer={ref} /> |
| 18 | + </TraceViewLogsDataProvider> |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +describe('TraceViewLogsSection', function () { |
| 23 | + beforeEach(function () { |
| 24 | + // the search query combobox is firing updates and causing console.errors |
| 25 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(function () { |
| 29 | + jest.clearAllMocks(); |
| 30 | + MockApiClient.clearMockResponses(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('renders empty logs', async function () { |
| 34 | + const organization = OrganizationFixture({features: ['ourlogs-enabled']}); |
| 35 | + const mockRequest = MockApiClient.addMockResponse({ |
| 36 | + url: `/organizations/${organization.slug}/trace-logs/`, |
| 37 | + body: { |
| 38 | + data: [], |
| 39 | + meta: {}, |
| 40 | + }, |
| 41 | + }); |
| 42 | + render(<Component traceSlug={TRACE_SLUG} />, {organization}); |
| 43 | + |
| 44 | + expect(screen.getByTestId('loading-indicator')).toBeInTheDocument(); |
| 45 | + |
| 46 | + expect(mockRequest).toHaveBeenCalledTimes(1); |
| 47 | + |
| 48 | + // without waiting a few ticks, the test fails just before the |
| 49 | + // promise corresponding to the request resolves |
| 50 | + // by adding some ticks, it forces the test to wait a little longer |
| 51 | + // until the promise is resolved |
| 52 | + for (let i = 0; i < 10; i++) { |
| 53 | + await tick(); |
| 54 | + } |
| 55 | + |
| 56 | + expect(screen.getByText(/No logs found/)).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders some logs', async function () { |
| 60 | + const now = new Date(); |
| 61 | + const organization = OrganizationFixture({features: ['ourlogs-enabled']}); |
| 62 | + const mockRequest = MockApiClient.addMockResponse({ |
| 63 | + url: `/organizations/${organization.slug}/trace-logs/`, |
| 64 | + body: { |
| 65 | + data: [ |
| 66 | + { |
| 67 | + 'sentry.item_id': '11111111111111111111111111111111', |
| 68 | + 'project.id': 1, |
| 69 | + trace: TRACE_SLUG, |
| 70 | + severity_number: 0, |
| 71 | + severity: 'info', |
| 72 | + timestamp: now.toISOString(), |
| 73 | + 'tags[sentry.timestamp_precise,number]': now.getTime() * 1e6, |
| 74 | + message: 'i am a log', |
| 75 | + }, |
| 76 | + ], |
| 77 | + meta: {}, |
| 78 | + }, |
| 79 | + }); |
| 80 | + render(<Component traceSlug={TRACE_SLUG} />, {organization}); |
| 81 | + |
| 82 | + expect(screen.getByTestId('loading-indicator')).toBeInTheDocument(); |
| 83 | + |
| 84 | + expect(mockRequest).toHaveBeenCalledTimes(1); |
| 85 | + |
| 86 | + // without waiting a few ticks, the test fails just before the |
| 87 | + // promise corresponding to the request resolves |
| 88 | + // by adding some ticks, it forces the test to wait a little longer |
| 89 | + // until the promise is resolved |
| 90 | + for (let i = 0; i < 10; i++) { |
| 91 | + await tick(); |
| 92 | + } |
| 93 | + |
| 94 | + expect(screen.getByText(/i am a log/)).toBeInTheDocument(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments