1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

make ProjectLookup auto-select project if only one found

This commit is contained in:
Keith Grant 2019-10-31 16:01:33 -07:00
parent 9b09344bae
commit b3e056fe55
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();
});
});