From 7eeadfa8e311058a09a67a80b034be1c7449fd9b Mon Sep 17 00:00:00 2001 From: asheen1234 Date: Mon, 30 Mar 2026 21:12:40 -0400 Subject: [PATCH] feat: add RepoOwnerPicker tests and prettier linting Signed-off-by: asheen1234 --- .../GitLabRepoOwnerPicker.test.tsx | 8 +- .../RepoOwnerPicker/RepoOwnerPicker.test.tsx | 158 ++++++++++++++++++ 2 files changed, 161 insertions(+), 5 deletions(-) diff --git a/plugins/scaffolder/src/components/fields/RepoOwnerPicker/GitLabRepoOwnerPicker.test.tsx b/plugins/scaffolder/src/components/fields/RepoOwnerPicker/GitLabRepoOwnerPicker.test.tsx index 863e9e5a96..7fb9ea21d6 100644 --- a/plugins/scaffolder/src/components/fields/RepoOwnerPicker/GitLabRepoOwnerPicker.test.tsx +++ b/plugins/scaffolder/src/components/fields/RepoOwnerPicker/GitLabRepoOwnerPicker.test.tsx @@ -25,11 +25,9 @@ import userEvent from '@testing-library/user-event'; describe('GitLabRepoOwnerPicker', () => { const scaffolderApiMock: Partial = { - autocomplete: jest - .fn() - .mockResolvedValue({ - results: [{ title: 'owner1' }, { title: 'owner2' }], - }), + autocomplete: jest.fn().mockResolvedValue({ + results: [{ title: 'owner1' }, { title: 'owner2' }], + }), }; it('renders an input field', async () => { diff --git a/plugins/scaffolder/src/components/fields/RepoOwnerPicker/RepoOwnerPicker.test.tsx b/plugins/scaffolder/src/components/fields/RepoOwnerPicker/RepoOwnerPicker.test.tsx index c51d74416b..12cdd34359 100644 --- a/plugins/scaffolder/src/components/fields/RepoOwnerPicker/RepoOwnerPicker.test.tsx +++ b/plugins/scaffolder/src/components/fields/RepoOwnerPicker/RepoOwnerPicker.test.tsx @@ -38,6 +38,10 @@ describe('RepoOwnerPicker', () => { byHost: () => ({ type: 'github' }), }; + const mockIntegrationsApiGitLab: Partial = { + byHost: () => ({ type: 'gitlab' }), + }; + let mockScmAuthApi: Partial; beforeEach(() => { @@ -305,4 +309,158 @@ describe('RepoOwnerPicker', () => { expect(getByText('abc123')).toBeInTheDocument(); }); }); + + describe('requestUserCredentialsGitLab', () => { + it('should call the scmAuthApi with the correct params', async () => { + const secretsKey = 'testKey'; + + const SecretsComponent = () => { + const { secrets } = useTemplateSecrets(); + const secret = secrets[secretsKey]; + return secret ?
{secret}
: null; + }; + + const { getByText } = await renderInTestApp( + + +
, + }} + /> + + + , + ); + + await act(async () => { + // need to wait for the debounce to finish + await new Promise(resolve => setTimeout(resolve, 600)); + }); + + expect(mockScmAuthApi.getCredentials).toHaveBeenCalledWith({ + url: 'https://gitlab.com', + additionalScope: { + repoWrite: true, + customScopes: { + gitlab: ['workflow'], + }, + }, + }); + + expect(getByText('abc123')).toBeInTheDocument(); + }); + + it('should call the scmAuthApi with the correct params if workspace is nested', async () => { + await renderInTestApp( + + + , + }} + /> + + , + ); + + await act(async () => { + // need to wait for the debounce to finish + await new Promise(resolve => setTimeout(resolve, 600)); + }); + + expect(mockScmAuthApi.getCredentials).toHaveBeenCalledWith({ + url: 'https://gitlab.com', + additionalScope: { + repoWrite: true, + }, + }); + }); + + it('should not call the scmAuthApi if secret is available in the state', async () => { + const secretsKey = 'testKey'; + + const SecretsComponent = () => { + const { secrets } = useTemplateSecrets(); + const secret = secrets[secretsKey]; + return secret ?
{secret}
: null; + }; + + const { getByText } = await renderInTestApp( + + + , + }} + /> + + + , + ); + + await act(async () => { + // need to wait for the debounce to finish + await new Promise(resolve => setTimeout(resolve, 600)); + }); + + // as we already have a secret in the state, getCredentials should not be called again. + expect(mockScmAuthApi.getCredentials).toHaveBeenCalledTimes(0); + + expect(getByText('abc123')).toBeInTheDocument(); + }); + }); });