From 1bea77386fc8928998dbab85652f42909c1093aa Mon Sep 17 00:00:00 2001 From: Benjamin Janssens Date: Mon, 3 Feb 2025 15:18:47 +0100 Subject: [PATCH] test(scaffolder): add autocompletion tests for GithubRepoPicker Signed-off-by: Benjamin Janssens --- .../RepoUrlPicker/GithubRepoPicker.test.tsx | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/GithubRepoPicker.test.tsx b/plugins/scaffolder/src/components/fields/RepoUrlPicker/GithubRepoPicker.test.tsx index a3e233c908..e9a7041fc5 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/GithubRepoPicker.test.tsx +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/GithubRepoPicker.test.tsx @@ -16,18 +16,26 @@ import React, { act } from 'react'; import { GithubRepoPicker } from './GithubRepoPicker'; -import { fireEvent } from '@testing-library/react'; +import { fireEvent, waitFor } from '@testing-library/react'; import { renderInTestApp, TestApiProvider } from '@backstage/test-utils'; import { ScaffolderApi, scaffolderApiRef, } from '@backstage/plugin-scaffolder-react'; +import userEvent from '@testing-library/user-event'; describe('GithubRepoPicker', () => { const scaffolderApiMock: Partial = { autocomplete: jest.fn().mockImplementation(opts => Promise.resolve({ - results: [{ title: `${opts.resource}_example` }], + results: [ + { + id: + opts.resource === 'repositoriesWithOwner' + ? 'spotify/backstage' + : `${opts.resource}_example`, + }, + ], }), ), }; @@ -108,4 +116,58 @@ describe('GithubRepoPicker', () => { expect(onChange).toHaveBeenCalledWith({ owner: 'my-mock-owner' }); }); }); + + describe('autocompletion', () => { + it('should populate owners if accessToken is provided', async () => { + const onChange = jest.fn(); + + const { getAllByRole, getByText } = await renderInTestApp( + + + , + ); + + // Open the Autcomplete dropdown + const ownerInput = getAllByRole('textbox')[0]; + await userEvent.click(ownerInput); + + // Verify that the available owners are shown + await waitFor(() => expect(getByText('spotify')).toBeInTheDocument()); + + // Verify that selecting an option calls onChange + await userEvent.click(getByText('spotify')); + expect(onChange).toHaveBeenCalledWith({ + owner: 'spotify', + }); + }); + + it('should populate owners if owner and accessToken are provided', async () => { + const onChange = jest.fn(); + + await renderInTestApp( + + + , + ); + + // Verify that the available repos are updated + await waitFor( + () => + expect(onChange).toHaveBeenCalledWith({ + availableRepos: [{ name: 'backstage' }], + }), + { timeout: 1500 }, + ); + }); + }); });