Skip to content

Commit

Permalink
Adding more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbreese committed Feb 20, 2024
1 parent d10c2dd commit 7c0fa69
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 103 deletions.
2 changes: 1 addition & 1 deletion resources/instructions/createSObjectLwcQuickActions.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h1>Create sObject LWC Quick Actions</h1>
<!-- will be filled in programatically below -->
</tbody>
</table>

<div id="errorDiv">
<!-- placeholder for any error messages -->
</div>
Expand Down
190 changes: 102 additions & 88 deletions src/commands/wizard/lwcGenerationCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,22 @@ export class LwcGenerationCommand {
type: 'generateLwcQuickActions',
action: async (_panel, _data, callback) => {
const quickActionStatus =
await LwcGenerationCommand.checkForExistingQuickActions().catch(error => {
if (callback) {
callback({error: error});
await LwcGenerationCommand.checkForExistingQuickActions().catch(
(error) => {
if (callback) {
callback({ error: error });
}
return;
}
return;
});
);

const newLwcQuickActionStatus =
await LwcGenerationCommand.generateMissingLwcsAndQuickActions(
extensionUri,
quickActionStatus!
).catch(error => {
).catch((error) => {
if (callback) {
callback({error: error});
callback({ error: error });
}
return;
});
Expand All @@ -81,10 +83,12 @@ export class LwcGenerationCommand {
action: async (_panel, _data, callback) => {
if (callback) {
const quickActionStatus =
await LwcGenerationCommand.checkForExistingQuickActions().catch(error => {
callback({error: error});
return;
});
await LwcGenerationCommand.checkForExistingQuickActions().catch(
(error) => {
callback({ error: error });
return;
}
);
callback(quickActionStatus!);
}
}
Expand Down Expand Up @@ -123,96 +127,106 @@ export class LwcGenerationCommand {
}

static async checkForExistingQuickActions(): Promise<SObjectQuickActionStatus> {
return new Promise<SObjectQuickActionStatus>(async (resolve, reject) => {
const results: SObjectQuickActionStatus = { sobjects: {} };

const sObjectsStatus = await this.getSObjectsFromLandingPage().catch(error => {
return reject(error);
});

sObjectsStatus!.sobjects.forEach((sobject) => {
const quickActionStatus: QuickActionStatus = {
view: false,
edit: false,
create: false
};
quickActionStatus.view =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'view'
);
quickActionStatus.edit =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'edit'
);
quickActionStatus.create =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'create'
);

results.sobjects[sobject] = quickActionStatus;
});

return resolve(results);
});
return new Promise<SObjectQuickActionStatus>(
async (resolve, reject) => {
const results: SObjectQuickActionStatus = { sobjects: {} };

const sObjectsStatus =
await this.getSObjectsFromLandingPage().catch((error) => {
return reject(error);
});

sObjectsStatus!.sobjects.forEach((sobject) => {
const quickActionStatus: QuickActionStatus = {
view: false,
edit: false,
create: false
};
quickActionStatus.view =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'view'
);
quickActionStatus.edit =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'edit'
);
quickActionStatus.create =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'create'
);

results.sobjects[sobject] = quickActionStatus;
});

return resolve(results);
}
);
}

static async generateMissingLwcsAndQuickActions(
extensionUri: Uri,
quickActionStatus: SObjectQuickActionStatus
): Promise<SObjectQuickActionStatus> {
return new Promise<SObjectQuickActionStatus>(async (resolve, reject) => {
for (const sobject in quickActionStatus.sobjects) {
try {
const quickActions = quickActionStatus.sobjects[sobject];

if (
!quickActions.create ||
!quickActions.edit ||
!quickActions.view
) {
// at least 1 needs to be created
const compactLayoutFields =
await OrgUtils.getCompactLayoutFieldsForSObject(
sobject
).catch(err => {
reject(`An error occurred while obtaining layout for ${sobject} : ${(err as Error).message}`);
return;
});

const codeBuilder = new CodeBuilder(
extensionUri,
sobject,
compactLayoutFields!
);
return new Promise<SObjectQuickActionStatus>(
async (resolve, reject) => {
for (const sobject in quickActionStatus.sobjects) {
try {
const quickActions =
quickActionStatus.sobjects[sobject];

if (
!quickActions.create ||
!quickActions.edit ||
!quickActions.view
) {
// at least 1 needs to be created
const compactLayoutFields =
await OrgUtils.getCompactLayoutFieldsForSObject(
sobject
).catch((err) => {
reject(
`An error occurred while obtaining layout for ${sobject} : ${
(err as Error).message
}`
);
return;
});

if (!quickActions.view) {
await codeBuilder.generateView();
}
const codeBuilder = new CodeBuilder(
extensionUri,
sobject,
compactLayoutFields!
);

if (!quickActions.edit) {
await codeBuilder.generateEdit();
}
if (!quickActions.view) {
await codeBuilder.generateView();
}

if (!quickActions.create) {
await codeBuilder.generateCreate();
if (!quickActions.edit) {
await codeBuilder.generateEdit();
}

if (!quickActions.create) {
await codeBuilder.generateCreate();
}
}
} catch (err) {
console.error(
`Could not generate quick actions for sobject ${sobject}, so skipping`,
err
);
}
} catch (err) {
console.error(
`Could not generate quick actions for sobject ${sobject}, so skipping`,
err
);
}
}

// Just double check now that things have been created.
const newStatus =
await LwcGenerationCommand.checkForExistingQuickActions();
resolve(newStatus);
});
// Just double check now that things have been created.
const newStatus =
await LwcGenerationCommand.checkForExistingQuickActions();
resolve(newStatus);
}
);
}

private static checkForExistingQuickAction(
Expand Down
86 changes: 72 additions & 14 deletions src/test/suite/commands/wizard/lwcGenerationCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ import {
} from '../../../../commands/wizard/lwcGenerationCommand';
import { WorkspaceUtils } from '../../../../utils/workspaceUtils';
import { TempProjectDirManager } from '../../../TestHelper';
import { Uri } from 'vscode';
import { CompactLayoutField, OrgUtils } from '../../../../utils/orgUtils';
import { CodeBuilder } from '../../../../utils/codeBuilder';
import { create } from 'domain';

suite('LWC Generation Command Test Suite', () => {
beforeEach(function () {});
let sandbox: sinon.SinonSandbox;

beforeEach(function () {
sandbox = sinon.createSandbox();
});

afterEach(function () {
sinon.restore();
sandbox.restore();
});

test('Quick Action directories check', async () => {
const baseDir = 'force-app/main/default/quickActions';
const statSyncStub = sinon.stub(fs, 'statSync');
const statsStub = sinon.createStubInstance(fs.Stats);
const statSyncStub = sandbox.stub(fs, 'statSync');
const statsStub = sandbox.createStubInstance(fs.Stats);
statsStub.isFile.returns(true);

// stub the file system responses - any return value is a positive hit, an exception is a negative hit
Expand All @@ -51,7 +59,7 @@ suite('LWC Generation Command Test Suite', () => {
.withArgs(`${baseDir}/sobject2.create.quickAction-meta.xml`)
.throws('error');

const getSObjectsStub = sinon.stub(
const getSObjectsStub = sandbox.stub(
LwcGenerationCommand,
'getSObjectsFromLandingPage'
);
Expand Down Expand Up @@ -97,7 +105,7 @@ suite('LWC Generation Command Test Suite', () => {

test('Should return error status for landing page with invalid json', async () => {
const dirManager = await TempProjectDirManager.createTempProjectDir();
const getWorkspaceDirStub = sinon.stub(
const getWorkspaceDirStub = sandbox.stub(
WorkspaceUtils,
'getStaticResourcesDir'
);
Expand All @@ -112,13 +120,16 @@ suite('LWC Generation Command Test Suite', () => {
);

const status =
await LwcGenerationCommand.getSObjectsFromLandingPage().then(results => {
// an error should have occurred
assert.fail('Invalid JSON should have caused a rejection of the promise.');
}).catch(error => {
assert.ok(error && error.length > 0);
});

await LwcGenerationCommand.getSObjectsFromLandingPage()
.then((results) => {
// an error should have occurred
assert.fail(
'Invalid JSON should have caused a rejection of the promise.'
);
})
.catch((error) => {
assert.ok(error && error.length > 0);
});
} finally {
getWorkspaceDirStub.restore();
await dirManager.removeDir();
Expand All @@ -127,7 +138,7 @@ suite('LWC Generation Command Test Suite', () => {

test('Should return 2 sObjects', async () => {
const dirManager = await TempProjectDirManager.createTempProjectDir();
const getWorkspaceDirStub = sinon.stub(
const getWorkspaceDirStub = sandbox.stub(
WorkspaceUtils,
'getStaticResourcesDir'
);
Expand All @@ -153,4 +164,51 @@ suite('LWC Generation Command Test Suite', () => {
await dirManager.removeDir();
}
});

test('Should generate view, create, and edit quick actions', async () => {
const extensionUri = Uri.file('whateva');
const quickActionStatus: SObjectQuickActionStatus = {
sobjects: {
account: {
view: false,
edit: false,
create: false
}
}
};

// stubs for OrgUtils.getCompactLayoutFieldsForSObject()
const compactLayoutFields: CompactLayoutField[] = [
{
editableForNew: true,
editableForUpdate: true,
label: 'label',
layoutComponents: [
{
value: 'field1'
}
]
}
];
const compactLayoutFieldsStub = sandbox
.stub(OrgUtils, 'getCompactLayoutFieldsForSObject')
.resolves(compactLayoutFields);
const quickActionStatusStub = sandbox.stub(
LwcGenerationCommand,
'checkForExistingQuickActions'
);
quickActionStatusStub.resolves(quickActionStatus);
const codeBuilderStub = sandbox.stub(CodeBuilder.prototype);

// act
const abc =
await LwcGenerationCommand.generateMissingLwcsAndQuickActions(
extensionUri,
quickActionStatus
);

sandbox.assert.calledOnce(codeBuilderStub.generateCreate);
sandbox.assert.calledOnce(codeBuilderStub.generateEdit);
sandbox.assert.calledOnce(codeBuilderStub.generateView);
});
});

0 comments on commit 7c0fa69

Please sign in to comment.