Skip to content

Commit

Permalink
remove unnecessary util
Browse files Browse the repository at this point in the history
  • Loading branch information
coliu-akamai committed Dec 9, 2024
1 parent bc20b43 commit 90aeb37
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 149 deletions.
95 changes: 0 additions & 95 deletions packages/manager/src/utilities/formikErrorUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
getFormikErrorsFromAPIErrors,
handleAPIErrors,
handleVPCAndSubnetErrors,
set,
} from './formikErrorUtils';

Expand Down Expand Up @@ -34,100 +33,6 @@ describe('handleAPIErrors', () => {
});
});

const subnetMultipleErrorsPerField = [
{
field: 'subnets[0].label',
reason: 'not expected error for label',
},
{
field: 'subnets[0].label',
reason: 'expected error for label',
},
{
field: 'subnets[3].label',
reason: 'not expected error for label',
},
{
field: 'subnets[3].label',
reason: 'expected error for label',
},
{
field: 'subnets[3].ipv4',
reason: 'not expected error for ipv4',
},
{
field: 'subnets[3].ipv4',
reason: 'expected error for ipv4',
},
];

const subnetErrors = [
{
field: 'subnets[1].label',
reason: 'Label required',
},
{
field: 'subnets[2].label',
reason: 'bad label',
},
{
field: 'subnets[2].ipv4',
reason: 'cidr ipv4',
},
{
field: 'subnets[4].ipv4',
reason: 'needs an ip',
},
{
field: 'subnets[4].ipv6',
reason: 'needs an ipv6',
},
];

describe('handleVpcAndConvertSubnetErrors', () => {
it('converts API errors for subnets into a map of subnet index to SubnetErrors', () => {
const errors = handleVPCAndSubnetErrors(
subnetErrors,
setFieldError,
setError
);
expect(Object.keys(errors)).toHaveLength(3);
expect(Object.keys(errors)).toEqual(['1', '2', '4']);
expect(errors[1]).toEqual({ label: 'Label required' });
expect(errors[2]).toEqual({ ipv4: 'cidr ipv4', label: 'bad label' });
expect(errors[4]).toEqual({ ipv4: 'needs an ip', ipv6: 'needs an ipv6' });
});

it('takes the last error to display if a subnet field has multiple errors associated with it', () => {
const errors = handleVPCAndSubnetErrors(
subnetMultipleErrorsPerField,
setFieldError,
setError
);
expect(Object.keys(errors)).toHaveLength(2);
expect(errors[0]).toEqual({ label: 'expected error for label' });
expect(errors[3]).toEqual({
ipv4: 'expected error for ipv4',
label: 'expected error for label',
});
});

it('passes errors without the subnet field to handleApiErrors', () => {
const errors = handleVPCAndSubnetErrors(
errorWithField,
setFieldError,
setError
);
expect(Object.keys(errors)).toHaveLength(0);
expect(errors).toEqual({});
expect(setFieldError).toHaveBeenCalledWith(
'card_number',
errorWithField[0].reason
);
expect(setError).not.toHaveBeenCalled();
});
});

describe('getFormikErrorsFromAPIErrors', () => {
it('should convert APIError[] to errors in the shape formik expects', () => {
const testCases = [
Expand Down
54 changes: 0 additions & 54 deletions packages/manager/src/utilities/formikErrorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,57 +145,3 @@ export const handleAPIErrors = (
}
});
};

export interface SubnetError {
ipv4?: string;
ipv6?: string;
label?: string;
}

/**
* Handles given API errors and converts any specific subnet related errors into a usable format;
* Returns a map of subnets' indexes to their @interface SubnetError
* Example: errors = [{ reason: 'error1', field: 'subnets[1].label' },
* { reason: 'error2', field: 'subnets[1].ipv4' },
* { reason: 'not a subnet error so will not appear in return obj', field: 'label'},
* { reason: 'error3', field: 'subnets[4].ipv4' }]
* returns: {
* 1: { label: 'error1', ipv4: 'error2' },
* 4: { ipv4: 'error3'}
* }
*
* @param errors the errors from the API
* @param setFieldError function to set non-subnet related field errors
* @param setError function to set (non-subnet related) general API errors
*/
export const handleVPCAndSubnetErrors = (
errors: APIError[],
setFieldError: (field: string, message: string) => void,
setError?: (message: string) => void
) => {
const subnetErrors: Record<number, SubnetError> = {};
const nonSubnetErrors: APIError[] = [];

errors.forEach((error) => {
if (error.field && error.field.includes('subnets[')) {
const [subnetIdx, field] = error.field.split('.');
const idx = parseInt(
subnetIdx.substring(subnetIdx.indexOf('[') + 1, subnetIdx.indexOf(']')),
10
);

// if there already exists some previous error for the subnet at index idx, we
// just add the current error. Otherwise, we create a new entry for the subnet.
if (subnetErrors[idx]) {
subnetErrors[idx] = { ...subnetErrors[idx], [field]: error.reason };
} else {
subnetErrors[idx] = { [field]: error.reason };
}
} else {
nonSubnetErrors.push(error);
}
});

handleAPIErrors(nonSubnetErrors, setFieldError, setError);
return subnetErrors;
};

0 comments on commit 90aeb37

Please sign in to comment.