Merge pull request #5206 from keithjgrant/4966-project-auto-select

make ProjectLookup auto-select project if only one found

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot] 2019-11-18 16:23:38 +00:00 committed by GitHub
commit c7cabfa785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 2 deletions

View File

@ -8,8 +8,6 @@ import { Project } from '@types';
import Lookup from '@components/Lookup';
import { FieldTooltip } from '@components/FormField';
const loadProjects = async params => ProjectsAPI.read(params);
class ProjectLookup extends React.Component {
render() {
const {
@ -23,6 +21,15 @@ class ProjectLookup extends React.Component {
onBlur,
} = this.props;
const loadProjects = async params => {
const response = await ProjectsAPI.read(params);
const { results, count } = response.data;
if (count === 1) {
onChange(results[0], 'project');
}
return response;
};
return (
<FormGroup
fieldId="project"

View File

@ -0,0 +1,35 @@
import React from 'react';
import { mountWithContexts } from '@testUtils/enzymeHelpers';
import { sleep } from '@testUtils/testUtils';
import { ProjectsAPI } from '@api';
import ProjectLookup from './ProjectLookup';
jest.mock('@api');
describe('<ProjectLookup />', () => {
test('should auto-select project when only one available', async () => {
ProjectsAPI.read.mockReturnValue({
data: {
results: [{ id: 1 }],
count: 1,
},
});
const onChange = jest.fn();
mountWithContexts(<ProjectLookup onChange={onChange} />);
await sleep(0);
expect(onChange).toHaveBeenCalledWith({ id: 1 }, 'project');
});
test('should not auto-select project when multiple available', async () => {
ProjectsAPI.read.mockReturnValue({
data: {
results: [{ id: 1 }, { id: 2 }],
count: 2,
},
});
const onChange = jest.fn();
mountWithContexts(<ProjectLookup onChange={onChange} />);
await sleep(0);
expect(onChange).not.toHaveBeenCalled();
});
});