chore: added some tests for the hosts dropdown
Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
@@ -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(
|
||||
<TestApiProvider apis={[[scaffolderApiRef, mockScaffolderApi]]}>
|
||||
<RepoUrlPickerHost
|
||||
hosts={['github.com']}
|
||||
onChange={mockOnChange}
|
||||
rawErrors={[]}
|
||||
/>
|
||||
</TestApiProvider>,
|
||||
);
|
||||
|
||||
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(
|
||||
<TestApiProvider apis={[[scaffolderApiRef, mockScaffolderApi]]}>
|
||||
<RepoUrlPickerHost
|
||||
hosts={['github.com', 'gitlab.com']}
|
||||
onChange={mockOnChange}
|
||||
rawErrors={[]}
|
||||
/>
|
||||
</TestApiProvider>,
|
||||
);
|
||||
|
||||
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(
|
||||
<TestApiProvider apis={[[scaffolderApiRef, mockScaffolderApi]]}>
|
||||
<RepoUrlPickerHost
|
||||
hosts={['github.com', 'gitlab.com', 'notfound.host']}
|
||||
onChange={mockOnChange}
|
||||
rawErrors={[]}
|
||||
/>
|
||||
</TestApiProvider>,
|
||||
);
|
||||
|
||||
fireEvent.mouseDown(getByTestId('select'));
|
||||
expect(getByText('gitlab.com')).toBeInTheDocument();
|
||||
|
||||
const listbox = within(getByRole('combobox'));
|
||||
|
||||
expect(listbox.getAllByRole('option')).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
@@ -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"
|
||||
/>
|
||||
|
||||
<FormHelperText>
|
||||
|
||||
Reference in New Issue
Block a user