|
| 1 | +import {OrganizationFixture} from 'sentry-fixture/organization'; |
| 2 | +import {RepositoryFixture} from 'sentry-fixture/repository'; |
| 3 | + |
| 4 | +import {act, renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary'; |
| 5 | + |
| 6 | +import {useScmRepoSearch} from './useScmRepoSearch'; |
| 7 | + |
| 8 | +describe('useScmRepoSearch', () => { |
| 9 | + const organization = OrganizationFixture(); |
| 10 | + const integrationId = '1'; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + jest.useFakeTimers(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + MockApiClient.clearMockResponses(); |
| 18 | + jest.useRealTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + function renderHook(selectedRepo?: Parameters<typeof useScmRepoSearch>[1]) { |
| 22 | + return renderHookWithProviders(() => useScmRepoSearch(integrationId, selectedRepo), { |
| 23 | + organization, |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + it('does not fetch when search is empty', () => { |
| 28 | + const request = MockApiClient.addMockResponse({ |
| 29 | + url: `/organizations/${organization.slug}/integrations/${integrationId}/repos/`, |
| 30 | + body: {repos: []}, |
| 31 | + }); |
| 32 | + |
| 33 | + renderHook(); |
| 34 | + |
| 35 | + expect(request).not.toHaveBeenCalled(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('fetches repos after search is set', async () => { |
| 39 | + const request = MockApiClient.addMockResponse({ |
| 40 | + url: `/organizations/${organization.slug}/integrations/${integrationId}/repos/`, |
| 41 | + body: {repos: []}, |
| 42 | + }); |
| 43 | + |
| 44 | + const {result} = renderHook(); |
| 45 | + |
| 46 | + act(() => { |
| 47 | + result.current.setSearch('sentry'); |
| 48 | + jest.advanceTimersByTime(200); |
| 49 | + }); |
| 50 | + |
| 51 | + await waitFor(() => expect(request).toHaveBeenCalled()); |
| 52 | + }); |
| 53 | + |
| 54 | + it('passes search query param to the API', async () => { |
| 55 | + const request = MockApiClient.addMockResponse({ |
| 56 | + url: `/organizations/${organization.slug}/integrations/${integrationId}/repos/`, |
| 57 | + body: {repos: []}, |
| 58 | + }); |
| 59 | + |
| 60 | + const {result} = renderHook(); |
| 61 | + |
| 62 | + act(() => { |
| 63 | + result.current.setSearch('sentry'); |
| 64 | + jest.advanceTimersByTime(200); |
| 65 | + }); |
| 66 | + |
| 67 | + await waitFor(() => expect(request).toHaveBeenCalled()); |
| 68 | + |
| 69 | + expect(request).toHaveBeenCalledWith( |
| 70 | + expect.anything(), |
| 71 | + expect.objectContaining({ |
| 72 | + query: expect.objectContaining({search: 'sentry'}), |
| 73 | + }) |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('transforms API response into reposByIdentifier and dropdownItems', async () => { |
| 78 | + MockApiClient.addMockResponse({ |
| 79 | + url: `/organizations/${organization.slug}/integrations/${integrationId}/repos/`, |
| 80 | + body: { |
| 81 | + repos: [ |
| 82 | + {identifier: 'getsentry/sentry', name: 'sentry', isInstalled: false}, |
| 83 | + {identifier: 'getsentry/relay', name: 'relay', isInstalled: true}, |
| 84 | + ], |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + const {result} = renderHook(); |
| 89 | + |
| 90 | + act(() => { |
| 91 | + result.current.setSearch('get'); |
| 92 | + jest.advanceTimersByTime(200); |
| 93 | + }); |
| 94 | + |
| 95 | + await waitFor(() => expect(result.current.reposByIdentifier.size).toBe(2)); |
| 96 | + |
| 97 | + expect(result.current.reposByIdentifier.get('getsentry/sentry')).toEqual({ |
| 98 | + identifier: 'getsentry/sentry', |
| 99 | + name: 'sentry', |
| 100 | + isInstalled: false, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(result.current.dropdownItems).toEqual([ |
| 104 | + {value: 'getsentry/sentry', label: 'sentry', disabled: false}, |
| 105 | + {value: 'getsentry/relay', label: 'relay', disabled: false}, |
| 106 | + ]); |
| 107 | + }); |
| 108 | + |
| 109 | + it('marks selected repo as disabled in dropdownItems', async () => { |
| 110 | + MockApiClient.addMockResponse({ |
| 111 | + url: `/organizations/${organization.slug}/integrations/${integrationId}/repos/`, |
| 112 | + body: { |
| 113 | + repos: [ |
| 114 | + {identifier: 'getsentry/sentry', name: 'sentry', isInstalled: false}, |
| 115 | + {identifier: 'getsentry/relay', name: 'relay', isInstalled: false}, |
| 116 | + ], |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + const selectedRepo = RepositoryFixture({externalSlug: 'getsentry/sentry'}); |
| 121 | + const {result} = renderHook(selectedRepo); |
| 122 | + |
| 123 | + act(() => { |
| 124 | + result.current.setSearch('get'); |
| 125 | + jest.advanceTimersByTime(200); |
| 126 | + }); |
| 127 | + |
| 128 | + await waitFor(() => expect(result.current.dropdownItems).toHaveLength(2)); |
| 129 | + |
| 130 | + expect(result.current.dropdownItems[0]).toEqual( |
| 131 | + expect.objectContaining({value: 'getsentry/sentry', disabled: true}) |
| 132 | + ); |
| 133 | + expect(result.current.dropdownItems[1]).toEqual( |
| 134 | + expect.objectContaining({value: 'getsentry/relay', disabled: false}) |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('returns isError true on API failure', async () => { |
| 139 | + MockApiClient.addMockResponse({ |
| 140 | + url: `/organizations/${organization.slug}/integrations/${integrationId}/repos/`, |
| 141 | + statusCode: 500, |
| 142 | + body: {detail: 'Internal Error'}, |
| 143 | + }); |
| 144 | + |
| 145 | + const {result} = renderHook(); |
| 146 | + |
| 147 | + act(() => { |
| 148 | + result.current.setSearch('sentry'); |
| 149 | + jest.advanceTimersByTime(200); |
| 150 | + }); |
| 151 | + |
| 152 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 153 | + }); |
| 154 | + |
| 155 | + it('returns empty results when search is cleared after searching', async () => { |
| 156 | + MockApiClient.addMockResponse({ |
| 157 | + url: `/organizations/${organization.slug}/integrations/${integrationId}/repos/`, |
| 158 | + body: { |
| 159 | + repos: [{identifier: 'getsentry/sentry', name: 'sentry', isInstalled: false}], |
| 160 | + }, |
| 161 | + }); |
| 162 | + |
| 163 | + const {result} = renderHook(); |
| 164 | + |
| 165 | + // Search and get results |
| 166 | + act(() => { |
| 167 | + result.current.setSearch('sentry'); |
| 168 | + jest.advanceTimersByTime(200); |
| 169 | + }); |
| 170 | + |
| 171 | + await waitFor(() => expect(result.current.reposByIdentifier.size).toBe(1)); |
| 172 | + |
| 173 | + // Clear search |
| 174 | + act(() => { |
| 175 | + result.current.setSearch(''); |
| 176 | + jest.advanceTimersByTime(200); |
| 177 | + }); |
| 178 | + |
| 179 | + // placeholderData returns undefined when debouncedSearch is empty |
| 180 | + await waitFor(() => expect(result.current.reposByIdentifier.size).toBe(0)); |
| 181 | + expect(result.current.dropdownItems).toEqual([]); |
| 182 | + }); |
| 183 | +}); |
0 commit comments