|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return, camelcase, comma-dangle */ |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { CardMigrationTool } from '../../../src/migration/flexcard'; |
| 4 | +import { NameMappingRegistry } from '../../../src/migration/NameMappingRegistry'; |
| 5 | +import CardMappings from '../../../src/mappings/VlocityCard'; |
| 6 | + |
| 7 | +describe('FlexCard Community Targets Functionality', () => { |
| 8 | + let cardTool: CardMigrationTool; |
| 9 | + let nameRegistry: NameMappingRegistry; |
| 10 | + let mockConnection: any; |
| 11 | + let mockMessages: any; |
| 12 | + let mockUx: any; |
| 13 | + let mockLogger: any; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + nameRegistry = NameMappingRegistry.getInstance(); |
| 17 | + nameRegistry.clear(); |
| 18 | + |
| 19 | + // Use simple mock objects instead of Sinon stubs to avoid conflicts |
| 20 | + mockConnection = {}; |
| 21 | + mockMessages = { |
| 22 | + getMessage: () => 'Mock message for testing', |
| 23 | + }; |
| 24 | + mockUx = {}; |
| 25 | + mockLogger = {}; |
| 26 | + |
| 27 | + cardTool = new CardMigrationTool('vlocity_ins', mockConnection, mockLogger, mockMessages, mockUx, false); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('ensureCommunityTargets', () => { |
| 31 | + it('should add community targets when xmlObject.targets does not exist', () => { |
| 32 | + const mappedObject = { |
| 33 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 34 | + xmlObject: { |
| 35 | + // No targets property |
| 36 | + }, |
| 37 | + }), |
| 38 | + }; |
| 39 | + |
| 40 | + // Call the private method via type assertion |
| 41 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 42 | + |
| 43 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 44 | + expect(updatedDefinition.xmlObject.targets).to.exist; |
| 45 | + expect(updatedDefinition.xmlObject.targets.target).to.be.an('array'); |
| 46 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Page'); |
| 47 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should add community targets when targets exist but are empty', () => { |
| 51 | + const mappedObject = { |
| 52 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 53 | + xmlObject: { |
| 54 | + targets: { |
| 55 | + target: [], |
| 56 | + }, |
| 57 | + }, |
| 58 | + }), |
| 59 | + }; |
| 60 | + |
| 61 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 62 | + |
| 63 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 64 | + expect(updatedDefinition.xmlObject.targets.target).to.have.length(2); |
| 65 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Page'); |
| 66 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should add missing community targets when some targets already exist', () => { |
| 70 | + const mappedObject = { |
| 71 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 72 | + xmlObject: { |
| 73 | + targets: { |
| 74 | + target: ['lightning__AppPage', 'lightningCommunity__Page'], |
| 75 | + }, |
| 76 | + }, |
| 77 | + }), |
| 78 | + }; |
| 79 | + |
| 80 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 81 | + |
| 82 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 83 | + expect(updatedDefinition.xmlObject.targets.target).to.have.length(3); |
| 84 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightning__AppPage'); |
| 85 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Page'); |
| 86 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should not add duplicate community targets when they already exist', () => { |
| 90 | + const mappedObject = { |
| 91 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 92 | + xmlObject: { |
| 93 | + targets: { |
| 94 | + target: ['lightningCommunity__Page', 'lightningCommunity__Default', 'lightning__AppPage'], |
| 95 | + }, |
| 96 | + }, |
| 97 | + }), |
| 98 | + }; |
| 99 | + |
| 100 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 101 | + |
| 102 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 103 | + expect(updatedDefinition.xmlObject.targets.target).to.have.length(3); |
| 104 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Page'); |
| 105 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 106 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightning__AppPage'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should convert non-array target to array and add community targets', () => { |
| 110 | + const mappedObject = { |
| 111 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 112 | + xmlObject: { |
| 113 | + targets: { |
| 114 | + target: 'lightning__AppPage', // Single string instead of array |
| 115 | + }, |
| 116 | + }, |
| 117 | + }), |
| 118 | + }; |
| 119 | + |
| 120 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 121 | + |
| 122 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 123 | + expect(updatedDefinition.xmlObject.targets.target).to.be.an('array'); |
| 124 | + expect(updatedDefinition.xmlObject.targets.target).to.have.length(3); |
| 125 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightning__AppPage'); |
| 126 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Page'); |
| 127 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should handle empty definition gracefully', () => { |
| 131 | + const mappedObject = { |
| 132 | + [CardMappings.Definition__c]: '{}', |
| 133 | + }; |
| 134 | + |
| 135 | + // Should not throw an error |
| 136 | + expect(() => { |
| 137 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 138 | + }).to.not.throw(); |
| 139 | + |
| 140 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 141 | + expect(updatedDefinition).to.deep.equal({}); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle missing definition gracefully', () => { |
| 145 | + const mappedObject = {}; |
| 146 | + |
| 147 | + // Should not throw an error |
| 148 | + expect(() => { |
| 149 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 150 | + }).to.not.throw(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should handle definition without xmlObject gracefully', () => { |
| 154 | + const mappedObject = { |
| 155 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 156 | + someOtherProperty: 'value', |
| 157 | + // No xmlObject |
| 158 | + }), |
| 159 | + }; |
| 160 | + |
| 161 | + // Should not throw an error |
| 162 | + expect(() => { |
| 163 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 164 | + }).to.not.throw(); |
| 165 | + |
| 166 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 167 | + expect(updatedDefinition.someOtherProperty).to.equal('value'); |
| 168 | + expect(updatedDefinition.xmlObject).to.be.undefined; |
| 169 | + }); |
| 170 | + |
| 171 | + it('should handle null xmlObject gracefully', () => { |
| 172 | + const mappedObject = { |
| 173 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 174 | + xmlObject: null, |
| 175 | + }), |
| 176 | + }; |
| 177 | + |
| 178 | + // Should not throw an error |
| 179 | + expect(() => { |
| 180 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 181 | + }).to.not.throw(); |
| 182 | + |
| 183 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 184 | + expect(updatedDefinition.xmlObject).to.be.null; |
| 185 | + }); |
| 186 | + |
| 187 | + it('should preserve existing properties while adding community targets', () => { |
| 188 | + const mappedObject = { |
| 189 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 190 | + xmlObject: { |
| 191 | + apiVersion: '55.0', |
| 192 | + isExposed: true, |
| 193 | + targets: { |
| 194 | + target: ['lightning__AppPage'], |
| 195 | + }, |
| 196 | + masterLabel: 'Test Card', |
| 197 | + }, |
| 198 | + otherProperty: 'preserved', |
| 199 | + }), |
| 200 | + }; |
| 201 | + |
| 202 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 203 | + |
| 204 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 205 | + expect(updatedDefinition.xmlObject.apiVersion).to.equal('55.0'); |
| 206 | + expect(updatedDefinition.xmlObject.isExposed).to.be.true; |
| 207 | + expect(updatedDefinition.xmlObject.masterLabel).to.equal('Test Card'); |
| 208 | + expect(updatedDefinition.otherProperty).to.equal('preserved'); |
| 209 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightning__AppPage'); |
| 210 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Page'); |
| 211 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 212 | + }); |
| 213 | + |
| 214 | + it('should handle malformed JSON gracefully', () => { |
| 215 | + const mappedObject = { |
| 216 | + [CardMappings.Definition__c]: 'invalid json {', |
| 217 | + }; |
| 218 | + |
| 219 | + // Should throw an error due to JSON.parse, but not crash the application |
| 220 | + expect(() => { |
| 221 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 222 | + }).to.throw(); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should verify both required community targets are added', () => { |
| 226 | + const mappedObject = { |
| 227 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 228 | + xmlObject: { |
| 229 | + targets: { |
| 230 | + target: [], |
| 231 | + }, |
| 232 | + }, |
| 233 | + }), |
| 234 | + }; |
| 235 | + |
| 236 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 237 | + |
| 238 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 239 | + const requiredTargets = ['lightningCommunity__Page', 'lightningCommunity__Default']; |
| 240 | + |
| 241 | + requiredTargets.forEach((target) => { |
| 242 | + expect(updatedDefinition.xmlObject.targets.target).to.include(target); |
| 243 | + }); |
| 244 | + |
| 245 | + expect(updatedDefinition.xmlObject.targets.target).to.have.length(2); |
| 246 | + }); |
| 247 | + |
| 248 | + it('should handle complex existing target arrays', () => { |
| 249 | + const existingTargets = [ |
| 250 | + 'lightning__AppPage', |
| 251 | + 'lightning__HomePage', |
| 252 | + 'lightning__RecordPage', |
| 253 | + 'lightningCommunity__Page', // Already exists |
| 254 | + ]; |
| 255 | + |
| 256 | + const mappedObject = { |
| 257 | + [CardMappings.Definition__c]: JSON.stringify({ |
| 258 | + xmlObject: { |
| 259 | + targets: { |
| 260 | + target: [...existingTargets], |
| 261 | + }, |
| 262 | + }, |
| 263 | + }), |
| 264 | + }; |
| 265 | + |
| 266 | + (cardTool as any).ensureCommunityTargets(mappedObject); |
| 267 | + |
| 268 | + const updatedDefinition = JSON.parse(mappedObject[CardMappings.Definition__c]); |
| 269 | + |
| 270 | + // Should have all original targets plus the missing community target |
| 271 | + expect(updatedDefinition.xmlObject.targets.target).to.have.length(5); |
| 272 | + existingTargets.forEach((target) => { |
| 273 | + expect(updatedDefinition.xmlObject.targets.target).to.include(target); |
| 274 | + }); |
| 275 | + expect(updatedDefinition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + describe('Integration with mapVlocityCardRecord', () => { |
| 280 | + it('should ensure community targets are added during card mapping', () => { |
| 281 | + const testCard: any = { |
| 282 | + Id: 'card1', |
| 283 | + Name: 'Test Card', |
| 284 | + vlocity_ins__Definition__c: JSON.stringify({ |
| 285 | + xmlObject: { |
| 286 | + targets: { |
| 287 | + target: ['lightning__AppPage'], |
| 288 | + }, |
| 289 | + }, |
| 290 | + }), |
| 291 | + }; |
| 292 | + |
| 293 | + const result = (cardTool as any).mapVlocityCardRecord(testCard, new Map(), new Map()); |
| 294 | + const definition = JSON.parse(result['PropertySetConfig']); |
| 295 | + |
| 296 | + expect(definition.xmlObject.targets.target).to.include('lightning__AppPage'); |
| 297 | + expect(definition.xmlObject.targets.target).to.include('lightningCommunity__Page'); |
| 298 | + expect(definition.xmlObject.targets.target).to.include('lightningCommunity__Default'); |
| 299 | + }); |
| 300 | + |
| 301 | + it('should handle cards without xmlObject during mapping', () => { |
| 302 | + const testCard: any = { |
| 303 | + Id: 'card1', |
| 304 | + Name: 'Test Card', |
| 305 | + vlocity_ins__Definition__c: JSON.stringify({ |
| 306 | + someProperty: 'value', |
| 307 | + }), |
| 308 | + }; |
| 309 | + |
| 310 | + // Should not throw an error |
| 311 | + expect(() => { |
| 312 | + (cardTool as any).mapVlocityCardRecord(testCard, new Map(), new Map()); |
| 313 | + }).to.not.throw(); |
| 314 | + |
| 315 | + const result = (cardTool as any).mapVlocityCardRecord(testCard, new Map(), new Map()); |
| 316 | + const definition = JSON.parse(result['PropertySetConfig']); |
| 317 | + |
| 318 | + expect(definition.someProperty).to.equal('value'); |
| 319 | + }); |
| 320 | + }); |
| 321 | +}); |
0 commit comments