Skip to content

Commit

Permalink
Add test cases for Create CustomRuns
Browse files Browse the repository at this point in the history
  • Loading branch information
jisoolee committed Sep 28, 2023
1 parent f942ce4 commit 65865b7
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 240 deletions.
6 changes: 0 additions & 6 deletions src/api/customRuns.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { deleteRequest, get, patch, post } from './comms';
import {
getQueryParams,
getTektonAPI,
getTektonPipelinesAPIVersion,
removeSystemAnnotations,
removeSystemLabels,
useCollection,
Expand All @@ -43,10 +42,8 @@ function getCustomRunsAPI({ filters, isWebSocket, name, namespace }) {
}

export function getCustomRunPayload({
kind,
labels,
namespace,
nodeSelector,
params,
serviceAccount,
customName,
Expand Down Expand Up @@ -76,9 +73,6 @@ export function getCustomRunPayload({
value: params[name]
}));
}
if (nodeSelector) {
payload.spec.podTemplate = { nodeSelector };
}
if (serviceAccount) {
payload.spec.serviceAccountName = serviceAccount;
}
Expand Down
219 changes: 0 additions & 219 deletions src/containers/CreateCustomRun/CreateCustomRun.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,6 @@ const initialState = {
workspaces: ''
};

const initialParamsState = paramSpecs => {
if (!paramSpecs) {
return {};
}
return paramSpecs.reduce(
(acc, param) => ({ ...acc, [param.name]: param.default || '' }),
{}
);
};

const itemToString = ({ text }) => text;

function CreateCustomRun() {
Expand Down Expand Up @@ -98,11 +88,6 @@ function CreateCustomRun() {
);
}

function isYAMLMode() {
const urlSearchParams = new URLSearchParams(location.search);
return urlSearchParams.get('mode') === 'yaml';
}

const { kind: initialCustomKind, customName: customRefFromDetails } =
getCustomDetails();
const [
Expand Down Expand Up @@ -130,7 +115,6 @@ function CreateCustomRun() {
kind: initialCustomKind || 'Custom',
namespace: getNamespace(),
customRef: customRefFromDetails,
params: initialParamsState(null)
});

const { data: custom, error: customError } = useTaskByKind(
Expand All @@ -147,129 +131,6 @@ function CreateCustomRun() {
})
});

function switchToYamlMode() {
const queryParams = new URLSearchParams(location.search);
queryParams.set('mode', 'yaml');
const browserURL = location.pathname.concat(`?${queryParams.toString()}`);
navigate(browserURL);
}

function checkFormValidation() {
// Namespace, customRef, and Params must all have values
const validNamespace = !!namespace;
const validCustomRef = !!customRef;

const paramSpecMap = keyBy(paramSpecs, 'name');
const validParams =
!params ||
Object.keys(params).reduce(
(acc, name) =>
acc &&
(!!params[name] ||
typeof paramSpecMap[name]?.default !== 'undefined'),
true
);

// CustomRun name
const customRunNameTest =
!customRunName ||
(resourceNameRegex.test(customRunName) && customRunName.length < 64);
setState(state => ({ ...state, validCustomRunName: customRunNameTest }));

// Labels
let validLabels = true;
labels.forEach(label => {
['key', 'value'].forEach(type => {
if (!isValidLabel(type, label[type])) {
validLabels = false;
setState(prevState => ({
...prevState,
invalidLabels: {
...prevState.invalidLabels,
[`${label.id}-${type}`]: true
}
}));
}
});
});

return (
validNamespace &&
validCustomRef &&
validParams &&
validLabels &&
customRunNameTest
);
}

function handleClose() {
const { kind: customKind, customName } = getCustomDetails();
let url = urls.customRuns.all();
if (customName && namespace && namespace !== ALL_NAMESPACES) {
url = urls.customRuns[
customKind === 'ClusterTask' ? 'byClusterTask' : 'byTask'
]({
namespace,
customName
});
} else if (namespace && namespace !== ALL_NAMESPACES) {
url = urls.customRuns.byNamespace({ namespace });
}
navigate(url);
}

function handleAddLabel(prop) {
setState(prevState => ({
...prevState,
[prop]: [
...prevState[prop],
{
id: generateId(`label${prevState[prop].length}-`),
key: '',
keyPlaceholder: 'key',
value: '',
valuePlaceholder: 'value'
}
]
}));
}

function handleRemoveLabel(prop, invalidProp, index) {
setState(prevState => {
const newLabels = [...prevState[prop]];
const newInvalidLabels = { ...prevState[invalidProp] };
const removedLabel = newLabels[index];
newLabels.splice(index, 1);
if (removedLabel.id in newInvalidLabels) {
delete newInvalidLabels[`${removedLabel.id}-key`];
delete newInvalidLabels[`${removedLabel.id}-value`];
}
return {
...prevState,
[prop]: newLabels,
[invalidProp]: newInvalidLabels
};
});
}

function handleChangeLabel(prop, invalidProp, { type, index, value }) {
setState(prevState => {
const newLabels = [...prevState[prop]];
newLabels[index][type] = value;
const newInvalidLabels = { ...prevState[invalidProp] };
if (!isValidLabel(type, value)) {
newInvalidLabels[`${newLabels[index].id}-${type}`] = true;
} else {
delete newInvalidLabels[`${newLabels[index].id}-${type}`];
}
return {
...prevState,
[prop]: newLabels,
[invalidProp]: newInvalidLabels
};
});
}

function handleCloseYAMLEditor() {
let url = urls.customRuns.all();
if (defaultNamespace && defaultNamespace !== ALL_NAMESPACES) {
Expand All @@ -288,86 +149,6 @@ function CreateCustomRun() {
});
}

function handleNamespaceChange({ selectedItem }) {
const { text = '' } = selectedItem || {};
if (text !== namespace) {
setState(state => ({
...state,
...initialState,
kind: state.kind,
namespace: text
}));

const queryParams = new URLSearchParams(location.search);
if (text) {
queryParams.set('namespace', text);
} else {
queryParams.delete('namespace');
}
queryParams.delete('customName');
const browserURL = location.pathname.concat(`?${queryParams.toString()}`);
navigate(browserURL);
}
}

function handleKindChange({ selectedItem }) {
const { text = '' } = selectedItem || {};
if (text !== kind) {
setState(state => ({
...state,
...initialState,
kind: text
}));

const queryParams = new URLSearchParams(location.search);
queryParams.set('kind', text);
queryParams.delete('namespace');
queryParams.delete('customName');
const browserURL = location.pathname.concat(`?${queryParams.toString()}`);
navigate(browserURL);
}
}

function handleParamChange(key, value) {
setState(state => ({
...state,
params: {
...state.params,
[key]: value
}
}));
}

function handleCustomChange({ selectedItem }) {
const { text } = selectedItem || {};

const queryParams = new URLSearchParams(location.search);
if (text) {
queryParams.set('customName', text);
} else {
queryParams.delete('customName');
}
const browserURL = location.pathname.concat(`?${queryParams.toString()}`);
navigate(browserURL);

if (text && text !== customRef) {
setState(state => {
return {
...state,
customRef: text,
params: initialParamsState(paramSpecs)
};
});
return;
}
// Reset params when no Task is selected
setState(state => ({
...state,
...initialState,
namespace: state.namespace
}));
}

function handleSubmit(event) {
event.preventDefault();

Expand Down
Loading

0 comments on commit 65865b7

Please sign in to comment.