diff --git a/awx/ui_next/src/components/Lookup/ProjectLookup.jsx b/awx/ui_next/src/components/Lookup/ProjectLookup.jsx
index 3493c2d531..90d36a64a7 100644
--- a/awx/ui_next/src/components/Lookup/ProjectLookup.jsx
+++ b/awx/ui_next/src/components/Lookup/ProjectLookup.jsx
@@ -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 (
', () => {
+ test('should auto-select project when only one available', async () => {
+ ProjectsAPI.read.mockReturnValue({
+ data: {
+ results: [{ id: 1 }],
+ count: 1,
+ },
+ });
+ const onChange = jest.fn();
+ mountWithContexts();
+ 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();
+ await sleep(0);
+ expect(onChange).not.toHaveBeenCalled();
+ });
+});