|
| 1 | +import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; |
| 2 | + |
| 3 | +import {SlowestFunctionsTable} from 'sentry/views/profiling/landing/slowestFunctionsTable'; |
| 4 | + |
| 5 | +describe('SlowestFunctionsTable', () => { |
| 6 | + it('shows loading state', () => { |
| 7 | + MockApiClient.addMockResponse({ |
| 8 | + url: '/organizations/org-slug/profiling/flamegraph/', |
| 9 | + body: [], |
| 10 | + }); |
| 11 | + |
| 12 | + render(<SlowestFunctionsTable />); |
| 13 | + expect(screen.getByTestId('loading-indicator')).toBeInTheDocument(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('shows error state', async () => { |
| 17 | + MockApiClient.addMockResponse({ |
| 18 | + url: '/organizations/org-slug/profiling/flamegraph/', |
| 19 | + body: [], |
| 20 | + statusCode: 500, |
| 21 | + }); |
| 22 | + |
| 23 | + render(<SlowestFunctionsTable />); |
| 24 | + expect(await screen.findByTestId('error-indicator')).toBeInTheDocument(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('shows no functions state', async () => { |
| 28 | + // @ts-expect-error partial schema mock |
| 29 | + const schema: Profiling.Schema = { |
| 30 | + metrics: [], |
| 31 | + }; |
| 32 | + |
| 33 | + MockApiClient.addMockResponse({ |
| 34 | + url: '/organizations/org-slug/profiling/flamegraph/', |
| 35 | + match: [ |
| 36 | + MockApiClient.matchQuery({ |
| 37 | + expand: 'metrics', |
| 38 | + }), |
| 39 | + ], |
| 40 | + body: schema, |
| 41 | + }); |
| 42 | + |
| 43 | + render(<SlowestFunctionsTable />); |
| 44 | + expect(await screen.findByText('No functions found')).toBeInTheDocument(); |
| 45 | + }); |
| 46 | + it('renders function fields', async () => { |
| 47 | + // @ts-expect-error partial schema mock |
| 48 | + const schema: Profiling.Schema = { |
| 49 | + metrics: [ |
| 50 | + { |
| 51 | + name: 'slow-function', |
| 52 | + package: 'slow-package', |
| 53 | + p75: 1500 * 1e6, |
| 54 | + p95: 2000 * 1e6, |
| 55 | + p99: 3000 * 1e6, |
| 56 | + sum: 60_000 * 1e6, |
| 57 | + count: 5000, |
| 58 | + avg: 0.5 * 1e6, |
| 59 | + in_app: true, |
| 60 | + fingerprint: 12345, |
| 61 | + examples: [ |
| 62 | + { |
| 63 | + project_id: 1, |
| 64 | + profile_id: 'profile-id', |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + ], |
| 69 | + }; |
| 70 | + |
| 71 | + MockApiClient.addMockResponse({ |
| 72 | + url: '/organizations/org-slug/profiling/flamegraph/', |
| 73 | + match: [ |
| 74 | + MockApiClient.matchQuery({ |
| 75 | + expand: 'metrics', |
| 76 | + }), |
| 77 | + ], |
| 78 | + body: schema, |
| 79 | + }); |
| 80 | + |
| 81 | + render(<SlowestFunctionsTable />); |
| 82 | + for (const value of [ |
| 83 | + 'slow-function', |
| 84 | + 'slow-package', |
| 85 | + '5k', |
| 86 | + '1.50s', |
| 87 | + '2.00s', |
| 88 | + '3.00s', |
| 89 | + '1.00min', |
| 90 | + ]) { |
| 91 | + expect(await screen.findByText(value)).toBeInTheDocument(); |
| 92 | + } |
| 93 | + }); |
| 94 | + it('paginates response', async () => { |
| 95 | + // @ts-expect-error partial schema mock |
| 96 | + const schema: Profiling.Schema = { |
| 97 | + metrics: [], |
| 98 | + }; |
| 99 | + |
| 100 | + for (let i = 0; i < 10; i++) { |
| 101 | + schema.metrics?.push({ |
| 102 | + name: 'slow-function', |
| 103 | + package: 'slow-package', |
| 104 | + p75: 1500 * 1e6, |
| 105 | + p95: 2000 * 1e6, |
| 106 | + p99: 3000 * 1e6, |
| 107 | + sum: 60_000 * 1e6, |
| 108 | + count: 5000, |
| 109 | + avg: 0.5 * 1e6, |
| 110 | + in_app: true, |
| 111 | + fingerprint: 12345, |
| 112 | + examples: [ |
| 113 | + { |
| 114 | + project_id: 1, |
| 115 | + profile_id: 'profile-id', |
| 116 | + }, |
| 117 | + ], |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + MockApiClient.addMockResponse({ |
| 122 | + url: '/organizations/org-slug/profiling/flamegraph/', |
| 123 | + match: [ |
| 124 | + MockApiClient.matchQuery({ |
| 125 | + expand: 'metrics', |
| 126 | + }), |
| 127 | + ], |
| 128 | + body: schema, |
| 129 | + }); |
| 130 | + |
| 131 | + render(<SlowestFunctionsTable />); |
| 132 | + expect(await screen.findAllByText('slow-function')).toHaveLength(5); |
| 133 | + }); |
| 134 | + |
| 135 | + it('paginates results', async () => { |
| 136 | + // @ts-expect-error partial schema mock |
| 137 | + const schema: Profiling.Schema = { |
| 138 | + metrics: [], |
| 139 | + }; |
| 140 | + |
| 141 | + for (let i = 0; i < 10; i++) { |
| 142 | + schema.metrics?.push({ |
| 143 | + name: 'slow-function-' + i, |
| 144 | + package: 'slow-package', |
| 145 | + p75: 1500 * 1e6, |
| 146 | + p95: 2000 * 1e6, |
| 147 | + p99: 3000 * 1e6, |
| 148 | + sum: 60_000 * 1e6, |
| 149 | + count: 5000, |
| 150 | + avg: 0.5 * 1e6, |
| 151 | + in_app: true, |
| 152 | + fingerprint: 12345, |
| 153 | + examples: [ |
| 154 | + { |
| 155 | + project_id: 1, |
| 156 | + profile_id: 'profile-id', |
| 157 | + }, |
| 158 | + ], |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + MockApiClient.addMockResponse({ |
| 163 | + url: '/organizations/org-slug/profiling/flamegraph/', |
| 164 | + match: [ |
| 165 | + MockApiClient.matchQuery({ |
| 166 | + expand: 'metrics', |
| 167 | + }), |
| 168 | + ], |
| 169 | + body: schema, |
| 170 | + }); |
| 171 | + |
| 172 | + render(<SlowestFunctionsTable />); |
| 173 | + expect(await screen.findAllByText('slow-package')).toHaveLength(5); |
| 174 | + |
| 175 | + userEvent.click(screen.getByLabelText('Next')); |
| 176 | + for (let i = 6; i < 10; i++) { |
| 177 | + expect(await screen.findByText('slow-function-' + i)).toBeInTheDocument(); |
| 178 | + } |
| 179 | + expect(screen.getByLabelText('Next')).toBeDisabled(); |
| 180 | + |
| 181 | + userEvent.click(screen.getByLabelText('Previous')); |
| 182 | + for (let i = 0; i < 5; i++) { |
| 183 | + expect(await screen.findByText('slow-function-' + i)).toBeInTheDocument(); |
| 184 | + } |
| 185 | + expect(screen.getByLabelText('Previous')).toBeDisabled(); |
| 186 | + }); |
| 187 | +}); |
0 commit comments