Skip to content

Commit

Permalink
@W-14442225 - Checks for a list of sobjects and the corresponding cre…
Browse files Browse the repository at this point in the history
…ate/edit/view qa directories.

May require some tweaking during integration, but this should be a good stepping stone.
  • Loading branch information
dbreese committed Nov 7, 2023
1 parent d3fb7fb commit 62f40d8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/commands/wizard/lwcGenerationCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Uri, l10n } from 'vscode';
import { InstructionsWebviewProvider } from '../../webviews/instructions';
import * as fs from 'fs';

export interface QuickActionFlags {
[name: string]: {
view: boolean;
edit: boolean;
create: boolean;
};
}

export class LwcGenerationCommand {
extensionUri: Uri;
Expand Down Expand Up @@ -27,4 +36,32 @@ export class LwcGenerationCommand {
);
});
}

static checkForExistingQuickActions(sobjects: string[]): QuickActionFlags {
const results: QuickActionFlags = {};
sobjects.forEach((sobject) => {
results[sobject] = {
view: false,
edit: false,
create: false,
};
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 {
const expectedDirName = `${sobject}.${qaName}.quickAction-meta.xml`;
try {
// Check if the qa directory exists
const stats = fs.statSync(`force-app/main/default/quickActions/${expectedDirName}`);
return stats.isDirectory();
} catch (error) {
// If an error occurs, the directory does not exist
return false;
}
}
}
55 changes: 55 additions & 0 deletions src/test/suite/commands/wizard/lwcGenerationCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

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';


export interface DoSomething {
[name: string]: {
view: boolean;
};
}

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

afterEach(function () {
sinon.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);
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");

const result = LwcGenerationCommand.checkForExistingQuickActions(["sobject1", "sobject2"]);

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 62f40d8

Please sign in to comment.