diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPickerHost.test.tsx b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPickerHost.test.tsx new file mode 100644 index 0000000000..e151ddbbc7 --- /dev/null +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPickerHost.test.tsx @@ -0,0 +1,99 @@ +/* + * Copyright 2021 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 React from 'react'; +import { RepoUrlPickerHost } from './RepoUrlPickerHost'; +import { renderInTestApp, TestApiProvider } from '@backstage/test-utils'; +import { scaffolderApiRef } from '../../../api'; +import { fireEvent, within } from '@testing-library/react'; + +describe('RepoUrlPickerHostField', () => { + it('renders the default host properly', async () => { + const mockOnChange = jest.fn(); + const mockScaffolderApi = { + getIntegrationsList: jest + .fn() + .mockResolvedValue([ + { host: 'github.com', title: 'github.com', type: 'github' }, + ]), + }; + const { getByText } = await renderInTestApp( + + + , + ); + + expect(getByText('github.com')).toBeInTheDocument(); + expect(mockOnChange).toHaveBeenCalledWith('github.com'); + }); + + it('should provide a dropdown when multiple hosts are returned that can be selected', async () => { + const mockOnChange = jest.fn(); + const mockScaffolderApi = { + getIntegrationsList: jest.fn().mockResolvedValue([ + { host: 'github.com', title: 'github.com', type: 'github' }, + { host: 'gitlab.com', title: 'gitlab.com', type: 'gitlab' }, + ]), + }; + + const { getByRole, getByText, getByTestId } = await renderInTestApp( + + + , + ); + + fireEvent.mouseDown(getByTestId('select')); + expect(getByText('gitlab.com')).toBeInTheDocument(); + + const listbox = within(getByRole('combobox')); + + expect(listbox.getAllByRole('option')).toHaveLength(2); + }); + + it('should not display hosts that dont have integration config set correctly', async () => { + const mockOnChange = jest.fn(); + const mockScaffolderApi = { + getIntegrationsList: jest.fn().mockResolvedValue([ + { host: 'github.com', title: 'github.com', type: 'github' }, + { host: 'gitlab.com', title: 'gitlab.com', type: 'gitlab' }, + ]), + }; + + const { getByRole, getByText, getByTestId } = await renderInTestApp( + + + , + ); + + fireEvent.mouseDown(getByTestId('select')); + expect(getByText('gitlab.com')).toBeInTheDocument(); + + const listbox = within(getByRole('combobox')); + + expect(listbox.getAllByRole('option')).toHaveLength(2); + }); +}); diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPickerHost.tsx b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPickerHost.tsx index 8e270610bc..979e33266c 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPickerHost.tsx +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPickerHost.tsx @@ -14,12 +14,7 @@ * limitations under the License. */ import React, { useEffect } from 'react'; -import { - Progress, - Select, - SelectedItems, - SelectItem, -} from '@backstage/core-components'; +import { Progress, Select, SelectItem } from '@backstage/core-components'; import FormControl from '@material-ui/core/FormControl'; import FormHelperText from '@material-ui/core/FormHelperText'; import { useApi } from '@backstage/core-plugin-api'; @@ -75,6 +70,7 @@ export const RepoUrlPickerHost = ({ onChange={s => onChange(String(Array.isArray(s) ? s[0] : s))} selected={host} items={hostsOptions} + data-testid="host-select" />