|
| 1 | +import {OrganizationFixture} from 'sentry-fixture/organization'; |
| 2 | +import {ProjectFixture} from 'sentry-fixture/project'; |
| 3 | + |
| 4 | +import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary'; |
| 5 | + |
| 6 | +import {ProjectsStore} from 'sentry/stores/projectsStore'; |
| 7 | + |
| 8 | +import {ProjectSampling} from './projectSampling'; |
| 9 | + |
| 10 | +jest.mock('@tanstack/react-virtual', () => ({ |
| 11 | + useVirtualizer: jest.fn(({count}: {count: number}) => ({ |
| 12 | + getVirtualItems: jest.fn(() => |
| 13 | + Array.from({length: count}, (_, index) => ({ |
| 14 | + key: index, |
| 15 | + index, |
| 16 | + start: index * 63, |
| 17 | + size: 63, |
| 18 | + })) |
| 19 | + ), |
| 20 | + getTotalSize: jest.fn(() => count * 63), |
| 21 | + measure: jest.fn(), |
| 22 | + })), |
| 23 | +})); |
| 24 | + |
| 25 | +describe('ProjectSampling', () => { |
| 26 | + const project = ProjectFixture({id: '1', slug: 'project-slug'}); |
| 27 | + const organization = OrganizationFixture({ |
| 28 | + slug: 'org-slug', |
| 29 | + access: ['org:write'], |
| 30 | + samplingMode: 'project', |
| 31 | + }); |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + MockApiClient.clearMockResponses(); |
| 35 | + act(() => ProjectsStore.loadInitialData([project])); |
| 36 | + |
| 37 | + MockApiClient.addMockResponse({ |
| 38 | + url: '/organizations/org-slug/sampling/project-root-counts/', |
| 39 | + body: { |
| 40 | + data: [ |
| 41 | + [ |
| 42 | + { |
| 43 | + by: {project: 'project-slug', target_project_id: '1'}, |
| 44 | + totals: 1000, |
| 45 | + series: [], |
| 46 | + }, |
| 47 | + ], |
| 48 | + ], |
| 49 | + end: '', |
| 50 | + intervals: [], |
| 51 | + start: '', |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + MockApiClient.addMockResponse({ |
| 56 | + url: '/organizations/org-slug/sampling/project-rates/', |
| 57 | + body: [{id: 1, sampleRate: 0.5}], |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + async function waitForProjectRateInput() { |
| 62 | + return screen.findByRole('spinbutton', { |
| 63 | + name: 'Sample rate for project-slug', |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + it('renders project rate inputs with initial values', async () => { |
| 68 | + // The input briefly transitions from uncontrolled to controlled as form |
| 69 | + // state initializes with the fetched project rates. |
| 70 | + jest.spyOn(console, 'error').mockImplementation(); |
| 71 | + |
| 72 | + render(<ProjectSampling />, {organization}); |
| 73 | + |
| 74 | + const input = await waitForProjectRateInput(); |
| 75 | + expect(input).toHaveValue(50); |
| 76 | + }); |
| 77 | + |
| 78 | + it('enables Reset button after changing a project rate', async () => { |
| 79 | + render(<ProjectSampling />, {organization}); |
| 80 | + |
| 81 | + const input = await waitForProjectRateInput(); |
| 82 | + expect(screen.getByRole('button', {name: 'Reset'})).toBeDisabled(); |
| 83 | + |
| 84 | + await userEvent.clear(input); |
| 85 | + await userEvent.type(input, '30'); |
| 86 | + |
| 87 | + expect(screen.getByRole('button', {name: 'Reset'})).toBeEnabled(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('resets the input back to the saved value when Reset is clicked', async () => { |
| 91 | + render(<ProjectSampling />, {organization}); |
| 92 | + |
| 93 | + const input = await waitForProjectRateInput(); |
| 94 | + await userEvent.clear(input); |
| 95 | + await userEvent.type(input, '30'); |
| 96 | + |
| 97 | + await userEvent.click(screen.getByRole('button', {name: 'Reset'})); |
| 98 | + |
| 99 | + expect(input).toHaveValue(50); |
| 100 | + }); |
| 101 | + |
| 102 | + it('shows validation error for empty value on submit', async () => { |
| 103 | + render(<ProjectSampling />, {organization}); |
| 104 | + |
| 105 | + const input = await waitForProjectRateInput(); |
| 106 | + await userEvent.clear(input); |
| 107 | + await userEvent.click(screen.getByRole('button', {name: 'Apply Changes'})); |
| 108 | + |
| 109 | + expect(await screen.findByText('Please enter a valid number')).toBeInTheDocument(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('calls the API with the correct payload on save', async () => { |
| 113 | + const putMock = MockApiClient.addMockResponse({ |
| 114 | + url: '/organizations/org-slug/sampling/project-rates/', |
| 115 | + method: 'PUT', |
| 116 | + body: [{id: 1, sampleRate: 0.3}], |
| 117 | + }); |
| 118 | + |
| 119 | + render(<ProjectSampling />, {organization}); |
| 120 | + |
| 121 | + const input = await waitForProjectRateInput(); |
| 122 | + await userEvent.clear(input); |
| 123 | + await userEvent.type(input, '30'); |
| 124 | + await userEvent.click(screen.getByRole('button', {name: 'Apply Changes'})); |
| 125 | + |
| 126 | + await waitFor(() => { |
| 127 | + expect(putMock).toHaveBeenCalledWith( |
| 128 | + '/organizations/org-slug/sampling/project-rates/', |
| 129 | + expect.objectContaining({data: [{id: 1, sampleRate: 0.3}]}) |
| 130 | + ); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('resets form to clean state after a successful save', async () => { |
| 135 | + MockApiClient.addMockResponse({ |
| 136 | + url: '/organizations/org-slug/sampling/project-rates/', |
| 137 | + method: 'PUT', |
| 138 | + body: [{id: 1, sampleRate: 0.3}], |
| 139 | + }); |
| 140 | + |
| 141 | + render(<ProjectSampling />, {organization}); |
| 142 | + |
| 143 | + const input = await waitForProjectRateInput(); |
| 144 | + await userEvent.clear(input); |
| 145 | + await userEvent.type(input, '30'); |
| 146 | + await userEvent.click(screen.getByRole('button', {name: 'Apply Changes'})); |
| 147 | + |
| 148 | + await waitFor(() => |
| 149 | + expect(screen.getByRole('button', {name: 'Reset'})).toBeDisabled() |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('keeps form dirty after an API error', async () => { |
| 154 | + MockApiClient.addMockResponse({ |
| 155 | + url: '/organizations/org-slug/sampling/project-rates/', |
| 156 | + method: 'PUT', |
| 157 | + statusCode: 500, |
| 158 | + body: {detail: 'Internal Server Error'}, |
| 159 | + }); |
| 160 | + |
| 161 | + render(<ProjectSampling />, {organization}); |
| 162 | + |
| 163 | + const input = await waitForProjectRateInput(); |
| 164 | + await userEvent.clear(input); |
| 165 | + await userEvent.type(input, '30'); |
| 166 | + await userEvent.click(screen.getByRole('button', {name: 'Apply Changes'})); |
| 167 | + |
| 168 | + await waitFor(() => |
| 169 | + expect(screen.getByRole('button', {name: 'Reset'})).toBeEnabled() |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('updates project rates atomically via bulk org rate edit', async () => { |
| 174 | + const putMock = MockApiClient.addMockResponse({ |
| 175 | + url: '/organizations/org-slug/sampling/project-rates/', |
| 176 | + method: 'PUT', |
| 177 | + body: [{id: 1, sampleRate: 0.8}], |
| 178 | + }); |
| 179 | + |
| 180 | + render(<ProjectSampling />, {organization}); |
| 181 | + |
| 182 | + await waitForProjectRateInput(); |
| 183 | + |
| 184 | + // Activate bulk edit mode |
| 185 | + await userEvent.click( |
| 186 | + screen.getByRole('button', {name: 'Proportionally scale project rates'}) |
| 187 | + ); |
| 188 | + |
| 189 | + // Type a new org rate — this should update all project rates in one atomic call |
| 190 | + const orgRateInput = screen.getAllByRole('spinbutton')[0]!; |
| 191 | + await userEvent.clear(orgRateInput); |
| 192 | + await userEvent.type(orgRateInput, '80'); |
| 193 | + |
| 194 | + // The project rate should have been scaled |
| 195 | + const projectInput = screen.getByRole('spinbutton', { |
| 196 | + name: 'Sample rate for project-slug', |
| 197 | + }); |
| 198 | + expect(projectInput).toHaveValue(80); |
| 199 | + |
| 200 | + // Submit and verify the API call |
| 201 | + await userEvent.click(screen.getByRole('button', {name: 'Apply Changes'})); |
| 202 | + |
| 203 | + await waitFor(() => { |
| 204 | + expect(putMock).toHaveBeenCalledWith( |
| 205 | + '/organizations/org-slug/sampling/project-rates/', |
| 206 | + expect.objectContaining({data: [{id: 1, sampleRate: 0.8}]}) |
| 207 | + ); |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + it('disables Apply Changes for users without org:write access', async () => { |
| 212 | + const orgWithoutAccess = OrganizationFixture({ |
| 213 | + access: [], |
| 214 | + samplingMode: 'project', |
| 215 | + }); |
| 216 | + |
| 217 | + render(<ProjectSampling />, {organization: orgWithoutAccess}); |
| 218 | + |
| 219 | + await waitForProjectRateInput(); |
| 220 | + expect(screen.getByRole('button', {name: 'Apply Changes'})).toBeDisabled(); |
| 221 | + }); |
| 222 | +}); |
0 commit comments