Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions webpack/JobInvocationDetail/JobInvocationConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const GET_TEMPLATE_INVOCATIONS = 'GET_TEMPLATE_INVOCATIONS';
export const CHANGE_ENABLED_RECURRING_LOGIC = 'CHANGE_ENABLED_RECURRING_LOGIC';
export const CANCEL_RECURRING_LOGIC = 'CANCEL_RECURRING_LOGIC';
export const GET_REPORT_TEMPLATES = 'GET_REPORT_TEMPLATES';
export const GET_REPORT_TEMPLATE_SETTING = 'GET_REPORT_TEMPLATE_SETTING';
export const GET_REPORT_TEMPLATE_INPUTS = 'GET_REPORT_TEMPLATE_INPUTS';
export const JOB_INVOCATION_HOSTS = 'JOB_INVOCATION_HOSTS';
export const GET_TEMPLATE_INVOCATION = 'GET_TEMPLATE_INVOCATION';
Expand Down
40 changes: 28 additions & 12 deletions webpack/JobInvocationDetail/JobInvocationToolbarButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import {
STATUS,
GET_REPORT_TEMPLATES,
GET_REPORT_TEMPLATE_SETTING,
GET_REPORT_TEMPLATE_INPUTS,
} from './JobInvocationConstants';
import { selectTaskCancelable } from './JobInvocationSelectors';
Expand Down Expand Up @@ -80,22 +81,37 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {

useEffect(() => {
let isMounted = true;
const fetchReportTemplate = templateName => {
dispatch(
get({
key: GET_REPORT_TEMPLATES,
url: '/api/report_templates',
params: {
search: `name="${templateName}"`,
per_page: 1,
},
handleSuccess: ({ data: { results } }) => {
if (isMounted) {
setReportTemplateJobId(results[0]?.id);
}
},
handleError: () => {
if (isMounted) {
setReportTemplateJobId(undefined);
}
},
})
);
};
dispatch(
get({
key: GET_REPORT_TEMPLATES,
url: '/api/report_templates',
handleSuccess: ({ data: { results } }) => {
if (isMounted) {
setReportTemplateJobId(
results.find(result => result.name === 'Job - Invocation Report')
?.id
);
}
key: GET_REPORT_TEMPLATE_SETTING,
url: '/api/settings/remote_execution_job_invocation_report_template',
handleSuccess: ({ data: { value } }) => {
fetchReportTemplate(value);
},
handleError: () => {
if (isMounted) {
setReportTemplateJobId(undefined);
}
fetchReportTemplate('Job - Invocation Report');
},
})
);
Expand Down
68 changes: 67 additions & 1 deletion webpack/JobInvocationDetail/__tests__/MainInformation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
CANCEL_RECURRING_LOGIC,
CHANGE_ENABLED_RECURRING_LOGIC,
GET_REPORT_TEMPLATES,
GET_REPORT_TEMPLATE_SETTING,
GET_REPORT_TEMPLATE_INPUTS,
GET_TASK,
JOB_INVOCATION_KEY,
Expand Down Expand Up @@ -85,7 +86,13 @@ jest.mock('foremanReact/routes/common/PageLayout/PageLayout', () =>

const setupApiMocks = () => {
api.get.mockImplementation(({ handleSuccess, key, ...action }) => {
if (key === GET_REPORT_TEMPLATES) {
if (key === GET_REPORT_TEMPLATE_SETTING) {
if (handleSuccess) {
handleSuccess({
data: { value: 'Job - Invocation Report' },
});
}
} else if (key === GET_REPORT_TEMPLATES) {
if (handleSuccess) {
handleSuccess({
data: mockReportTemplatesResponse,
Expand Down Expand Up @@ -323,6 +330,59 @@ describe('JobInvocationDetailPage', () => {
expect(createReportButton).toHaveClass('pf-m-disabled');
});

it('falls back to default template name when settings API fails', async () => {
api.get.mockImplementation(
({ handleSuccess, handleError, key, ...action }) => {
if (key === GET_REPORT_TEMPLATE_SETTING) {
if (handleError) {
handleError({ message: 'Not found' });
}
} else if (key === GET_REPORT_TEMPLATES) {
if (handleSuccess) {
handleSuccess({
data: mockReportTemplatesResponse,
});
}
} else if (key === GET_REPORT_TEMPLATE_INPUTS) {
if (handleSuccess) {
handleSuccess({
data: mockReportTemplateInputsResponse,
});
}
}

return { type: 'get', key, ...action };
}
);

const jobId = jobInvocationData.id;

renderJobInvocationDetailPage(createJobInvocationDetailState(), {
jobId,
});

expect(api.get).toHaveBeenCalledWith(
expect.objectContaining({
key: GET_REPORT_TEMPLATE_SETTING,
url: '/api/settings/remote_execution_job_invocation_report_template',
})
);
expect(api.get).toHaveBeenCalledWith(
expect.objectContaining({
key: GET_REPORT_TEMPLATES,
url: '/api/report_templates',
params: expect.objectContaining({
search: 'name="Job - Invocation Report"',
}),
})
);
expect(screen.getByText('Create report').getAttribute('href')).toEqual(
foremanUrl(
`/templates/report_templates/${mockReportTemplatesResponse.results[0].id}/generate?report_template_report%5Binput_values%5D%5B${mockReportTemplateInputsResponse.results[0].id}%5D%5Bvalue%5D=${jobId}`
)
);
});

it('should dispatch global actions', async () => {
const actualActions = jest.requireActual('../JobInvocationActions');
const { getTask } = jest.requireMock('../JobInvocationActions');
Expand All @@ -342,6 +402,12 @@ describe('JobInvocationDetailPage', () => {
{ jobId }
);

expect(api.get).toHaveBeenCalledWith(
expect.objectContaining({
key: GET_REPORT_TEMPLATE_SETTING,
url: '/api/settings/remote_execution_job_invocation_report_template',
})
);
expect(api.get).toHaveBeenCalledWith(
expect.objectContaining({
key: GET_REPORT_TEMPLATES,
Expand Down
Loading