Skip to content

Commit

Permalink
Resolving any type build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vsushmita committed Sep 3, 2024
1 parent d64cd90 commit 8136f0f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as os from 'os';
import { flags } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import '../../../utils/prototypes';
import { IConfig } from '@oclif/config';
import OmniStudioBaseCommand from '../../basecommand';
import { DebugTimer, MigratedObject, MigratedRecordInfo } from '../../../utils';
import { MigrationResult, MigrationTool } from '../../../migration/interfaces';
import { ResultsBuilder } from '../../../utils/resultsbuilder';
import { LWCComponentMigrationTool, CustomLabelMigrationTool, ApexClassMigrationTool } from '../../../migration/interfaces';
import {
LWCComponentMigrationTool,
CustomLabelMigrationTool,
ApexClassMigrationTool,
} from '../../../migration/interfaces';

// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
Expand All @@ -29,22 +33,17 @@ export default class OmnistudioRelatedObjectMigrationFacade extends OmniStudioBa
public static examples = messages.getMessage('examples').split(os.EOL);
public static args = [{ name: 'file' }];

protected static flagsConfig = {
namespace: flags.string({
char: 'n',
description: messages.getMessage('namespaceFlagDescription'),
}),
only: flags.string({
char: 'o',
description: messages.getMessage('onlyFlagDescription'),
}),
allversions: flags.boolean({
char: 'a',
description: messages.getMessage('allVersionsDescription'),
required: false,
}),
};
protected readonly namespace: string;
protected readonly only: string;
protected readonly allversions: boolean;

public constructor(argv: string[], config: IConfig) {
super(argv, config);
const { flags } = this.parse(OmnistudioRelatedObjectMigrationFacade);
this.namespace = flags.namespace;
this.only = flags.only;
this.allversions = flags.allversions;
}
public async migrateAll(migrationResult: MigrationResult, namespace: string, relatedObjects: string[]): Promise<any> {
const apiVersion = '55.0'; // Define the API version or make it configurable
const conn = this.org.getConnection();
Expand All @@ -54,7 +53,7 @@ export default class OmnistudioRelatedObjectMigrationFacade extends OmniStudioBa
DebugTimer.getInstance().start();

// Declare an array of MigrationTool
let migrationTools: MigrationTool[] = [];
const migrationTools: MigrationTool[] = [];

// Initialize migration tools based on the relatedObjects parameter
if (relatedObjects.includes('lwc')) {
Expand Down Expand Up @@ -177,4 +176,4 @@ export default class OmnistudioRelatedObjectMigrationFacade extends OmniStudioBa

return mergedResults;
}
}
}
20 changes: 10 additions & 10 deletions src/migration/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface NameTransformData {
export interface RelatedObjectsMigrate {
/**
* Identifies migration candidates based on the provided migration results and namespace.
*
* @param migrationResults List of migration results to identify objects from.
* @param namespace The namespace used to identify objects.
* @returns List of identified migration candidates as strings.
Expand All @@ -94,21 +95,20 @@ export interface RelatedObjectsMigrate {

/**
* Private method to perform the migration of related objects based on the provided candidates.
*
* @param migrationResults List of migration results to use for migration.
* @param namespace The namespace used to perform the migration.
* @param migrationCandidates List of candidates to migrate.
*/
migrateRelatedObjects(migrationResults: MigrationResult[], namespace: string, migrationCandidates: string[]): Promise<void>;
migrateRelatedObjects(
migrationResults: MigrationResult[],
namespace: string,
migrationCandidates: string[]
): Promise<void>;
}

export interface LWCComponentMigrationTool extends MigrationTool {
// Specific methods or properties for LWCComponentMigrationTool if any
}
export type LWCComponentMigrationTool = MigrationTool;

export interface CustomLabelMigrationTool extends MigrationTool {
// Specific methods or properties for CustomLabelMigrationTool if any
}
export type CustomLabelMigrationTool = MigrationTool;

export interface ApexClassMigrationTool extends MigrationTool {
// Specific methods or properties for ApexClassMigrationTool if any
}
export type ApexClassMigrationTool = MigrationTool;
1 change: 1 addition & 0 deletions test/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
'@typescript-eslint/no-empty-function': 'off',
// Easily return a promise in a mocked method.
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
},
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { ux } from '@salesforce/command';
import OmniStudioBaseCommand from '../../basecommand';
import { LWCComponentMigrationTool, CustomLabelMigrationTool, ApexClassMigrationTool } from '../../../migration/interfaces';
import OmnistudioRelatedObjectMigrationFacade from '../../../path/to/OmnistudioRelatedObjectMigrationFacade'; // Adjust import as necessary
import { DebugTimer, MigratedObject, MigratedRecordInfo } from '../../../utils';
import { MigrationResult, MigrationTool } from '../../../migration/interfaces';
import { IConfig } from '@oclif/config';
import OmnistudioRelatedObjectMigrationFacade from '../../../../src/commands/omnistudio/migration/OmnistudioRelatedObjectMigrationFacade'; // Adjust import as necessary
import {
MigrationResult,
LWCComponentMigrationTool,
CustomLabelMigrationTool,
ApexClassMigrationTool,
UploadRecordResult,
MigratedObject,
} from '../../../../src/migration/interfaces';

// Define mock types for dependencies
interface OrgConnection {
getConnection: () => {
setApiVersion: sinon.SinonStub;
instanceUrl: string;
};
}

interface Logger {
error: sinon.SinonStub;
debug: sinon.SinonStub;
}

interface UX {
log: sinon.SinonStub;
}

// Define a mock config object
const mockConfig: IConfig = {
root: '',
userAgent: '',
version: '1.0.0',
// Add any other properties that might be needed
} as unknown as IConfig;

// Mock classes for Migration Tools
class MockLWCComponentMigrationTool implements LWCComponentMigrationTool {
public migrate = sinon.stub().resolves([{ name: 'LWC Component', id: '001' }]);
public truncate = sinon.stub().resolves();
public getName = sinon.stub().returns('LWC Component');
}

class MockCustomLabelMigrationTool implements CustomLabelMigrationTool {
public migrate = sinon.stub().resolves([{ name: 'Custom Label', id: '002' }]);
public truncate = sinon.stub().resolves();
public getName = sinon.stub().returns('Custom Label');
}

class MockApexClassMigrationTool implements ApexClassMigrationTool {
public migrate = sinon.stub().resolves([{ name: 'Apex Class', id: '003' }]);
public truncate = sinon.stub().resolves();
public getName = sinon.stub().returns('Apex Class');
}

describe('OmnistudioRelatedObjectMigrationFacade', function () {
let facade: OmnistudioRelatedObjectMigrationFacade;
Expand All @@ -14,50 +62,50 @@ describe('OmnistudioRelatedObjectMigrationFacade', function () {
beforeEach(() => {
sandbox = sinon.createSandbox();

// Stub the necessary methods and objects
facade = new OmnistudioRelatedObjectMigrationFacade();
// Initialize facade with proper types
facade = new OmnistudioRelatedObjectMigrationFacade([], mockConfig);

// Stub the 'org' property
sandbox.stub(facade, 'org').value({
getConnection: () => ({
setApiVersion: sinon.stub(),
instanceUrl: 'http://example.com'
})
});
setApiVersion: sinon.stub().resolves(),
instanceUrl: 'http://example.com',
}),
} as OrgConnection);

// Stub the 'logger' property
sandbox.stub(facade, 'logger').value({
error: sinon.stub(),
debug: sinon.stub()
});

sandbox.stub(facade, 'ux').value(ux);
error: sinon.stub().callsFake(() => {}), // No-op
debug: sinon.stub().callsFake(() => {}), // No-op
} as Logger);

// Stub the 'ux' property
sandbox.stub(facade, 'ux').value({
log: sinon.stub().callsFake(() => {}), // No-op
} as UX);
});

afterEach(() => {
sandbox.restore();
});

it('should migrate all specified objects', async function () {
it('should migrate all specified objects successfully', async function () {
const migrationResult: MigrationResult = {
records: new Map(),
results: new Map()
records: new Map<string, UploadRecordResult>(), // Use specific type if known
results: new Map<string, UploadRecordResult>(),
};

const lwcTool = sandbox.createStubInstance(LWCComponentMigrationTool);
const labelsTool = sandbox.createStubInstance(CustomLabelMigrationTool);
const apexTool = sandbox.createStubInstance(ApexClassMigrationTool);
// Create instances of mock tools
const lwcTool = new MockLWCComponentMigrationTool();
const labelsTool = new MockCustomLabelMigrationTool();
const apexTool = new MockApexClassMigrationTool();

lwcTool.migrate.resolves([{ name: 'LWC Component', id: '001' }]);
labelsTool.migrate.resolves([{ name: 'Custom Label', id: '002' }]);
apexTool.migrate.resolves([{ name: 'Apex Class', id: '003' }]);
// Stub the factory methods to return our mock tools
sandbox.stub(facade, 'createLWCComponentMigrationTool').returns(lwcTool);
sandbox.stub(facade, 'createCustomLabelMigrationTool').returns(labelsTool);
sandbox.stub(facade, 'createApexClassMigrationTool').returns(apexTool);

sandbox.stub(facade, 'migrateAll').resolves({
objectMigrationResults: [
{ name: 'LWC Component', data: [] },
{ name: 'Custom Label', data: [] },
{ name: 'Apex Class', data: [] }
]
});

await facade.migrateAll(migrationResult, 'testNamespace', ['lwc', 'labels', 'apex']);
const result = await facade.migrateAll(migrationResult, 'testNamespace', ['lwc', 'labels', 'apex']);

expect(lwcTool.migrate.calledOnce).to.be.true;
expect(labelsTool.migrate.calledOnce).to.be.true;
Expand All @@ -66,31 +114,33 @@ describe('OmnistudioRelatedObjectMigrationFacade', function () {
// Assert that the migration results are processed correctly
expect(facade.logger.debug.calledOnce).to.be.true;
expect(facade.ux.log.called).to.have.lengthOf(3); // Assuming each tool logs once

// Type assertion to avoid `any` issues
const objectMigrationResults = result as { objectMigrationResults: MigratedObject[] };
expect(objectMigrationResults.objectMigrationResults).to.have.lengthOf(3); // Verify result count
});

it('should handle errors during migration', async function () {
const migrationResult: MigrationResult = {
records: new Map(),
results: new Map()
records: new Map<string, UploadRecordResult>(), // Use specific type if known
results: new Map<string, UploadRecordResult>(),
};

const lwcTool = sandbox.createStubInstance(LWCComponentMigrationTool);
const labelsTool = sandbox.createStubInstance(CustomLabelMigrationTool);
const apexTool = sandbox.createStubInstance(ApexClassMigrationTool);
// Create instances of mock tools
const lwcTool = new MockLWCComponentMigrationTool();
const labelsTool = new MockCustomLabelMigrationTool();
const apexTool = new MockApexClassMigrationTool();

lwcTool.migrate.rejects(new Error('LWC migration error'));
labelsTool.migrate.resolves([{ name: 'Custom Label', id: '002' }]);
apexTool.migrate.resolves([{ name: 'Apex Class', id: '003' }]);

sandbox.stub(facade, 'migrateAll').resolves({
objectMigrationResults: [
{ name: 'LWC Component', data: [], errors: ['LWC migration error'] },
{ name: 'Custom Label', data: [] },
{ name: 'Apex Class', data: [] }
]
});
// Stub the factory methods to return our mock tools
sandbox.stub(facade, 'createLWCComponentMigrationTool').returns(lwcTool);
sandbox.stub(facade, 'createCustomLabelMigrationTool').returns(labelsTool);
sandbox.stub(facade, 'createApexClassMigrationTool').returns(apexTool);

await facade.migrateAll(migrationResult, 'testNamespace', ['lwc', 'labels', 'apex']);
const result = await facade.migrateAll(migrationResult, 'testNamespace', ['lwc', 'labels', 'apex']);

expect(lwcTool.migrate.calledOnce).to.be.true;
expect(labelsTool.migrate.calledOnce).to.be.true;
Expand All @@ -99,5 +149,9 @@ describe('OmnistudioRelatedObjectMigrationFacade', function () {
// Check that the error was logged
expect(facade.logger.error.calledOnce).to.be.true;
expect(facade.ux.log.called).to.have.lengthOf(3); // Assuming each tool logs once

// Type assertion to avoid `any` issues
const objectMigrationResults = result as { objectMigrationResults: MigratedObject[] };
expect(objectMigrationResults.objectMigrationResults).to.be.an('array').that.has.lengthOf(3); // Verify result count
});
});
});

0 comments on commit 8136f0f

Please sign in to comment.