|
| 1 | +import {expectTypeOf} from 'expect-type'; |
| 2 | +import {OrganizationFixture} from 'sentry-fixture/organization'; |
| 3 | + |
| 4 | +import type {Detector, UptimeDetector} from 'sentry/types/workflowEngine/detectors'; |
| 5 | +import type {ApiResponse} from 'sentry/utils/api/apiFetch'; |
| 6 | +import {parseQueryKey} from 'sentry/utils/api/apiQueryKey'; |
| 7 | +import {detectorListApiOptions} from 'sentry/views/detectors/hooks'; |
| 8 | + |
| 9 | +const organization = OrganizationFixture(); |
| 10 | + |
| 11 | +describe('detectorListApiOptions', () => { |
| 12 | + describe('query construction', () => { |
| 13 | + it('excludes issue_stream detectors by default', () => { |
| 14 | + const {options} = parseQueryKey(detectorListApiOptions(organization).queryKey); |
| 15 | + expect(options?.query?.query).toBe('!type:issue_stream'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('does not exclude issue_stream when includeIssueStreamDetectors is true', () => { |
| 19 | + const {options} = parseQueryKey( |
| 20 | + detectorListApiOptions(organization, { |
| 21 | + includeIssueStreamDetectors: true, |
| 22 | + }).queryKey |
| 23 | + ); |
| 24 | + expect(options?.query?.query).toBeUndefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('adds type filter when type is provided', () => { |
| 28 | + const {options} = parseQueryKey( |
| 29 | + detectorListApiOptions(organization, {type: 'uptime'}).queryKey |
| 30 | + ); |
| 31 | + expect(options?.query?.query).toBe('!type:issue_stream type:uptime'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('combines type filter with custom query', () => { |
| 35 | + const {options} = parseQueryKey( |
| 36 | + detectorListApiOptions(organization, { |
| 37 | + type: 'uptime', |
| 38 | + query: 'my-search', |
| 39 | + }).queryKey |
| 40 | + ); |
| 41 | + expect(options?.query?.query).toBe('!type:issue_stream type:uptime my-search'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('combines includeIssueStreamDetectors with type', () => { |
| 45 | + const {options} = parseQueryKey( |
| 46 | + detectorListApiOptions(organization, { |
| 47 | + type: 'uptime', |
| 48 | + includeIssueStreamDetectors: true, |
| 49 | + }).queryKey |
| 50 | + ); |
| 51 | + expect(options?.query?.query).toBe('type:uptime'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('passes through pagination and filter params', () => { |
| 55 | + const {options} = parseQueryKey( |
| 56 | + detectorListApiOptions(organization, { |
| 57 | + cursor: 'abc123', |
| 58 | + limit: 25, |
| 59 | + sortBy: 'name', |
| 60 | + projects: [1, 2], |
| 61 | + ids: ['10', '20'], |
| 62 | + }).queryKey |
| 63 | + ); |
| 64 | + expect(options?.query).toEqual( |
| 65 | + expect.objectContaining({ |
| 66 | + cursor: 'abc123', |
| 67 | + per_page: 25, |
| 68 | + sortBy: 'name', |
| 69 | + project: [1, 2], |
| 70 | + id: ['10', '20'], |
| 71 | + }) |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('builds the correct URL', () => { |
| 76 | + const {url} = parseQueryKey(detectorListApiOptions(organization).queryKey); |
| 77 | + expect(url).toBe(`/organizations/${organization.slug}/detectors/`); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('types', () => { |
| 82 | + it('returns Detector[] by default', () => { |
| 83 | + const options = detectorListApiOptions(organization); |
| 84 | + expectTypeOf(options.select).returns.toEqualTypeOf<Detector[]>(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('returns UptimeDetector[] when type is uptime', () => { |
| 88 | + const options = detectorListApiOptions(organization, {type: 'uptime'}); |
| 89 | + expectTypeOf(options.select).returns.toEqualTypeOf<UptimeDetector[]>(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns Detector[] when no type is provided with other params', () => { |
| 93 | + const options = detectorListApiOptions(organization, {cursor: 'abc'}); |
| 94 | + expectTypeOf(options.select).returns.toEqualTypeOf<Detector[]>(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns ApiResponse<UptimeDetector[]> when selectJsonWithHeaders is used', () => { |
| 98 | + const options = detectorListApiOptions(organization, {type: 'uptime'}); |
| 99 | + // The select override changes the type, but the queryFn data type is correct |
| 100 | + expectTypeOf(options.select) |
| 101 | + .parameter(0) |
| 102 | + .toEqualTypeOf<ApiResponse<UptimeDetector[]>>(); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments