From b3e056fe55183db8005d6b50bb3ede684479d871 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Thu, 31 Oct 2019 16:01:33 -0700 Subject: [PATCH] make ProjectLookup auto-select project if only one found --- .../src/components/Lookup/ProjectLookup.jsx | 11 ++++-- .../components/Lookup/ProjectLookup.test.jsx | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 awx/ui_next/src/components/Lookup/ProjectLookup.test.jsx 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(); + }); +});