Skip to content

Commit

Permalink
Prettier. Renamed interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbreese committed Nov 7, 2023
1 parent 62f40d8 commit bf11787
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 29 deletions.
37 changes: 28 additions & 9 deletions src/commands/wizard/lwcGenerationCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Uri, l10n } from 'vscode';
import { InstructionsWebviewProvider } from '../../webviews/instructions';
import * as fs from 'fs';

export interface QuickActionFlags {
export interface SObjectQuickActions {
[name: string]: {
view: boolean;
edit: boolean;
Expand Down Expand Up @@ -37,27 +37,46 @@ export class LwcGenerationCommand {
});
}

static checkForExistingQuickActions(sobjects: string[]): QuickActionFlags {
const results: QuickActionFlags = {};
static checkForExistingQuickActions(
sobjects: string[]
): SObjectQuickActions {
const results: SObjectQuickActions = {};
sobjects.forEach((sobject) => {
results[sobject] = {
view: false,
edit: false,
create: false,
create: false
};
results[sobject].view = LwcGenerationCommand.checkForExistingQuickAction(sobject, "view");
results[sobject].edit = LwcGenerationCommand.checkForExistingQuickAction(sobject, "edit");
results[sobject].create = LwcGenerationCommand.checkForExistingQuickAction(sobject, "create");
results[sobject].view =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'view'
);
results[sobject].edit =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'edit'
);
results[sobject].create =
LwcGenerationCommand.checkForExistingQuickAction(
sobject,
'create'
);
});

return results;
}

private static checkForExistingQuickAction(sobject: string, qaName: string): boolean {
private static checkForExistingQuickAction(
sobject: string,
qaName: string
): boolean {
const expectedDirName = `${sobject}.${qaName}.quickAction-meta.xml`;
try {
// Check if the qa directory exists
const stats = fs.statSync(`force-app/main/default/quickActions/${expectedDirName}`);
const stats = fs.statSync(
`force-app/main/default/quickActions/${expectedDirName}`
);
return stats.isDirectory();
} catch (error) {
// If an error occurs, the directory does not exist
Expand Down
80 changes: 60 additions & 20 deletions src/test/suite/commands/wizard/lwcGenerationCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import * as fs from 'fs';
import { afterEach, beforeEach } from 'mocha';
import { LwcGenerationCommand, QuickActionFlags } from '../../../../commands/wizard/lwcGenerationCommand';

import {
LwcGenerationCommand,
SObjectQuickActions
} from '../../../../commands/wizard/lwcGenerationCommand';

export interface DoSomething {
[name: string]: {
Expand All @@ -19,37 +21,75 @@ export interface DoSomething {
}

suite('LWC Generation Command Test Suite', () => {
beforeEach(function () {
});
beforeEach(function () {});

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

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

// stub the file system responses - any return value is a positive hit, an exception is a negative hit
statSyncStub.withArgs(`${baseDir}/sobject1.view.quickAction-meta.xml`).returns(statsStub);
statSyncStub.withArgs(`${baseDir}/sobject1.edit.quickAction-meta.xml`).returns(statsStub);
statSyncStub.withArgs(`${baseDir}/sobject1.create.quickAction-meta.xml`).returns(statsStub);

statSyncStub.withArgs(`${baseDir}/sobject2.view.quickAction-meta.xml`).throws("error");
statSyncStub.withArgs(`${baseDir}/sobject2.edit.quickAction-meta.xml`).throws("error");
statSyncStub.withArgs(`${baseDir}/sobject2.create.quickAction-meta.xml`).throws("error");
statSyncStub
.withArgs(`${baseDir}/sobject1.view.quickAction-meta.xml`)
.returns(statsStub);
statSyncStub
.withArgs(`${baseDir}/sobject1.edit.quickAction-meta.xml`)
.returns(statsStub);
statSyncStub
.withArgs(`${baseDir}/sobject1.create.quickAction-meta.xml`)
.returns(statsStub);

const result = LwcGenerationCommand.checkForExistingQuickActions(["sobject1", "sobject2"]);
statSyncStub
.withArgs(`${baseDir}/sobject2.view.quickAction-meta.xml`)
.throws('error');
statSyncStub
.withArgs(`${baseDir}/sobject2.edit.quickAction-meta.xml`)
.throws('error');
statSyncStub
.withArgs(`${baseDir}/sobject2.create.quickAction-meta.xml`)
.throws('error');

assert.equal(result["sobject1"].view, true, "sobject1.view should exist");
assert.equal(result["sobject1"].edit, true, "sobject1.edit should exist");
assert.equal(result["sobject1"].create, true, "sobject1.create should exist");
const result: SObjectQuickActions =
LwcGenerationCommand.checkForExistingQuickActions([
'sobject1',
'sobject2'
]);

assert.equal(result["sobject2"].view, false, "sobject2.view should NOT exist");
assert.equal(result["sobject2"].edit, false, "sobject2.edit should NOT exist");
assert.equal(result["sobject2"].create, false, "sobject2.create should NOT exist");
assert.equal(
result['sobject1'].view,
true,
'sobject1.view should exist'
);
assert.equal(
result['sobject1'].edit,
true,
'sobject1.edit should exist'
);
assert.equal(
result['sobject1'].create,
true,
'sobject1.create should exist'
);

assert.equal(
result['sobject2'].view,
false,
'sobject2.view should NOT exist'
);
assert.equal(
result['sobject2'].edit,
false,
'sobject2.edit should NOT exist'
);
assert.equal(
result['sobject2'].create,
false,
'sobject2.create should NOT exist'
);
});
});
});

0 comments on commit bf11787

Please sign in to comment.