mirror of
https://github.com/ansible/awx.git
synced 2026-02-23 14:05:59 -03:30
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:
@@ -8,8 +8,6 @@ import { Project } from '@types';
|
|||||||
import Lookup from '@components/Lookup';
|
import Lookup from '@components/Lookup';
|
||||||
import { FieldTooltip } from '@components/FormField';
|
import { FieldTooltip } from '@components/FormField';
|
||||||
|
|
||||||
const loadProjects = async params => ProjectsAPI.read(params);
|
|
||||||
|
|
||||||
class ProjectLookup extends React.Component {
|
class ProjectLookup extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
@@ -23,6 +21,15 @@ class ProjectLookup extends React.Component {
|
|||||||
onBlur,
|
onBlur,
|
||||||
} = this.props;
|
} = 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 (
|
return (
|
||||||
<FormGroup
|
<FormGroup
|
||||||
fieldId="project"
|
fieldId="project"
|
||||||
|
|||||||
35
awx/ui_next/src/components/Lookup/ProjectLookup.test.jsx
Normal file
35
awx/ui_next/src/components/Lookup/ProjectLookup.test.jsx
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user