Skip to content

Commit

Permalink
Fixed no implicit conversion of String into Integer Error when Addi…
Browse files Browse the repository at this point in the history
…ng/Editing Container Provider
  • Loading branch information
DavidResende0 committed Jan 12, 2024
1 parent 745d91d commit 458cb18
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 3 deletions.
13 changes: 13 additions & 0 deletions app/javascript/components/provider-form/helper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
/** Field names that needs to be trimmed. */
const trimFields = ['hostname'];

const isObject = (data) => (typeof data === 'object' && data !== null);

/** Function to trim a field's value.
* If the nested data object contains the key which includes in variable 'trimFields',
* then, trim the spaces from the value before sending to API.
*/
export const trimFieldValue = (resource) => {
if (!isObject(resource)) {
return resource;
}

if (Array.isArray(resource)) {
resource.forEach((value, index) => {
resource[index] = trimFieldValue(value);
});
return resource;
}

const keys = Object.keys(resource);

keys.find((key) => {
Expand Down
6 changes: 3 additions & 3 deletions app/javascript/components/provider-form/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ const ProviderForm = ({
}

// Construct the full form data with all the necessary items
const data = {
const data = trimFieldValue({
...rest,
endpoints: trimFieldValue(endpoints),
endpoints,
authentications,
...(edit ? undefined : { type }),
ddf: true,
};
});

const request = providerId ? API.patch(`/api/providers/${providerId}`, data) : API.post('/api/providers', data);
request.then(() => miqRedirectBack(message, 'success', redirect)).catch(miqSparkleOff);
Expand Down
125 changes: 125 additions & 0 deletions app/javascript/spec/provider-form/helper.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { trimFieldValue } from '../../components/provider-form/helper';

describe('trimFieldValue function', () => {
it('should trim the hostname field', () => {
const resource = {
hostname: ' example.com ',
otherField: 'value',
};

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource.hostname).toEqual('example.com');
expect(trimmedResource.otherField).toEqual('value');
});

it('should handle nested objects and trim the hostname field', () => {
const resource = {
nestedObject: {
hostname: ' nested-example.com ',
},
otherField: 'value',
};

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource.nestedObject.hostname).toEqual('nested-example.com');
expect(trimmedResource.otherField).toEqual('value');
});

it('should handle multiple nested objects and trim the hostname field', () => {
const resource = {
firstLevel: {
nestedObject: {
hostname: ' nested-example.com ',
},
},
otherField: 'value',
};

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource.firstLevel.nestedObject.hostname).toEqual('nested-example.com');
expect(trimmedResource.otherField).toEqual('value');
});

it('should handle undefined values', () => {
const resource = {
hostname: undefined,
};

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource.hostname).toEqual('');
});

it('should handle null values', () => {
const resource = {
hostname: null,
};

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource.hostname).toEqual('');
});

it('should not modify non-trimmed fields', () => {
const resource = {
username: 'user123',
email: '[email protected]',
};

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource.username).toEqual('user123');
expect(trimmedResource.email).toEqual('[email protected]');
});

it('should handle non-object values', () => {
const resource = 'Hello';

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource).toEqual('Hello');
});

it('should handle non-object values inside an array', () => {
const resource = [' example.com ', ' nested-example.com ', { hostname: ' test ' }];

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource).toEqual([' example.com ', ' nested-example.com ', { hostname: 'test' }]);
});

it('should handle objects nested inside an array', () => {
const resource = {
otherField: ' example.com ',
otherField2: [
{
firstLevel: {
nestedObject: {
hostname: ' nested-example.com ',
},
},
nestedField: 'value',
},
],
};

const trimmedResource = trimFieldValue(resource);

expect(trimmedResource).toEqual({
otherField: ' example.com ',
otherField2: [
{
firstLevel: {
nestedObject: {
hostname: 'nested-example.com',
},
},
nestedField: 'value',
},
],
});
});
});

0 comments on commit 458cb18

Please sign in to comment.