|
| 1 | +import { trimFieldValue } from '../../components/provider-form/helper'; |
| 2 | + |
| 3 | +describe('trimFieldValue function', () => { |
| 4 | + it('should trim the hostname field', () => { |
| 5 | + const resource = { |
| 6 | + hostname: ' example.com ', |
| 7 | + otherField: 'value', |
| 8 | + }; |
| 9 | + |
| 10 | + const trimmedResource = trimFieldValue(resource); |
| 11 | + |
| 12 | + expect(trimmedResource.hostname).toEqual('example.com'); |
| 13 | + expect(trimmedResource.otherField).toEqual('value'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should handle nested objects and trim the hostname field', () => { |
| 17 | + const resource = { |
| 18 | + nestedObject: { |
| 19 | + hostname: ' nested-example.com ', |
| 20 | + }, |
| 21 | + otherField: 'value', |
| 22 | + }; |
| 23 | + |
| 24 | + const trimmedResource = trimFieldValue(resource); |
| 25 | + |
| 26 | + expect(trimmedResource.nestedObject.hostname).toEqual('nested-example.com'); |
| 27 | + expect(trimmedResource.otherField).toEqual('value'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should handle multiple nested objects and trim the hostname field', () => { |
| 31 | + const resource = { |
| 32 | + firstLevel: { |
| 33 | + nestedObject: { |
| 34 | + hostname: ' nested-example.com ', |
| 35 | + }, |
| 36 | + }, |
| 37 | + otherField: 'value', |
| 38 | + }; |
| 39 | + |
| 40 | + const trimmedResource = trimFieldValue(resource); |
| 41 | + |
| 42 | + expect(trimmedResource.firstLevel.nestedObject.hostname).toEqual('nested-example.com'); |
| 43 | + expect(trimmedResource.otherField).toEqual('value'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle undefined values', () => { |
| 47 | + const resource = { |
| 48 | + hostname: undefined, |
| 49 | + }; |
| 50 | + |
| 51 | + const trimmedResource = trimFieldValue(resource); |
| 52 | + |
| 53 | + expect(trimmedResource.hostname).toEqual(''); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle null values', () => { |
| 57 | + const resource = { |
| 58 | + hostname: null, |
| 59 | + }; |
| 60 | + |
| 61 | + const trimmedResource = trimFieldValue(resource); |
| 62 | + |
| 63 | + expect(trimmedResource.hostname).toEqual(''); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not modify non-trimmed fields', () => { |
| 67 | + const resource = { |
| 68 | + username: 'user123', |
| 69 | + |
| 70 | + }; |
| 71 | + |
| 72 | + const trimmedResource = trimFieldValue(resource); |
| 73 | + |
| 74 | + expect(trimmedResource.username).toEqual('user123'); |
| 75 | + expect(trimmedResource.email).toEqual('[email protected]'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle non-object values', () => { |
| 79 | + const resource = 'Hello'; |
| 80 | + |
| 81 | + const trimmedResource = trimFieldValue(resource); |
| 82 | + |
| 83 | + expect(trimmedResource).toEqual('Hello'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle non-object values inside an array', () => { |
| 87 | + const resource = [' example.com ', ' nested-example.com ', { hostname: ' test ' }]; |
| 88 | + |
| 89 | + const trimmedResource = trimFieldValue(resource); |
| 90 | + |
| 91 | + expect(trimmedResource).toEqual([' example.com ', ' nested-example.com ', { hostname: 'test' }]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle objects nested inside an array', () => { |
| 95 | + const resource = { |
| 96 | + otherField: ' example.com ', |
| 97 | + otherField2: [ |
| 98 | + { |
| 99 | + firstLevel: { |
| 100 | + nestedObject: { |
| 101 | + hostname: ' nested-example.com ', |
| 102 | + }, |
| 103 | + }, |
| 104 | + nestedField: 'value', |
| 105 | + }, |
| 106 | + ], |
| 107 | + }; |
| 108 | + |
| 109 | + const trimmedResource = trimFieldValue(resource); |
| 110 | + |
| 111 | + expect(trimmedResource).toEqual({ |
| 112 | + otherField: ' example.com ', |
| 113 | + otherField2: [ |
| 114 | + { |
| 115 | + firstLevel: { |
| 116 | + nestedObject: { |
| 117 | + hostname: 'nested-example.com', |
| 118 | + }, |
| 119 | + }, |
| 120 | + nestedField: 'value', |
| 121 | + }, |
| 122 | + ], |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments