123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import {
- fireEvent,
- render,
- renderGlobalModal,
- screen,
- userEvent,
- } from 'sentry-test/reactTestingLibrary';
- import * as utils from 'sentry/utils/isActiveSuperuser';
- import ProjectPerformance, {
- allowedDurationValues,
- } from 'sentry/views/settings/projectPerformance/projectPerformance';
- describe('projectPerformance', function () {
- const org = TestStubs.Organization({
- features: [
- 'performance-view',
- 'performance-issues-dev',
- 'project-performance-settings-admin',
- ],
- });
- const project = TestStubs.ProjectDetails();
- const configUrl = '/projects/org-slug/project-slug/transaction-threshold/configure/';
- let getMock, postMock, deleteMock, performanceIssuesMock;
- beforeEach(function () {
- MockApiClient.clearMockResponses();
- getMock = MockApiClient.addMockResponse({
- url: configUrl,
- method: 'GET',
- body: {
- id: project.id,
- threshold: '300',
- metric: 'duration',
- },
- statusCode: 200,
- });
- postMock = MockApiClient.addMockResponse({
- url: configUrl,
- method: 'POST',
- body: {
- id: project.id,
- threshold: '400',
- metric: 'lcp',
- },
- statusCode: 200,
- });
- deleteMock = MockApiClient.addMockResponse({
- url: configUrl,
- method: 'DELETE',
- statusCode: 200,
- });
- MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/',
- method: 'GET',
- body: {},
- statusCode: 200,
- });
- performanceIssuesMock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/performance-issues/configure/',
- method: 'GET',
- body: {},
- statusCode: 200,
- });
- });
- it('renders the fields', function () {
- render(
- <ProjectPerformance
- params={{orgId: org.slug, projectId: project.slug}}
- organization={org}
- project={project}
- />
- );
- expect(
- screen.getByRole('textbox', {name: 'Response Time Threshold (ms)'})
- ).toHaveValue('300');
- expect(getMock).toHaveBeenCalledTimes(1);
- });
- it('updates the field', async function () {
- render(
- <ProjectPerformance
- params={{orgId: org.slug, projectId: project.slug}}
- organization={org}
- project={project}
- />
- );
- const input = screen.getByRole('textbox', {name: 'Response Time Threshold (ms)'});
- await userEvent.clear(input);
- await userEvent.type(input, '400');
- await userEvent.tab();
- expect(postMock).toHaveBeenCalledWith(
- configUrl,
- expect.objectContaining({
- data: {threshold: '400'},
- })
- );
- expect(input).toHaveValue('400');
- });
- it('clears the data', async function () {
- render(
- <ProjectPerformance
- params={{orgId: org.slug, projectId: project.slug}}
- organization={org}
- project={project}
- />
- );
- await userEvent.click(screen.getByRole('button', {name: 'Reset All'}));
- expect(deleteMock).toHaveBeenCalled();
- });
- it('does not get performance issues settings without the feature flag', function () {
- const orgWithoutPerfIssues = TestStubs.Organization({
- features: ['performance-view', 'performance-issues-dev'],
- });
- render(
- <ProjectPerformance
- params={{orgId: org.slug, projectId: project.slug}}
- organization={orgWithoutPerfIssues}
- project={project}
- />
- );
- expect(performanceIssuesMock).not.toHaveBeenCalled();
- });
- it('renders detector threshold configuration - admin ui', async function () {
- jest.spyOn(utils, 'isActiveSuperuser').mockReturnValue(true);
- MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/performance-issues/configure/',
- method: 'GET',
- body: {n_plus_one_db_queries_detection_enabled: false},
- statusCode: 200,
- });
- const performanceIssuesPutMock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/performance-issues/configure/',
- method: 'PUT',
- });
- render(
- <ProjectPerformance
- params={{orgId: org.slug, projectId: project.slug}}
- organization={org}
- project={project}
- />,
- {organization: org}
- );
- expect(
- await screen.findByText('N+1 DB Queries Detection Enabled')
- ).toBeInTheDocument();
- expect(screen.getByText('Slow DB Queries Detection Enabled')).toBeInTheDocument();
- const toggle = screen.getByRole('checkbox', {
- name: 'N+1 DB Queries Detection Enabled',
- });
- await userEvent.click(toggle);
- expect(performanceIssuesPutMock).toHaveBeenCalledWith(
- '/projects/org-slug/project-slug/performance-issues/configure/',
- expect.objectContaining({
- data: {n_plus_one_db_queries_detection_enabled: true},
- })
- );
- });
- it.each([
- {
- title: 'N+1 DB Queries',
- threshold: 'n_plus_one_db_duration_threshold',
- allowedValues: allowedDurationValues,
- defaultValue: 100,
- newValue: 500,
- newValueIndex: 5,
- sliderIndex: 1,
- },
- {
- title: 'Slow DB Queries',
- threshold: 'slow_db_query_duration_threshold',
- allowedValues: allowedDurationValues.slice(1),
- defaultValue: 1000,
- newValue: 3000,
- newValueIndex: 7,
- sliderIndex: 2,
- },
- ])(
- 'renders detector thresholds settings for $title issue',
- async ({
- title,
- threshold,
- allowedValues,
- defaultValue,
- newValue,
- newValueIndex,
- sliderIndex,
- }) => {
- // Mock endpoints
- const mockGETBody = {};
- mockGETBody[threshold] = defaultValue;
- const performanceIssuesGetMock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/performance-issues/configure/',
- method: 'GET',
- body: mockGETBody,
- statusCode: 200,
- });
- const performanceIssuesPutMock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/performance-issues/configure/',
- method: 'PUT',
- });
- render(
- <ProjectPerformance
- params={{orgId: org.slug, projectId: project.slug}}
- organization={org}
- project={project}
- />,
- {organization: org}
- );
- expect(
- await screen.findByText('Performance Issues - Detector Threshold Settings')
- ).toBeInTheDocument();
- expect(screen.getByText(title)).toBeInTheDocument();
- const slider = screen.getAllByRole('slider')[sliderIndex];
- const indexOfValue = allowedValues.indexOf(defaultValue);
- // The value of the slider should be equal to the index
- // of the value returned from the GET method,
- // passed to it in the allowedValues array.
- expect(performanceIssuesGetMock).toHaveBeenCalled();
- expect(slider).toHaveValue(indexOfValue.toString());
- // Slide value on range slider.
- fireEvent.change(slider, {target: {value: newValueIndex}});
- expect(slider).toHaveValue(newValueIndex.toString());
- fireEvent.keyUp(slider);
- // Ensure that PUT request is fired to update
- // project settings
- const expectedPUTPayload = {};
- expectedPUTPayload[threshold] = newValue;
- expect(performanceIssuesPutMock).toHaveBeenCalledWith(
- '/projects/org-slug/project-slug/performance-issues/configure/',
- expect.objectContaining({
- data: expectedPUTPayload,
- })
- );
- }
- );
- it('test reset all detector thresholds', async function () {
- MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/performance-issues/configure/',
- method: 'GET',
- body: {
- n_plus_one_db_queries_detection_enabled: true,
- slow_db_queries_detection_enabled: false,
- },
- statusCode: 200,
- });
- const delete_request_mock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/performance-issues/configure/',
- method: 'DELETE',
- });
- render(
- <ProjectPerformance
- params={{orgId: org.slug, projectId: project.slug}}
- organization={org}
- project={project}
- />,
- {organization: org}
- );
- const button = await screen.findByText('Reset All Thresholds');
- expect(button).toBeInTheDocument();
- renderGlobalModal();
- await userEvent.click(button);
- // Ensure that confirm modal renders
- const confirmButton = screen.getByText('Confirm');
- expect(confirmButton).toBeInTheDocument();
- await userEvent.click(confirmButton);
- expect(delete_request_mock).toHaveBeenCalled();
- });
- });
|