From b3d798a0d25482d09a62489f6fbe2b380ca48e3b Mon Sep 17 00:00:00 2001 From: Dehru Cromer Date: Tue, 21 Jun 2022 12:15:59 -0600 Subject: [PATCH 1/6] fix: allow lwc component rename from inside __tests__ directory --- .../commands/forceRenameLightningComponent.ts | 12 +++++++++- .../forceRenameLightningComponent.test.ts | 22 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts index 28a34e6093..176ca7e3d1 100644 --- a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts +++ b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts @@ -132,9 +132,19 @@ async function renameComponent(sourceFsPath: string, newName: string) { notificationService.showWarningMessage(nls.localize(RENAME_WARNING)); } +export function getParentDirectoryOfTestFolder(sourceFsPath: string): string { + const directories = sourceFsPath.split(path.sep); + directories.splice(directories.length - 1); + return directories.join(path.sep); +} + async function getComponentPath(sourceFsPath: string): Promise { const stats = await fs.promises.stat(sourceFsPath); - return stats.isFile() ? path.dirname(sourceFsPath) : sourceFsPath; + let dirname = stats.isFile() ? path.dirname(sourceFsPath) : sourceFsPath; + if (dirname.endsWith(TEST_FOLDER)) { + dirname = getParentDirectoryOfTestFolder(dirname); + } + return dirname; } function getComponentName(componentPath: string): string { diff --git a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts index c04f4c4ce2..ac48060a90 100644 --- a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts +++ b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts @@ -4,7 +4,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as sinon from 'sinon'; import * as vscode from 'vscode'; -import {inputGuard, isNameMatch, RenameLwcComponentExecutor} from '../../../src/commands/forceRenameLightningComponent'; +import {getParentDirectoryOfTestFolder, inputGuard, isNameMatch, RenameLwcComponentExecutor} from '../../../src/commands/forceRenameLightningComponent'; import { nls } from '../../../src/messages'; const RENAME_INPUT_DUP_ERROR = 'rename_component_input_dup_error'; @@ -399,5 +399,25 @@ describe('Force Rename Lightning Component', () => { expect(exceptionThrownLwc).to.equal(true); expect(exceptionThrownAura).to.equal(true); }); + + }); + + describe('getParentDirectoryOfTestFolder function', () => { + beforeEach(() => { + statStub = env.stub(fs.promises, 'stat').resolves({ + isFile: () => { + return false; + } + }); + }); + afterEach(() => { + env.restore(); + }); + it('should correctly return parent component directory of __tests__ directory', async () => { + const pathWithoutTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent).toString(); + const pathWithTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent, testFolder).toString(); + const result2 = await getParentDirectoryOfTestFolder(pathWithTestDirectory); + expect(result2).to.equal(pathWithoutTestDirectory); + }); }); }); From de82062d83e8875414493afb70e4ccb5830e202c Mon Sep 17 00:00:00 2001 From: Dehru Cromer Date: Tue, 21 Jun 2022 13:35:07 -0600 Subject: [PATCH 2/6] fix: expand when clause, handle __test__ directory in path --- .../salesforcedx-vscode-core/package.json | 2 +- .../commands/forceRenameLightningComponent.ts | 8 +++--- .../forceRenameLightningComponent.test.ts | 25 ++++++++----------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/salesforcedx-vscode-core/package.json b/packages/salesforcedx-vscode-core/package.json index 983f445ebc..21af521596 100644 --- a/packages/salesforcedx-vscode-core/package.json +++ b/packages/salesforcedx-vscode-core/package.json @@ -307,7 +307,7 @@ }, { "command": "sfdx.lightning.rename", - "when": "sfdx:project_opened && resource =~ /.*/(lwc|aura)/[^/]+(/[^/]+\\.(html|css|js|xml|svg|cmp|app|design|auradoc))?$/" + "when": "sfdx:project_opened && resource =~ /.*/(lwc|aura)/.*(/[^/]+\\.(html|css|js|xml|svg|cmp|app|design|auradoc))?$/" } ], "commandPalette": [ diff --git a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts index 176ca7e3d1..9df7d27e76 100644 --- a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts +++ b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts @@ -132,17 +132,17 @@ async function renameComponent(sourceFsPath: string, newName: string) { notificationService.showWarningMessage(nls.localize(RENAME_WARNING)); } -export function getParentDirectoryOfTestFolder(sourceFsPath: string): string { - const directories = sourceFsPath.split(path.sep); +export function getParentDirectoryOfTestFolder(sourceFsPath: string, pathSeparator: string): string { + const directories = sourceFsPath.split(pathSeparator); directories.splice(directories.length - 1); - return directories.join(path.sep); + return directories.join(pathSeparator); } async function getComponentPath(sourceFsPath: string): Promise { const stats = await fs.promises.stat(sourceFsPath); let dirname = stats.isFile() ? path.dirname(sourceFsPath) : sourceFsPath; if (dirname.endsWith(TEST_FOLDER)) { - dirname = getParentDirectoryOfTestFolder(dirname); + dirname = getParentDirectoryOfTestFolder(dirname, path.sep); } return dirname; } diff --git a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts index ac48060a90..1dd1cf4b1b 100644 --- a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts +++ b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts @@ -403,21 +403,18 @@ describe('Force Rename Lightning Component', () => { }); describe('getParentDirectoryOfTestFolder function', () => { - beforeEach(() => { - statStub = env.stub(fs.promises, 'stat').resolves({ - isFile: () => { - return false; - } - }); + it('should return parent component directory of __tests__ directory on windows', async () => { + const windowsPathWithoutTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent).toString().replace(/\//g, '\\'); + const windowsPathWithTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent, testFolder).toString().replace(/\//g, '\\'); + const windowsResult = getParentDirectoryOfTestFolder(windowsPathWithTestDirectory, '\\'); + expect(windowsResult).to.equal(windowsPathWithoutTestDirectory); }); - afterEach(() => { - env.restore(); - }); - it('should correctly return parent component directory of __tests__ directory', async () => { - const pathWithoutTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent).toString(); - const pathWithTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent, testFolder).toString(); - const result2 = await getParentDirectoryOfTestFolder(pathWithTestDirectory); - expect(result2).to.equal(pathWithoutTestDirectory); + + it('should return parent component directory of __tests__ directory on linux', async () => { + const linuxPathWithoutTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent).toString(); + const linuxPathWithTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent, testFolder).toString(); + const linuxResult = getParentDirectoryOfTestFolder(linuxPathWithTestDirectory, '/'); + expect(linuxResult).to.equal(linuxPathWithoutTestDirectory); }); }); }); From 8e10d5a065ee17ae83664da16e1b652f963d3e2e Mon Sep 17 00:00:00 2001 From: Dehru Cromer Date: Tue, 28 Jun 2022 13:56:05 -0600 Subject: [PATCH 3/6] chore: update based on PR comments --- .../commands/forceRenameLightningComponent.ts | 8 ++++---- .../forceRenameLightningComponent.test.ts | 19 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts index 9df7d27e76..176ca7e3d1 100644 --- a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts +++ b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts @@ -132,17 +132,17 @@ async function renameComponent(sourceFsPath: string, newName: string) { notificationService.showWarningMessage(nls.localize(RENAME_WARNING)); } -export function getParentDirectoryOfTestFolder(sourceFsPath: string, pathSeparator: string): string { - const directories = sourceFsPath.split(pathSeparator); +export function getParentDirectoryOfTestFolder(sourceFsPath: string): string { + const directories = sourceFsPath.split(path.sep); directories.splice(directories.length - 1); - return directories.join(pathSeparator); + return directories.join(path.sep); } async function getComponentPath(sourceFsPath: string): Promise { const stats = await fs.promises.stat(sourceFsPath); let dirname = stats.isFile() ? path.dirname(sourceFsPath) : sourceFsPath; if (dirname.endsWith(TEST_FOLDER)) { - dirname = getParentDirectoryOfTestFolder(dirname, path.sep); + dirname = getParentDirectoryOfTestFolder(dirname); } return dirname; } diff --git a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts index 1dd1cf4b1b..d96f5fa934 100644 --- a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts +++ b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts @@ -403,18 +403,15 @@ describe('Force Rename Lightning Component', () => { }); describe('getParentDirectoryOfTestFolder function', () => { - it('should return parent component directory of __tests__ directory on windows', async () => { - const windowsPathWithoutTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent).toString().replace(/\//g, '\\'); - const windowsPathWithTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent, testFolder).toString().replace(/\//g, '\\'); - const windowsResult = getParentDirectoryOfTestFolder(windowsPathWithTestDirectory, '\\'); - expect(windowsResult).to.equal(windowsPathWithoutTestDirectory); - }); + it('validates getParentDirectoryOfTestFolder', () => { + const folders = ['path', 'to', '__tests__']; + const folderPath = folders.join(path.sep); + + const parentFolder = folders.slice(0, -1); + const parentFolderPath = parentFolder.join(path.sep); - it('should return parent component directory of __tests__ directory on linux', async () => { - const linuxPathWithoutTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent).toString(); - const linuxPathWithTestDirectory = vscode.Uri.joinPath(lwcPath, lwcComponent, testFolder).toString(); - const linuxResult = getParentDirectoryOfTestFolder(linuxPathWithTestDirectory, '/'); - expect(linuxResult).to.equal(linuxPathWithoutTestDirectory); + const parentDirectory = getParentDirectoryOfTestFolder(folderPath); + expect(parentDirectory).to.equal(parentFolderPath); }); }); }); From c57416907bd8ad4bbdd3ad2f9148ea10ae30f5b2 Mon Sep 17 00:00:00 2001 From: Dehru Cromer Date: Thu, 30 Jun 2022 14:40:13 -0600 Subject: [PATCH 4/6] feat: refactor how we find the component directory --- .../commands/forceRenameLightningComponent.ts | 23 +- .../src/messages/i18n.ts | 2 + .../forceRenameLightningComponent.test.ts | 785 +++++++++--------- 3 files changed, 430 insertions(+), 380 deletions(-) diff --git a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts index 176ca7e3d1..b25e757471 100644 --- a/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts +++ b/packages/salesforcedx-vscode-core/src/commands/forceRenameLightningComponent.ts @@ -22,6 +22,7 @@ const RENAME_INPUT_PLACEHOLDER = 'rename_component_input_placeholder'; const RENAME_INPUT_PROMPT = 'rename_component_input_prompt'; const RENAME_INPUT_DUP_ERROR = 'rename_component_input_dup_error'; const RENAME_INPUT_DUP_FILE_NAME_ERROR = 'rename_component_input_dup_file_name_error'; +const RENAME_ERROR = 'rename_component_error'; const RENAME_WARNING = 'rename_component_warning'; const LWC = 'lwc'; const AURA = 'aura'; @@ -44,8 +45,14 @@ export class RenameLwcComponentExecutor extends LibraryCommandletExecutor -1) { + directories.splice(lwcDirectoryIndex + 2); + } return directories.join(path.sep); } async function getComponentPath(sourceFsPath: string): Promise { const stats = await fs.promises.stat(sourceFsPath); let dirname = stats.isFile() ? path.dirname(sourceFsPath) : sourceFsPath; - if (dirname.endsWith(TEST_FOLDER)) { - dirname = getParentDirectoryOfTestFolder(dirname); - } + dirname = getLightningComponentDirectory(dirname); return dirname; } diff --git a/packages/salesforcedx-vscode-core/src/messages/i18n.ts b/packages/salesforcedx-vscode-core/src/messages/i18n.ts index 18a71a930c..56560685fb 100644 --- a/packages/salesforcedx-vscode-core/src/messages/i18n.ts +++ b/packages/salesforcedx-vscode-core/src/messages/i18n.ts @@ -639,6 +639,8 @@ export const messages = { 'Press Enter to confirm your input or Escape to cancel', rename_component_warning: 'Warning: References to the old name will not be updated. Update manually and redeploy once all changes have been made.', + rename_component_error: + 'Unable to rename component. Update manually and redeploy once all changes have been made.', error_function_type: 'Unable to determine type of executing function.', error_unable_to_get_started_function: 'Unable to access the function in "{0}".' diff --git a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts index d96f5fa934..5b22fb8569 100644 --- a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts +++ b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts @@ -4,7 +4,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as sinon from 'sinon'; import * as vscode from 'vscode'; -import {getParentDirectoryOfTestFolder, inputGuard, isNameMatch, RenameLwcComponentExecutor} from '../../../src/commands/forceRenameLightningComponent'; +import {getLightningComponentDirectory, inputGuard, isNameMatch, RenameLwcComponentExecutor} from '../../../src/commands/forceRenameLightningComponent'; import { nls } from '../../../src/messages'; const RENAME_INPUT_DUP_ERROR = 'rename_component_input_dup_error'; @@ -24,394 +24,433 @@ let statStub: sinon.SinonStub; let readdirStub: sinon.SinonStub; describe('Force Rename Lightning Component', () => { - describe('Happy Path Unit Test', () => { - beforeEach(() => { - renameStub = env.stub(fs.promises, 'rename').resolves(undefined); - statStub = env.stub(fs.promises, 'stat').resolves({ - isFile: () => { - return false; - } - }); - readdirStub = env.stub(fs.promises, 'readdir'); - }); - - afterEach(() => { - env.restore(); - }); - - it('should rename the files and folder with new name under the same path', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves([itemsInHero[0]]); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'hero1'} - }); - const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); - const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); - const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); - expect(renameStub.callCount).to.equal(2); - expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); - expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); - }); - - it('should only rename the files and folder that have same name with LWC component', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves(itemsInHero); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'hero1'} - }); - expect(renameStub.callCount).to.equal(5); - }); - - it('should only rename the files and folder that have same name with Aura component', async () => { - const sourceUri = vscode.Uri.joinPath(auraPath, auraComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves(itemsInPage); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'page1'} - }); - expect(renameStub.callCount).to.equal(10); - }); - - it('should rename the test file that has the same name as component', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onCall(0).resolves([]) - .onCall(1).resolves([]) - .onCall(2).resolves([testFolder]) - .onCall(3).resolves([]) - .onCall(4).resolves(testFiles); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'hero1'} - }); - const testFolderPath = path.join(sourceUri.fsPath, testFolder); - const oldFilePath = path.join(testFolderPath, 'hero.test.js'); - const newFilePath = path.join(testFolderPath, 'hero1.test.js'); - const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); - expect(renameStub.callCount).to.equal(2); - expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); - expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); - }); - - it('should show the warning message once rename is done', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves([itemsInHero[1]]); - const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'hero1'} - }); - expect(showWarningMessageSpy.callCount).to.equal(1); - }); - }); - - describe('Exception and corner cases handling', () => { - beforeEach(() => { - renameStub = env.stub(fs.promises, 'rename').resolves(undefined); - statStub = env.stub(fs.promises, 'stat').resolves({ - isFile: () => { - return false; - } - }); - readdirStub = env.stub(fs.promises, 'readdir'); - }); - - afterEach(() => { - env.restore(); - }); - - it('should get trimmed component name if new component input has leading or trailing spaces', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves([itemsInHero[0]]); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: ' hero1 '} - }); - const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); - const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); - expect(renameStub.callCount).to.equal(2); - expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); - }); - - it('should not rename when input text only contains white spaces', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves([itemsInHero[0]]); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: ' '} - }); - expect(renameStub.callCount).to.equal(0); - }); - - it('should not rename when input text is empty', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves([itemsInHero[0]]); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {} - }); - expect(renameStub.callCount).to.equal(0); - }); - - it('should not show warning message when input text is empty', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([]) - .onSecondCall().resolves([]) - .onThirdCall().resolves([itemsInHero[0]]); - const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {} - }); - expect(showWarningMessageSpy.callCount).to.equal(0); - }); - - it('should enforce unique component name under LWC and Aura and show error message for duplicate name', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onFirstCall().resolves([lwcComponent]) - .onSecondCall().resolves([]); - let exceptionThrown: any; - const errorMessage = nls.localize(RENAME_INPUT_DUP_ERROR); - try { - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'hero'} - }); - } catch (e) { - exceptionThrown = e; - } - expect(exceptionThrown.message).to.equal(errorMessage); - expect(renameStub.callCount).to.equal(0); - }); - - it('should prevent new component name from duplicating any existing file name under current component directory', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onCall(0).resolves([]) - .onCall(1).resolves([]) - .onCall(2).resolves(itemsInHero.concat([testFolder])) - .onCall(3).resolves(testFiles); - let exceptionThrown: any; - const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); - try { - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'templateOne'} - }); - } catch (e) { - exceptionThrown = e; - } - expect(exceptionThrown.message).to.equal(errorMessage); - expect(renameStub.callCount).to.equal(0); - }); - - it('should prevent new component name from duplicating any exiting test file name', async () => { - const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - readdirStub - .onCall(0).resolves([]) - .onCall(1).resolves([]) - .onCall(2).resolves(itemsInHero.concat([testFolder])) - .onCall(3).resolves(testFiles); - let exceptionThrown: any; - const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); - try { - const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - await executor.run({ - type: 'CONTINUE', - data: {name: 'example'} - }); - } catch (e) { - exceptionThrown = e; - } - expect(exceptionThrown.message).to.equal(errorMessage); - expect(renameStub.callCount).to.equal(0); - }); - }); - - describe ('#isNameMatch', () => { - it('should return true if file name and component name match for essential LWC files', () => { - const componentName = 'hero'; - const componentPath = path.join(lwcPath.fsPath, lwcComponent); - expect(isNameMatch(itemsInHero[0], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInHero[1], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInHero[2], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInHero[3], componentName, componentPath)).to.equal(true); - }); - - it('should return true of file name and component name match for essential Aura files', () => { - const componentName = 'page'; - const componentPath = path.join(auraPath.fsPath, auraComponent); - expect(isNameMatch(itemsInPage[0], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[1], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[2], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[3], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[4], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[5], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[6], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[7], componentName, componentPath)).to.equal(true); - expect(isNameMatch(itemsInPage[8], componentName, componentPath)).to.equal(true); - }); - - it('should return false if file type is not in LWC or Aura or file name and component name do not match', () => { - const lwcComponentPath = path.join(lwcPath.fsPath, lwcComponent); - const auraComponentPath = path.join(auraPath.fsPath, auraComponent); - expect(isNameMatch('hero.jpg', 'hero', lwcComponentPath)).to.equal(false); - expect(isNameMatch('hero1.css', 'hero', lwcComponentPath)).to.equal(false); - expect(isNameMatch('page.jpg', 'page', auraComponentPath)).to.equal(false); - expect(isNameMatch('page1.css', 'hero', auraComponentPath)).to.equal(false); - }); - }); - - describe('Guard new component name', () => { - beforeEach(() => { - statStub = env.stub(fs.promises, 'stat').resolves({ - isFile: () => { - return false; - } - }); + // describe('Happy Path Unit Test', () => { + // beforeEach(() => { + // renameStub = env.stub(fs.promises, 'rename').resolves(undefined); + // statStub = env.stub(fs.promises, 'stat').resolves({ + // isFile: () => { + // return false; + // } + // }); + // readdirStub = env.stub(fs.promises, 'readdir'); + // }); + + // afterEach(() => { + // env.restore(); + // }); + + // it('should rename the files and folder with new name under the same path', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves([itemsInHero[0]]); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'hero1'} + // }); + // const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); + // const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); + // const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); + // expect(renameStub.callCount).to.equal(2); + // expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); + // expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); + // }); + + // it('should only rename the files and folder that have same name with LWC component', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves(itemsInHero); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'hero1'} + // }); + // expect(renameStub.callCount).to.equal(5); + // }); + + // it('should only rename the files and folder that have same name with Aura component', async () => { + // const sourceUri = vscode.Uri.joinPath(auraPath, auraComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves(itemsInPage); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'page1'} + // }); + // expect(renameStub.callCount).to.equal(10); + // }); + + // it('should rename the test file that has the same name as component', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onCall(0).resolves([]) + // .onCall(1).resolves([]) + // .onCall(2).resolves([testFolder]) + // .onCall(3).resolves([]) + // .onCall(4).resolves(testFiles); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'hero1'} + // }); + // const testFolderPath = path.join(sourceUri.fsPath, testFolder); + // const oldFilePath = path.join(testFolderPath, 'hero.test.js'); + // const newFilePath = path.join(testFolderPath, 'hero1.test.js'); + // const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); + // expect(renameStub.callCount).to.equal(2); + // expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); + // expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); + // }); + + // it('should show the warning message once rename is done', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves([itemsInHero[1]]); + // const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'hero1'} + // }); + // expect(showWarningMessageSpy.callCount).to.equal(1); + // }); + // }); + + // describe('Exception and corner cases handling', () => { + // beforeEach(() => { + // renameStub = env.stub(fs.promises, 'rename').resolves(undefined); + // statStub = env.stub(fs.promises, 'stat').resolves({ + // isFile: () => { + // return false; + // } + // }); + // readdirStub = env.stub(fs.promises, 'readdir'); + // }); + + // afterEach(() => { + // env.restore(); + // }); + + // it('should get trimmed component name if new component input has leading or trailing spaces', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves([itemsInHero[0]]); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: ' hero1 '} + // }); + // const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); + // const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); + // expect(renameStub.callCount).to.equal(2); + // expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); + // }); + + // it('should not rename when input text only contains white spaces', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves([itemsInHero[0]]); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: ' '} + // }); + // expect(renameStub.callCount).to.equal(0); + // }); + + // it('should not rename when input text is empty', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves([itemsInHero[0]]); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {} + // }); + // expect(renameStub.callCount).to.equal(0); + // }); + + // it('should not show warning message when input text is empty', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([]) + // .onSecondCall().resolves([]) + // .onThirdCall().resolves([itemsInHero[0]]); + // const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {} + // }); + // expect(showWarningMessageSpy.callCount).to.equal(0); + // }); + + // it('should enforce unique component name under LWC and Aura and show error message for duplicate name', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onFirstCall().resolves([lwcComponent]) + // .onSecondCall().resolves([]); + // let exceptionThrown: any; + // const errorMessage = nls.localize(RENAME_INPUT_DUP_ERROR); + // try { + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'hero'} + // }); + // } catch (e) { + // exceptionThrown = e; + // } + // expect(exceptionThrown.message).to.equal(errorMessage); + // expect(renameStub.callCount).to.equal(0); + // }); + + // it('should prevent new component name from duplicating any existing file name under current component directory', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onCall(0).resolves([]) + // .onCall(1).resolves([]) + // .onCall(2).resolves(itemsInHero.concat([testFolder])) + // .onCall(3).resolves(testFiles); + // let exceptionThrown: any; + // const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); + // try { + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'templateOne'} + // }); + // } catch (e) { + // exceptionThrown = e; + // } + // expect(exceptionThrown.message).to.equal(errorMessage); + // expect(renameStub.callCount).to.equal(0); + // }); + + // it('should prevent new component name from duplicating any exiting test file name', async () => { + // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + // readdirStub + // .onCall(0).resolves([]) + // .onCall(1).resolves([]) + // .onCall(2).resolves(itemsInHero.concat([testFolder])) + // .onCall(3).resolves(testFiles); + // let exceptionThrown: any; + // const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); + // try { + // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + // await executor.run({ + // type: 'CONTINUE', + // data: {name: 'example'} + // }); + // } catch (e) { + // exceptionThrown = e; + // } + // expect(exceptionThrown.message).to.equal(errorMessage); + // expect(renameStub.callCount).to.equal(0); + // }); + // }); + + // describe ('#isNameMatch', () => { + // it('should return true if file name and component name match for essential LWC files', () => { + // const componentName = 'hero'; + // const componentPath = path.join(lwcPath.fsPath, lwcComponent); + // expect(isNameMatch(itemsInHero[0], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInHero[1], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInHero[2], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInHero[3], componentName, componentPath)).to.equal(true); + // }); + + // it('should return true of file name and component name match for essential Aura files', () => { + // const componentName = 'page'; + // const componentPath = path.join(auraPath.fsPath, auraComponent); + // expect(isNameMatch(itemsInPage[0], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[1], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[2], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[3], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[4], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[5], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[6], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[7], componentName, componentPath)).to.equal(true); + // expect(isNameMatch(itemsInPage[8], componentName, componentPath)).to.equal(true); + // }); + + // it('should return false if file type is not in LWC or Aura or file name and component name do not match', () => { + // const lwcComponentPath = path.join(lwcPath.fsPath, lwcComponent); + // const auraComponentPath = path.join(auraPath.fsPath, auraComponent); + // expect(isNameMatch('hero.jpg', 'hero', lwcComponentPath)).to.equal(false); + // expect(isNameMatch('hero1.css', 'hero', lwcComponentPath)).to.equal(false); + // expect(isNameMatch('page.jpg', 'page', auraComponentPath)).to.equal(false); + // expect(isNameMatch('page1.css', 'hero', auraComponentPath)).to.equal(false); + // }); + // }); + + // describe('Guard new component name', () => { + // beforeEach(() => { + // statStub = env.stub(fs.promises, 'stat').resolves({ + // isFile: () => { + // return false; + // } + // }); + // }); + + // afterEach(() => { + // env.restore(); + // }); + + // it('should not show the error message when new component name starts with a letter', async () => { + // let exceptionThrownLwc = false; + // let exceptionThrownAura = false; + // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + // try { + // await inputGuard(sourceUriLWC.fsPath, 'Hello'); + // } catch (e) { + // exceptionThrownLwc = true; + // } + // try { + // await inputGuard(sourceUriAura.fsPath, 'Hello'); + // } catch (e) { + // exceptionThrownAura = true; + // } + // expect(exceptionThrownLwc).to.equal(false); + // expect(exceptionThrownAura).to.equal(false); + // }); + + // it('should change the first letter to lower case if the new LWC component name is a upper-case letter', async () => { + // let returnedName: any; + // let exceptionThrownLwc = false; + // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + // try { + // returnedName = await inputGuard(sourceUriLWC.fsPath, 'Hello'); + // } catch (e) { + // exceptionThrownLwc = true; + // } + // expect(returnedName).to.equal('hello'); + // expect(exceptionThrownLwc).to.equal(false); + // }); + + // it('should show the error message when component name contains special characters other than underscore or alphanumeric for LWC and Aura', async () => { + // let exceptionThrownLwc = false; + // let exceptionThrownAura = false; + // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + // try { + // await inputGuard(sourceUriLWC.fsPath, 'hello%$world'); + // } catch (e) { + // exceptionThrownLwc = true; + // } + // try { + // await inputGuard(sourceUriAura.fsPath, 'hello%$world'); + // } catch (e) { + // exceptionThrownAura = true; + // } + // expect(exceptionThrownLwc).to.equal(true); + // expect(exceptionThrownAura).to.equal(true); + // }); + + // it('should show the error message when component name contains two consecutive underscores for LWC and Aura', async () => { + // let exceptionThrownLwc = false; + // let exceptionThrownAura = false; + // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + // try { + // await inputGuard(sourceUriLWC.fsPath, 'hello__world'); + // } catch (e) { + // exceptionThrownLwc = true; + // } + // try { + // await inputGuard(sourceUriAura.fsPath, 'hello__world'); + // } catch (e) { + // exceptionThrownAura = true; + // } + // expect(exceptionThrownLwc).to.equal(true); + // expect(exceptionThrownAura).to.equal(true); + // }); + + // it('should show the error message when component name ends with an underscore for LWC and Aura', async () => { + // let exceptionThrownLwc = false; + // let exceptionThrownAura = false; + // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + // try { + // await inputGuard(sourceUriLWC.fsPath, 'hello_'); + // } catch (e) { + // exceptionThrownLwc = true; + // } + // try { + // await inputGuard(sourceUriAura.fsPath, 'hello_'); + // } catch (e) { + // exceptionThrownAura = true; + // } + // expect(exceptionThrownLwc).to.equal(true); + // expect(exceptionThrownAura).to.equal(true); + // }); + + // }); + + describe('getLightningComponentDirectory function', () => { + + it('works with simple component folder', () => { + const folders = ['src', 'main', 'default', 'lwc', 'cmp']; + const folderPath = folders.join(path.sep); + const parentDirectory = getLightningComponentDirectory(folderPath); + expect(parentDirectory).to.equal(folderPath); }); - afterEach(() => { - env.restore(); - }); + it('works with __tests__ folder in the path', () => { + const folders = ['src', 'main', 'default', 'lwc', 'cmp', '__tests__']; + const folderPath = folders.join(path.sep); - it('should not show the error message when new component name starts with a letter', async () => { - let exceptionThrownLwc = false; - let exceptionThrownAura = false; - const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - try { - await inputGuard(sourceUriLWC.fsPath, 'Hello'); - } catch (e) { - exceptionThrownLwc = true; - } - try { - await inputGuard(sourceUriAura.fsPath, 'Hello'); - } catch (e) { - exceptionThrownAura = true; - } - expect(exceptionThrownLwc).to.equal(false); - expect(exceptionThrownAura).to.equal(false); - }); + const parentFolder = folders.slice(0, -1); + const parentFolderPath = parentFolder.join(path.sep); - it('should change the first letter to lower case if the new LWC component name is a upper-case letter', async () => { - let returnedName: any; - let exceptionThrownLwc = false; - const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - try { - returnedName = await inputGuard(sourceUriLWC.fsPath, 'Hello'); - } catch (e) { - exceptionThrownLwc = true; - } - expect(returnedName).to.equal('hello'); - expect(exceptionThrownLwc).to.equal(false); + const parentDirectory = getLightningComponentDirectory(folderPath); + expect(parentDirectory).to.equal(parentFolderPath); }); - it('should show the error message when component name contains special characters other than underscore or alphanumeric for LWC and Aura', async () => { - let exceptionThrownLwc = false; - let exceptionThrownAura = false; - const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - try { - await inputGuard(sourceUriLWC.fsPath, 'hello%$world'); - } catch (e) { - exceptionThrownLwc = true; - } - try { - await inputGuard(sourceUriAura.fsPath, 'hello%$world'); - } catch (e) { - exceptionThrownAura = true; - } - expect(exceptionThrownLwc).to.equal(true); - expect(exceptionThrownAura).to.equal(true); - }); + it('works with child folder of __tests__ folder', () => { + const folders = ['lwc', 'cmp', '__tests__', 'data']; + const folderPath = folders.join(path.sep); - it('should show the error message when component name contains two consecutive underscores for LWC and Aura', async () => { - let exceptionThrownLwc = false; - let exceptionThrownAura = false; - const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - try { - await inputGuard(sourceUriLWC.fsPath, 'hello__world'); - } catch (e) { - exceptionThrownLwc = true; - } - try { - await inputGuard(sourceUriAura.fsPath, 'hello__world'); - } catch (e) { - exceptionThrownAura = true; - } - expect(exceptionThrownLwc).to.equal(true); - expect(exceptionThrownAura).to.equal(true); - }); + const parentFolder = folders.slice(0, -2); + const parentFolderPath = parentFolder.join(path.sep); - it('should show the error message when component name ends with an underscore for LWC and Aura', async () => { - let exceptionThrownLwc = false; - let exceptionThrownAura = false; - const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - try { - await inputGuard(sourceUriLWC.fsPath, 'hello_'); - } catch (e) { - exceptionThrownLwc = true; - } - try { - await inputGuard(sourceUriAura.fsPath, 'hello_'); - } catch (e) { - exceptionThrownAura = true; - } - expect(exceptionThrownLwc).to.equal(true); - expect(exceptionThrownAura).to.equal(true); + const parentDirectory = getLightningComponentDirectory(folderPath); + expect(parentDirectory).to.equal(parentFolderPath); }); - }); - - describe('getParentDirectoryOfTestFolder function', () => { - it('validates getParentDirectoryOfTestFolder', () => { - const folders = ['path', 'to', '__tests__']; + it('works with templates folder of component', () => { + const folders = ['lwc', 'cmp', 'templates']; const folderPath = folders.join(path.sep); const parentFolder = folders.slice(0, -1); const parentFolderPath = parentFolder.join(path.sep); - const parentDirectory = getParentDirectoryOfTestFolder(folderPath); + const parentDirectory = getLightningComponentDirectory(folderPath); expect(parentDirectory).to.equal(parentFolderPath); }); + + it('works with nested lwc folder of component', () => { + const folders = ['src', 'main', 'default', 'lwc', 'other', 'lwc', 'cmp']; + const folderPath = folders.join(path.sep); + + const parentDirectory = getLightningComponentDirectory(folderPath); + expect(parentDirectory).to.equal(folderPath); + }); + }); }); From bd7a94a83c7e55b2719226c76233c3c7399fbd6c Mon Sep 17 00:00:00 2001 From: Dehru Cromer Date: Thu, 30 Jun 2022 15:22:39 -0600 Subject: [PATCH 5/6] chore: renable tests --- .../forceRenameLightningComponent.test.ts | 754 +++++++++--------- 1 file changed, 377 insertions(+), 377 deletions(-) diff --git a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts index 5b22fb8569..c1b1503edb 100644 --- a/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts +++ b/packages/salesforcedx-vscode-core/test/vscode-integration/commands/forceRenameLightningComponent.test.ts @@ -24,383 +24,383 @@ let statStub: sinon.SinonStub; let readdirStub: sinon.SinonStub; describe('Force Rename Lightning Component', () => { - // describe('Happy Path Unit Test', () => { - // beforeEach(() => { - // renameStub = env.stub(fs.promises, 'rename').resolves(undefined); - // statStub = env.stub(fs.promises, 'stat').resolves({ - // isFile: () => { - // return false; - // } - // }); - // readdirStub = env.stub(fs.promises, 'readdir'); - // }); - - // afterEach(() => { - // env.restore(); - // }); - - // it('should rename the files and folder with new name under the same path', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves([itemsInHero[0]]); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'hero1'} - // }); - // const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); - // const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); - // const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); - // expect(renameStub.callCount).to.equal(2); - // expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); - // expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); - // }); - - // it('should only rename the files and folder that have same name with LWC component', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves(itemsInHero); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'hero1'} - // }); - // expect(renameStub.callCount).to.equal(5); - // }); - - // it('should only rename the files and folder that have same name with Aura component', async () => { - // const sourceUri = vscode.Uri.joinPath(auraPath, auraComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves(itemsInPage); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'page1'} - // }); - // expect(renameStub.callCount).to.equal(10); - // }); - - // it('should rename the test file that has the same name as component', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onCall(0).resolves([]) - // .onCall(1).resolves([]) - // .onCall(2).resolves([testFolder]) - // .onCall(3).resolves([]) - // .onCall(4).resolves(testFiles); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'hero1'} - // }); - // const testFolderPath = path.join(sourceUri.fsPath, testFolder); - // const oldFilePath = path.join(testFolderPath, 'hero.test.js'); - // const newFilePath = path.join(testFolderPath, 'hero1.test.js'); - // const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); - // expect(renameStub.callCount).to.equal(2); - // expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); - // expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); - // }); - - // it('should show the warning message once rename is done', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves([itemsInHero[1]]); - // const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'hero1'} - // }); - // expect(showWarningMessageSpy.callCount).to.equal(1); - // }); - // }); - - // describe('Exception and corner cases handling', () => { - // beforeEach(() => { - // renameStub = env.stub(fs.promises, 'rename').resolves(undefined); - // statStub = env.stub(fs.promises, 'stat').resolves({ - // isFile: () => { - // return false; - // } - // }); - // readdirStub = env.stub(fs.promises, 'readdir'); - // }); - - // afterEach(() => { - // env.restore(); - // }); - - // it('should get trimmed component name if new component input has leading or trailing spaces', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves([itemsInHero[0]]); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: ' hero1 '} - // }); - // const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); - // const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); - // expect(renameStub.callCount).to.equal(2); - // expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); - // }); - - // it('should not rename when input text only contains white spaces', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves([itemsInHero[0]]); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: ' '} - // }); - // expect(renameStub.callCount).to.equal(0); - // }); - - // it('should not rename when input text is empty', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves([itemsInHero[0]]); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {} - // }); - // expect(renameStub.callCount).to.equal(0); - // }); - - // it('should not show warning message when input text is empty', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([]) - // .onSecondCall().resolves([]) - // .onThirdCall().resolves([itemsInHero[0]]); - // const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {} - // }); - // expect(showWarningMessageSpy.callCount).to.equal(0); - // }); - - // it('should enforce unique component name under LWC and Aura and show error message for duplicate name', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onFirstCall().resolves([lwcComponent]) - // .onSecondCall().resolves([]); - // let exceptionThrown: any; - // const errorMessage = nls.localize(RENAME_INPUT_DUP_ERROR); - // try { - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'hero'} - // }); - // } catch (e) { - // exceptionThrown = e; - // } - // expect(exceptionThrown.message).to.equal(errorMessage); - // expect(renameStub.callCount).to.equal(0); - // }); - - // it('should prevent new component name from duplicating any existing file name under current component directory', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onCall(0).resolves([]) - // .onCall(1).resolves([]) - // .onCall(2).resolves(itemsInHero.concat([testFolder])) - // .onCall(3).resolves(testFiles); - // let exceptionThrown: any; - // const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); - // try { - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'templateOne'} - // }); - // } catch (e) { - // exceptionThrown = e; - // } - // expect(exceptionThrown.message).to.equal(errorMessage); - // expect(renameStub.callCount).to.equal(0); - // }); - - // it('should prevent new component name from duplicating any exiting test file name', async () => { - // const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); - // readdirStub - // .onCall(0).resolves([]) - // .onCall(1).resolves([]) - // .onCall(2).resolves(itemsInHero.concat([testFolder])) - // .onCall(3).resolves(testFiles); - // let exceptionThrown: any; - // const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); - // try { - // const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); - // await executor.run({ - // type: 'CONTINUE', - // data: {name: 'example'} - // }); - // } catch (e) { - // exceptionThrown = e; - // } - // expect(exceptionThrown.message).to.equal(errorMessage); - // expect(renameStub.callCount).to.equal(0); - // }); - // }); - - // describe ('#isNameMatch', () => { - // it('should return true if file name and component name match for essential LWC files', () => { - // const componentName = 'hero'; - // const componentPath = path.join(lwcPath.fsPath, lwcComponent); - // expect(isNameMatch(itemsInHero[0], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInHero[1], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInHero[2], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInHero[3], componentName, componentPath)).to.equal(true); - // }); - - // it('should return true of file name and component name match for essential Aura files', () => { - // const componentName = 'page'; - // const componentPath = path.join(auraPath.fsPath, auraComponent); - // expect(isNameMatch(itemsInPage[0], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[1], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[2], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[3], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[4], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[5], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[6], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[7], componentName, componentPath)).to.equal(true); - // expect(isNameMatch(itemsInPage[8], componentName, componentPath)).to.equal(true); - // }); - - // it('should return false if file type is not in LWC or Aura or file name and component name do not match', () => { - // const lwcComponentPath = path.join(lwcPath.fsPath, lwcComponent); - // const auraComponentPath = path.join(auraPath.fsPath, auraComponent); - // expect(isNameMatch('hero.jpg', 'hero', lwcComponentPath)).to.equal(false); - // expect(isNameMatch('hero1.css', 'hero', lwcComponentPath)).to.equal(false); - // expect(isNameMatch('page.jpg', 'page', auraComponentPath)).to.equal(false); - // expect(isNameMatch('page1.css', 'hero', auraComponentPath)).to.equal(false); - // }); - // }); - - // describe('Guard new component name', () => { - // beforeEach(() => { - // statStub = env.stub(fs.promises, 'stat').resolves({ - // isFile: () => { - // return false; - // } - // }); - // }); - - // afterEach(() => { - // env.restore(); - // }); - - // it('should not show the error message when new component name starts with a letter', async () => { - // let exceptionThrownLwc = false; - // let exceptionThrownAura = false; - // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - // try { - // await inputGuard(sourceUriLWC.fsPath, 'Hello'); - // } catch (e) { - // exceptionThrownLwc = true; - // } - // try { - // await inputGuard(sourceUriAura.fsPath, 'Hello'); - // } catch (e) { - // exceptionThrownAura = true; - // } - // expect(exceptionThrownLwc).to.equal(false); - // expect(exceptionThrownAura).to.equal(false); - // }); - - // it('should change the first letter to lower case if the new LWC component name is a upper-case letter', async () => { - // let returnedName: any; - // let exceptionThrownLwc = false; - // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - // try { - // returnedName = await inputGuard(sourceUriLWC.fsPath, 'Hello'); - // } catch (e) { - // exceptionThrownLwc = true; - // } - // expect(returnedName).to.equal('hello'); - // expect(exceptionThrownLwc).to.equal(false); - // }); - - // it('should show the error message when component name contains special characters other than underscore or alphanumeric for LWC and Aura', async () => { - // let exceptionThrownLwc = false; - // let exceptionThrownAura = false; - // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - // try { - // await inputGuard(sourceUriLWC.fsPath, 'hello%$world'); - // } catch (e) { - // exceptionThrownLwc = true; - // } - // try { - // await inputGuard(sourceUriAura.fsPath, 'hello%$world'); - // } catch (e) { - // exceptionThrownAura = true; - // } - // expect(exceptionThrownLwc).to.equal(true); - // expect(exceptionThrownAura).to.equal(true); - // }); - - // it('should show the error message when component name contains two consecutive underscores for LWC and Aura', async () => { - // let exceptionThrownLwc = false; - // let exceptionThrownAura = false; - // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - // try { - // await inputGuard(sourceUriLWC.fsPath, 'hello__world'); - // } catch (e) { - // exceptionThrownLwc = true; - // } - // try { - // await inputGuard(sourceUriAura.fsPath, 'hello__world'); - // } catch (e) { - // exceptionThrownAura = true; - // } - // expect(exceptionThrownLwc).to.equal(true); - // expect(exceptionThrownAura).to.equal(true); - // }); - - // it('should show the error message when component name ends with an underscore for LWC and Aura', async () => { - // let exceptionThrownLwc = false; - // let exceptionThrownAura = false; - // const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); - // const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); - // try { - // await inputGuard(sourceUriLWC.fsPath, 'hello_'); - // } catch (e) { - // exceptionThrownLwc = true; - // } - // try { - // await inputGuard(sourceUriAura.fsPath, 'hello_'); - // } catch (e) { - // exceptionThrownAura = true; - // } - // expect(exceptionThrownLwc).to.equal(true); - // expect(exceptionThrownAura).to.equal(true); - // }); - - // }); + describe('Happy Path Unit Test', () => { + beforeEach(() => { + renameStub = env.stub(fs.promises, 'rename').resolves(undefined); + statStub = env.stub(fs.promises, 'stat').resolves({ + isFile: () => { + return false; + } + }); + readdirStub = env.stub(fs.promises, 'readdir'); + }); + + afterEach(() => { + env.restore(); + }); + + it('should rename the files and folder with new name under the same path', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves([itemsInHero[0]]); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'hero1'} + }); + const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); + const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); + const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); + expect(renameStub.callCount).to.equal(2); + expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); + expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); + }); + + it('should only rename the files and folder that have same name with LWC component', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves(itemsInHero); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'hero1'} + }); + expect(renameStub.callCount).to.equal(5); + }); + + it('should only rename the files and folder that have same name with Aura component', async () => { + const sourceUri = vscode.Uri.joinPath(auraPath, auraComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves(itemsInPage); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'page1'} + }); + expect(renameStub.callCount).to.equal(10); + }); + + it('should rename the test file that has the same name as component', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onCall(0).resolves([]) + .onCall(1).resolves([]) + .onCall(2).resolves([testFolder]) + .onCall(3).resolves([]) + .onCall(4).resolves(testFiles); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'hero1'} + }); + const testFolderPath = path.join(sourceUri.fsPath, testFolder); + const oldFilePath = path.join(testFolderPath, 'hero.test.js'); + const newFilePath = path.join(testFolderPath, 'hero1.test.js'); + const newFolderPath = path.join(lwcPath.fsPath, 'hero1'); + expect(renameStub.callCount).to.equal(2); + expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); + expect(renameStub.calledWith(sourceUri.fsPath, newFolderPath)).to.equal(true); + }); + + it('should show the warning message once rename is done', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves([itemsInHero[1]]); + const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'hero1'} + }); + expect(showWarningMessageSpy.callCount).to.equal(1); + }); + }); + + describe('Exception and corner cases handling', () => { + beforeEach(() => { + renameStub = env.stub(fs.promises, 'rename').resolves(undefined); + statStub = env.stub(fs.promises, 'stat').resolves({ + isFile: () => { + return false; + } + }); + readdirStub = env.stub(fs.promises, 'readdir'); + }); + + afterEach(() => { + env.restore(); + }); + + it('should get trimmed component name if new component input has leading or trailing spaces', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves([itemsInHero[0]]); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: ' hero1 '} + }); + const oldFilePath = path.join(sourceUri.fsPath, 'hero.css'); + const newFilePath = path.join(sourceUri.fsPath, 'hero1.css'); + expect(renameStub.callCount).to.equal(2); + expect(renameStub.calledWith(oldFilePath, newFilePath)).to.equal(true); + }); + + it('should not rename when input text only contains white spaces', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves([itemsInHero[0]]); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: ' '} + }); + expect(renameStub.callCount).to.equal(0); + }); + + it('should not rename when input text is empty', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves([itemsInHero[0]]); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {} + }); + expect(renameStub.callCount).to.equal(0); + }); + + it('should not show warning message when input text is empty', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([]) + .onSecondCall().resolves([]) + .onThirdCall().resolves([itemsInHero[0]]); + const showWarningMessageSpy = env.spy(notificationService, 'showWarningMessage'); + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {} + }); + expect(showWarningMessageSpy.callCount).to.equal(0); + }); + + it('should enforce unique component name under LWC and Aura and show error message for duplicate name', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onFirstCall().resolves([lwcComponent]) + .onSecondCall().resolves([]); + let exceptionThrown: any; + const errorMessage = nls.localize(RENAME_INPUT_DUP_ERROR); + try { + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'hero'} + }); + } catch (e) { + exceptionThrown = e; + } + expect(exceptionThrown.message).to.equal(errorMessage); + expect(renameStub.callCount).to.equal(0); + }); + + it('should prevent new component name from duplicating any existing file name under current component directory', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onCall(0).resolves([]) + .onCall(1).resolves([]) + .onCall(2).resolves(itemsInHero.concat([testFolder])) + .onCall(3).resolves(testFiles); + let exceptionThrown: any; + const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); + try { + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'templateOne'} + }); + } catch (e) { + exceptionThrown = e; + } + expect(exceptionThrown.message).to.equal(errorMessage); + expect(renameStub.callCount).to.equal(0); + }); + + it('should prevent new component name from duplicating any exiting test file name', async () => { + const sourceUri = vscode.Uri.joinPath(lwcPath, lwcComponent); + readdirStub + .onCall(0).resolves([]) + .onCall(1).resolves([]) + .onCall(2).resolves(itemsInHero.concat([testFolder])) + .onCall(3).resolves(testFiles); + let exceptionThrown: any; + const errorMessage = nls.localize(RENAME_INPUT_DUP_FILE_NAME_ERROR); + try { + const executor = new RenameLwcComponentExecutor(sourceUri.fsPath); + await executor.run({ + type: 'CONTINUE', + data: {name: 'example'} + }); + } catch (e) { + exceptionThrown = e; + } + expect(exceptionThrown.message).to.equal(errorMessage); + expect(renameStub.callCount).to.equal(0); + }); + }); + + describe ('#isNameMatch', () => { + it('should return true if file name and component name match for essential LWC files', () => { + const componentName = 'hero'; + const componentPath = path.join(lwcPath.fsPath, lwcComponent); + expect(isNameMatch(itemsInHero[0], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInHero[1], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInHero[2], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInHero[3], componentName, componentPath)).to.equal(true); + }); + + it('should return true of file name and component name match for essential Aura files', () => { + const componentName = 'page'; + const componentPath = path.join(auraPath.fsPath, auraComponent); + expect(isNameMatch(itemsInPage[0], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[1], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[2], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[3], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[4], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[5], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[6], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[7], componentName, componentPath)).to.equal(true); + expect(isNameMatch(itemsInPage[8], componentName, componentPath)).to.equal(true); + }); + + it('should return false if file type is not in LWC or Aura or file name and component name do not match', () => { + const lwcComponentPath = path.join(lwcPath.fsPath, lwcComponent); + const auraComponentPath = path.join(auraPath.fsPath, auraComponent); + expect(isNameMatch('hero.jpg', 'hero', lwcComponentPath)).to.equal(false); + expect(isNameMatch('hero1.css', 'hero', lwcComponentPath)).to.equal(false); + expect(isNameMatch('page.jpg', 'page', auraComponentPath)).to.equal(false); + expect(isNameMatch('page1.css', 'hero', auraComponentPath)).to.equal(false); + }); + }); + + describe('Guard new component name', () => { + beforeEach(() => { + statStub = env.stub(fs.promises, 'stat').resolves({ + isFile: () => { + return false; + } + }); + }); + + afterEach(() => { + env.restore(); + }); + + it('should not show the error message when new component name starts with a letter', async () => { + let exceptionThrownLwc = false; + let exceptionThrownAura = false; + const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + try { + await inputGuard(sourceUriLWC.fsPath, 'Hello'); + } catch (e) { + exceptionThrownLwc = true; + } + try { + await inputGuard(sourceUriAura.fsPath, 'Hello'); + } catch (e) { + exceptionThrownAura = true; + } + expect(exceptionThrownLwc).to.equal(false); + expect(exceptionThrownAura).to.equal(false); + }); + + it('should change the first letter to lower case if the new LWC component name is a upper-case letter', async () => { + let returnedName: any; + let exceptionThrownLwc = false; + const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + try { + returnedName = await inputGuard(sourceUriLWC.fsPath, 'Hello'); + } catch (e) { + exceptionThrownLwc = true; + } + expect(returnedName).to.equal('hello'); + expect(exceptionThrownLwc).to.equal(false); + }); + + it('should show the error message when component name contains special characters other than underscore or alphanumeric for LWC and Aura', async () => { + let exceptionThrownLwc = false; + let exceptionThrownAura = false; + const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + try { + await inputGuard(sourceUriLWC.fsPath, 'hello%$world'); + } catch (e) { + exceptionThrownLwc = true; + } + try { + await inputGuard(sourceUriAura.fsPath, 'hello%$world'); + } catch (e) { + exceptionThrownAura = true; + } + expect(exceptionThrownLwc).to.equal(true); + expect(exceptionThrownAura).to.equal(true); + }); + + it('should show the error message when component name contains two consecutive underscores for LWC and Aura', async () => { + let exceptionThrownLwc = false; + let exceptionThrownAura = false; + const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + try { + await inputGuard(sourceUriLWC.fsPath, 'hello__world'); + } catch (e) { + exceptionThrownLwc = true; + } + try { + await inputGuard(sourceUriAura.fsPath, 'hello__world'); + } catch (e) { + exceptionThrownAura = true; + } + expect(exceptionThrownLwc).to.equal(true); + expect(exceptionThrownAura).to.equal(true); + }); + + it('should show the error message when component name ends with an underscore for LWC and Aura', async () => { + let exceptionThrownLwc = false; + let exceptionThrownAura = false; + const sourceUriLWC = vscode.Uri.joinPath(lwcPath, lwcComponent); + const sourceUriAura = vscode.Uri.joinPath(auraPath, auraComponent); + try { + await inputGuard(sourceUriLWC.fsPath, 'hello_'); + } catch (e) { + exceptionThrownLwc = true; + } + try { + await inputGuard(sourceUriAura.fsPath, 'hello_'); + } catch (e) { + exceptionThrownAura = true; + } + expect(exceptionThrownLwc).to.equal(true); + expect(exceptionThrownAura).to.equal(true); + }); + + }); describe('getLightningComponentDirectory function', () => { From 7f1b9262ce047ff7d3995b2c469892c4640b9ea8 Mon Sep 17 00:00:00 2001 From: Dehru Cromer Date: Fri, 8 Jul 2022 13:49:54 -0600 Subject: [PATCH 6/6] chore: update message after input from doc --- packages/salesforcedx-vscode-core/src/messages/i18n.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/salesforcedx-vscode-core/src/messages/i18n.ts b/packages/salesforcedx-vscode-core/src/messages/i18n.ts index 7f6fc175f3..65dcff99bc 100644 --- a/packages/salesforcedx-vscode-core/src/messages/i18n.ts +++ b/packages/salesforcedx-vscode-core/src/messages/i18n.ts @@ -640,7 +640,7 @@ export const messages = { rename_component_warning: 'Warning: References to the old name will not be updated. Update manually and redeploy once all changes have been made.', rename_component_error: - 'Unable to rename component. Update manually and redeploy once all changes have been made.', + 'Unable to rename the component. Try renaming the component manually and then redeploying your changes.', error_function_type: 'Unable to determine type of executing function.', error_unable_to_get_started_function: 'Unable to access the function in "{0}".'