mirror of
https://github.com/ansible/awx.git
synced 2026-02-24 14:36:00 -03:30
Merge pull request #9600 from keithjgrant/3167-variables-formatting
Initialize variables field/detail with formatted JSON strings SUMMARY When a variables detail or variables field are mounted with JSON code, this ensures the JSON is formatted with friendly whitespace, regardless how it was originally formatted when saved. addresses #3167 ISSUE TYPE Bugfix Pull Request COMPONENT NAME UI Reviewed-by: Kersom <None> Reviewed-by: Jake McDermott <yo@jakemcdermott.me>
This commit is contained in:
@@ -49,6 +49,9 @@ function VariablesDetail({
|
|||||||
}
|
}
|
||||||
const modeMatches = isJsonString(value) === (mode === JSON_MODE);
|
const modeMatches = isJsonString(value) === (mode === JSON_MODE);
|
||||||
if (modeMatches) {
|
if (modeMatches) {
|
||||||
|
if (mode === JSON_MODE) {
|
||||||
|
return JSON.stringify(JSON.parse(value), null, 2);
|
||||||
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return mode === YAML_MODE ? jsonToYaml(value) : yamlToJson(value);
|
return mode === YAML_MODE ? jsonToYaml(value) : yamlToJson(value);
|
||||||
|
|||||||
@@ -24,7 +24,15 @@ describe('<VariablesDetail>', () => {
|
|||||||
const input = wrapper.find('VariablesDetail___StyledCodeEditor');
|
const input = wrapper.find('VariablesDetail___StyledCodeEditor');
|
||||||
expect(input).toHaveLength(1);
|
expect(input).toHaveLength(1);
|
||||||
expect(input.prop('mode')).toEqual('javascript');
|
expect(input.prop('mode')).toEqual('javascript');
|
||||||
expect(input.prop('value')).toEqual('{"foo": "bar"}');
|
});
|
||||||
|
|
||||||
|
test('should format JSON', () => {
|
||||||
|
const wrapper = mountWithContexts(
|
||||||
|
<VariablesDetail value='{"foo": "bar"}' label="Variables" />
|
||||||
|
);
|
||||||
|
const input = wrapper.find('VariablesDetail___StyledCodeEditor');
|
||||||
|
expect(input).toHaveLength(1);
|
||||||
|
expect(input.prop('value')).toEqual('{\n "foo": "bar"\n}');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should convert between modes', () => {
|
test('should convert between modes', () => {
|
||||||
|
|||||||
@@ -57,11 +57,12 @@ function VariablesField({
|
|||||||
);
|
);
|
||||||
const [field, meta, helpers] = useField({ name, validate });
|
const [field, meta, helpers] = useField({ name, validate });
|
||||||
|
|
||||||
// mode's useState above couldn't be initialized to JSON_MODE because
|
useEffect(function initializeJSON() {
|
||||||
// the field value had to be defined below it
|
|
||||||
useEffect(function initializeMode() {
|
|
||||||
if (isJsonString(field.value)) {
|
if (isJsonString(field.value)) {
|
||||||
|
// mode's useState above couldn't be initialized to JSON_MODE because
|
||||||
|
// the field value had to be defined below it
|
||||||
setMode(JSON_MODE);
|
setMode(JSON_MODE);
|
||||||
|
helpers.setValue(JSON.stringify(JSON.parse(field.value), null, 2));
|
||||||
}
|
}
|
||||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
@@ -193,7 +194,14 @@ function VariablesFieldInternals({
|
|||||||
setShouldValidate,
|
setShouldValidate,
|
||||||
handleChange,
|
handleChange,
|
||||||
}) {
|
}) {
|
||||||
const [field, meta] = useField(name);
|
const [field, meta, helpers] = useField(name);
|
||||||
|
|
||||||
|
useEffect(function formatJsonString() {
|
||||||
|
if (mode === YAML_MODE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
helpers.setValue(JSON.stringify(JSON.parse(field.value), null, 2));
|
||||||
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="pf-c-form__group">
|
<div className="pf-c-form__group">
|
||||||
|
|||||||
@@ -5,10 +5,6 @@ import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
|
|||||||
import VariablesField from './VariablesField';
|
import VariablesField from './VariablesField';
|
||||||
|
|
||||||
describe('VariablesField', () => {
|
describe('VariablesField', () => {
|
||||||
beforeEach(() => {
|
|
||||||
document.body.createTextRange = jest.fn();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should render code editor', () => {
|
it('should render code editor', () => {
|
||||||
const value = '---\n';
|
const value = '---\n';
|
||||||
const wrapper = mountWithContexts(
|
const wrapper = mountWithContexts(
|
||||||
@@ -200,18 +196,26 @@ describe('VariablesField', () => {
|
|||||||
|
|
||||||
it('should initialize to JSON if value is JSON', async () => {
|
it('should initialize to JSON if value is JSON', async () => {
|
||||||
const value = '{"foo": "bar"}';
|
const value = '{"foo": "bar"}';
|
||||||
const wrapper = mountWithContexts(
|
let wrapper;
|
||||||
<Formik initialValues={{ variables: value }} onSubmit={jest.fn()}>
|
await act(async () => {
|
||||||
{formik => (
|
wrapper = mountWithContexts(
|
||||||
<form onSubmit={formik.handleSubmit}>
|
<Formik initialValues={{ variables: value }} onSubmit={jest.fn()}>
|
||||||
<VariablesField id="the-field" name="variables" label="Variables" />
|
{formik => (
|
||||||
<button type="submit" id="submit">
|
<form onSubmit={formik.handleSubmit}>
|
||||||
Submit
|
<VariablesField
|
||||||
</button>
|
id="the-field"
|
||||||
</form>
|
name="variables"
|
||||||
)}
|
label="Variables"
|
||||||
</Formik>
|
/>
|
||||||
);
|
<button type="submit" id="submit">
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
</Formik>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
expect(wrapper.find('CodeEditor').prop('mode')).toEqual('javascript');
|
expect(wrapper.find('CodeEditor').prop('mode')).toEqual('javascript');
|
||||||
});
|
});
|
||||||
@@ -238,4 +242,32 @@ describe('VariablesField', () => {
|
|||||||
expect(wrapper.find('Modal').prop('isOpen')).toEqual(true);
|
expect(wrapper.find('Modal').prop('isOpen')).toEqual(true);
|
||||||
expect(wrapper.find('Modal CodeEditor')).toHaveLength(1);
|
expect(wrapper.find('Modal CodeEditor')).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should format JSON for code editor', async () => {
|
||||||
|
const value = '{"foo": "bar"}';
|
||||||
|
let wrapper;
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(
|
||||||
|
<Formik initialValues={{ variables: value }} onSubmit={jest.fn()}>
|
||||||
|
{formik => (
|
||||||
|
<form onSubmit={formik.handleSubmit}>
|
||||||
|
<VariablesField
|
||||||
|
id="the-field"
|
||||||
|
name="variables"
|
||||||
|
label="Variables"
|
||||||
|
/>
|
||||||
|
<button type="submit" id="submit">
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
</Formik>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
expect(wrapper.find('CodeEditor').prop('value')).toEqual(
|
||||||
|
'{\n "foo": "bar"\n}'
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user