From 19fb7261da7856acfda1a8c5441b609c7e502ad5 Mon Sep 17 00:00:00 2001 From: Benjamin Janssens Date: Mon, 3 Feb 2025 14:22:45 +0100 Subject: [PATCH] test(scaffolder): add tests for handleAutocompleteRequest Signed-off-by: Benjamin Janssens --- .../src/autocomplete/autocomplete.test.ts | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts diff --git a/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts b/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts new file mode 100644 index 0000000000..a107117129 --- /dev/null +++ b/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts @@ -0,0 +1,97 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { InputError } from '@backstage/errors'; +import { createHandleAutocompleteRequest } from './autocomplete'; +import { ScmIntegrationRegistry } from '@backstage/integration'; + +jest.mock('../util', () => { + return { + getOctokitOptions: jest.fn(), + }; +}); + +const mockOctokit = { + paginate: async (fn: any) => (await fn()).data, + rest: { + repos: { + listForAuthenticatedUser: jest.fn(), + }, + }, +}; +jest.mock('octokit', () => ({ + Octokit: class { + constructor() { + return mockOctokit; + } + }, +})); + +describe('handleAutocompleteRequest', () => { + const mockIntegrations = {} as ScmIntegrationRegistry; + + it('should return repositories with owner', async () => { + const handleAutocompleteRequest = createHandleAutocompleteRequest({ + integrations: mockIntegrations, + }); + + mockOctokit.rest.repos.listForAuthenticatedUser.mockResolvedValue({ + data: [ + { + full_name: 'backstage/backstage', + }, + ], + }); + + const result = await handleAutocompleteRequest({ + resource: 'repositoriesWithOwner', + token: 'token', + context: {}, + }); + + expect(result).toEqual({ + results: [{ id: 'backstage/backstage' }], + }); + }); + + it('should throw an error for invalid resource', async () => { + const handleAutocompleteRequest = createHandleAutocompleteRequest({ + integrations: mockIntegrations, + }); + + await expect( + handleAutocompleteRequest({ + resource: 'invalid', + token: 'token', + context: {}, + }), + ).rejects.toThrow(InputError); + }); + + it('should throw an error if context id is missing for repositories', async () => { + const handleAutocompleteRequest = createHandleAutocompleteRequest({ + integrations: mockIntegrations, + }); + + await expect( + handleAutocompleteRequest({ + resource: 'repositories', + token: 'token', + context: {}, + }), + ).rejects.toThrow(InputError); + }); +});