remove output/stderr tabs from host detail modals when not present (#12064)

This commit is contained in:
Keith Grant
2022-04-19 14:17:37 -07:00
committed by GitHub
parent c93155132a
commit 526b1e692a
2 changed files with 38 additions and 43 deletions

View File

@@ -1,7 +1,6 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Modal, Tab, Tabs, TabTitleText } from '@patternfly/react-core'; import { Modal, Tab, Tabs, TabTitleText } from '@patternfly/react-core';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { encode } from 'html-entities'; import { encode } from 'html-entities';
import StatusLabel from '../../../components/StatusLabel'; import StatusLabel from '../../../components/StatusLabel';
@@ -37,10 +36,8 @@ const processEventStatus = (event) => {
const processCodeEditorValue = (value) => { const processCodeEditorValue = (value) => {
let codeEditorValue; let codeEditorValue;
if (value === undefined) { if (!value) {
codeEditorValue = false; codeEditorValue = '';
} else if (value === '') {
codeEditorValue = ' ';
} else if (typeof value === 'string') { } else if (typeof value === 'string') {
codeEditorValue = encode(value); codeEditorValue = encode(value);
} else { } else {
@@ -49,8 +46,8 @@ const processCodeEditorValue = (value) => {
return codeEditorValue; return codeEditorValue;
}; };
const processStdOutValue = (hostEvent) => { const getStdOutValue = (hostEvent) => {
const taskAction = hostEvent?.event_data?.taskAction; const taskAction = hostEvent?.event_data?.task_action;
const res = hostEvent?.event_data?.res; const res = hostEvent?.event_data?.res;
let stdOut; let stdOut;
@@ -61,8 +58,8 @@ const processStdOutValue = (hostEvent) => {
res.results && res.results &&
Array.isArray(res.results) Array.isArray(res.results)
) { ) {
[stdOut] = res.results; stdOut = res.results.join('\n');
} else if (res) { } else if (res?.stdout) {
stdOut = res.stdout; stdOut = res.stdout;
} }
return stdOut; return stdOut;
@@ -81,8 +78,8 @@ function HostEventModal({ onClose, hostEvent = {}, isOpen = false }) {
}; };
const jsonObj = processCodeEditorValue(hostEvent?.event_data?.res); const jsonObj = processCodeEditorValue(hostEvent?.event_data?.res);
const stdErr = processCodeEditorValue(hostEvent?.event_data?.res?.stderr); const stdErr = hostEvent?.event_data?.res?.stderr;
const stdOut = processCodeEditorValue(processStdOutValue(hostEvent)); const stdOut = processCodeEditorValue(getStdOutValue(hostEvent));
return ( return (
<Modal <Modal
@@ -147,13 +144,13 @@ function HostEventModal({ onClose, hostEvent = {}, isOpen = false }) {
<ContentEmpty title={t`No JSON Available`} /> <ContentEmpty title={t`No JSON Available`} />
)} )}
</Tab> </Tab>
<Tab {stdOut?.length ? (
eventKey={2} <Tab
title={<TabTitleText>{t`Standard Out`}</TabTitleText>} eventKey={2}
aria-label={t`Standard out tab`} title={<TabTitleText>{t`Output`}</TabTitleText>}
ouiaId="standard-out-tab" aria-label={t`Output tab`}
> ouiaId="standard-out-tab"
{activeTabKey === 2 && stdOut ? ( >
<CodeEditor <CodeEditor
mode="javascript" mode="javascript"
readOnly readOnly
@@ -162,17 +159,15 @@ function HostEventModal({ onClose, hostEvent = {}, isOpen = false }) {
rows={20} rows={20}
hasErrors={false} hasErrors={false}
/> />
) : ( </Tab>
<ContentEmpty title={t`No Standard Out Available`} /> ) : null}
)} {stdErr?.length ? (
</Tab> <Tab
<Tab eventKey={3}
eventKey={3} title={<TabTitleText>{t`Standard Error`}</TabTitleText>}
title={<TabTitleText>{t`Standard Error`}</TabTitleText>} aria-label={t`Standard error tab`}
aria-label={t`Standard error tab`} ouiaId="standard-error-tab"
ouiaId="standard-error-tab" >
>
{activeTabKey === 3 && stdErr ? (
<CodeEditor <CodeEditor
mode="javascript" mode="javascript"
readOnly readOnly
@@ -181,10 +176,8 @@ function HostEventModal({ onClose, hostEvent = {}, isOpen = false }) {
hasErrors={false} hasErrors={false}
rows={20} rows={20}
/> />
) : ( </Tab>
<ContentEmpty title={t`No Standard Error Available`} /> ) : null}
)}
</Tab>
</Tabs> </Tabs>
</Modal> </Modal>
); );

View File

@@ -17,6 +17,7 @@ const hostEvent = {
msg: 'This is a debug message: 1', msg: 'This is a debug message: 1',
stdout: stdout:
' total used free shared buff/cache available\nMem: 7973 3005 960 30 4007 4582\nSwap: 1023 0 1023', ' total used free shared buff/cache available\nMem: 7973 3005 960 30 4007 4582\nSwap: 1023 0 1023',
stderr: 'problems',
cmd: ['free', '-m'], cmd: ['free', '-m'],
stderr_lines: [], stderr_lines: [],
stdout_lines: [ stdout_lines: [
@@ -51,6 +52,7 @@ const jsonValue = `{
\"item\": \"1\", \"item\": \"1\",
\"msg\": \"This is a debug message: 1\", \"msg\": \"This is a debug message: 1\",
\"stdout\": \" total used free shared buff/cache available\\nMem: 7973 3005 960 30 4007 4582\\nSwap: 1023 0 1023\", \"stdout\": \" total used free shared buff/cache available\\nMem: 7973 3005 960 30 4007 4582\\nSwap: 1023 0 1023\",
\"stderr\": \"problems\",
\"cmd\": [ \"cmd\": [
\"free\", \"free\",
\"-m\" \"-m\"
@@ -169,7 +171,7 @@ describe('HostEventModal', () => {
handleTabClick(null, 1); handleTabClick(null, 1);
wrapper.update(); wrapper.update();
const codeEditor = wrapper.find('CodeEditor'); const codeEditor = wrapper.find('Tab[eventKey=1] CodeEditor');
expect(codeEditor.prop('mode')).toBe('javascript'); expect(codeEditor.prop('mode')).toBe('javascript');
expect(codeEditor.prop('readOnly')).toBe(true); expect(codeEditor.prop('readOnly')).toBe(true);
expect(codeEditor.prop('value')).toEqual(jsonValue); expect(codeEditor.prop('value')).toEqual(jsonValue);
@@ -184,7 +186,7 @@ describe('HostEventModal', () => {
handleTabClick(null, 2); handleTabClick(null, 2);
wrapper.update(); wrapper.update();
const codeEditor = wrapper.find('CodeEditor'); const codeEditor = wrapper.find('Tab[eventKey=2] CodeEditor');
expect(codeEditor.prop('mode')).toBe('javascript'); expect(codeEditor.prop('mode')).toBe('javascript');
expect(codeEditor.prop('readOnly')).toBe(true); expect(codeEditor.prop('readOnly')).toBe(true);
expect(codeEditor.prop('value')).toEqual(hostEvent.event_data.res.stdout); expect(codeEditor.prop('value')).toEqual(hostEvent.event_data.res.stdout);
@@ -195,7 +197,7 @@ describe('HostEventModal', () => {
...hostEvent, ...hostEvent,
event_data: { event_data: {
res: { res: {
stderr: '', stderr: 'error content',
}, },
}, },
}; };
@@ -207,10 +209,10 @@ describe('HostEventModal', () => {
handleTabClick(null, 3); handleTabClick(null, 3);
wrapper.update(); wrapper.update();
const codeEditor = wrapper.find('CodeEditor'); const codeEditor = wrapper.find('Tab[eventKey=3] CodeEditor');
expect(codeEditor.prop('mode')).toBe('javascript'); expect(codeEditor.prop('mode')).toBe('javascript');
expect(codeEditor.prop('readOnly')).toBe(true); expect(codeEditor.prop('readOnly')).toBe(true);
expect(codeEditor.prop('value')).toEqual(' '); expect(codeEditor.prop('value')).toEqual('error content');
}); });
test('should pass onClose to Modal', () => { test('should pass onClose to Modal', () => {
@@ -226,7 +228,7 @@ describe('HostEventModal', () => {
const debugTaskAction = { const debugTaskAction = {
...hostEvent, ...hostEvent,
event_data: { event_data: {
taskAction: 'debug', task_action: 'debug',
res: { res: {
result: { result: {
stdout: 'foo bar', stdout: 'foo bar',
@@ -242,7 +244,7 @@ describe('HostEventModal', () => {
handleTabClick(null, 2); handleTabClick(null, 2);
wrapper.update(); wrapper.update();
const codeEditor = wrapper.find('CodeEditor'); const codeEditor = wrapper.find('Tab[eventKey=2] CodeEditor');
expect(codeEditor.prop('mode')).toBe('javascript'); expect(codeEditor.prop('mode')).toBe('javascript');
expect(codeEditor.prop('readOnly')).toBe(true); expect(codeEditor.prop('readOnly')).toBe(true);
expect(codeEditor.prop('value')).toEqual('foo bar'); expect(codeEditor.prop('value')).toEqual('foo bar');
@@ -252,7 +254,7 @@ describe('HostEventModal', () => {
const yumTaskAction = { const yumTaskAction = {
...hostEvent, ...hostEvent,
event_data: { event_data: {
taskAction: 'yum', task_action: 'yum',
res: { res: {
results: ['baz', 'bar'], results: ['baz', 'bar'],
}, },
@@ -266,9 +268,9 @@ describe('HostEventModal', () => {
handleTabClick(null, 2); handleTabClick(null, 2);
wrapper.update(); wrapper.update();
const codeEditor = wrapper.find('CodeEditor'); const codeEditor = wrapper.find('Tab[eventKey=2] CodeEditor');
expect(codeEditor.prop('mode')).toBe('javascript'); expect(codeEditor.prop('mode')).toBe('javascript');
expect(codeEditor.prop('readOnly')).toBe(true); expect(codeEditor.prop('readOnly')).toBe(true);
expect(codeEditor.prop('value')).toEqual('baz'); expect(codeEditor.prop('value')).toEqual('baz\nbar');
}); });
}); });