Skip to content

Commit

Permalink
fix: already open Yeoman UI generator is closed on opening different … (
Browse files Browse the repository at this point in the history
#792)

* fix: already open Yeoman UI generator is closed on opening different Yeoman UI generator

* style: message text rephrased
  • Loading branch information
alex-gilin authored Jan 17, 2024
1 parent ec84741 commit d28b1e1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 18 deletions.
50 changes: 41 additions & 9 deletions packages/backend/src/extCommands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExtensionContext, commands } from "vscode";
import { ExtensionContext, commands, window } from "vscode";
import messages from "./messages";

export class ExtCommands {
private exploreGensPanel: any;
Expand Down Expand Up @@ -29,32 +30,63 @@ export class ExtCommands {
}

private async yeomanUIPanel_runGenerator_Command() {
return (await this.getYeomanUIPanel()).runGenerator();
try {
return (await this.getYeomanUIPanel()).runGenerator();
} catch (e) {
console.log(e);
}
}

private async yeomanUIPanel_loadYeomanUI_Command(uiOptions?: any) {
return (await this.getYeomanUIPanel()).loadWebviewPanel(uiOptions);
try {
return (await this.getYeomanUIPanel()).loadWebviewPanel(uiOptions);
} catch (e) {
console.log(e);
}
}

private async yeomanUIPanel_toggleOutput_Command() {
return (await this.getYeomanUIPanel()).toggleOutput();
return (await this.getYeomanUIPanel(false)).toggleOutput();
}

private async yeomanUIPanel_notifyGeneratorsChange_Command(uiOptions?: any) {
return (await this.getYeomanUIPanel()).notifyGeneratorsChange(uiOptions);
return (await this.getYeomanUIPanel(false)).notifyGeneratorsChange(uiOptions);
}

private async exploreGenerators_Command(uiOptions?: any) {
return (await this.getExploreGensPanel()).loadWebviewPanel(uiOptions);
try {
return (await this.getExploreGensPanel()).loadWebviewPanel(uiOptions);
} catch (e) {
console.log(e);
}
}

private async isInEmptyState(): Promise<boolean> {
if (this.yeomanUIPanel?.yeomanui?.generatorName) {
const btnContinue = "Continue";
if (
(await window.showWarningMessage(
messages.warn_another_generator_running(this.yeomanUIPanel.yeomanui.generatorName.split(":")[0]),
btnContinue,
"Cancel"
)) !== btnContinue
) {
return false;
}
}
return true;
}

public async getYeomanUIPanel() {
public async getYeomanUIPanel(verifyEmptyState = true) {
if (!this.yeomanUIPanel) {
const { YeomanUIPanel } = await import("./panels/YeomanUIPanel");
this.yeomanUIPanel = new YeomanUIPanel(this.context);
}

return this.yeomanUIPanel;
if (!verifyEmptyState || (await this.isInEmptyState())) {
return this.yeomanUIPanel;
} else {
throw new Error("another generator is running");
}
}

public async getExploreGensPanel() {
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ export default {
"Some generators were not installed. You can continue to work in SAP Business Application Studio and we will retry next time you restart the dev space.",
nodejs_install_not_found:
"Node.js installation cannot be detected. Please ensure you have installed Node.js before attempting to use Template Wizard",
warn_another_generator_running: (name: string) =>
`You have unsaved changes in the ${name} generator. Running a new generator will make you lose these changes. Are you sure you want to continue?`,
};
93 changes: 84 additions & 9 deletions packages/backend/test/extCommands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,94 @@ describe("extension commands unit test", () => {
expect(notifyGeneratorsChangeSpy.called).to.be.true;
});

it("getYeomanUIPanel", async () => {
it("call YeomanUIPanel commands - when yeomanUIPanel is in invalid state", async () => {
const yeomanUIPanelMock = {
runGenerator: () => "",
loadWebviewPanel: () => "",
toggleOutput: () => "",
notifyGeneratorsChange: () => "",
};

const extCommands = new ExtCommands(testContext);
extCommands["yeomanUIPanel"] = undefined;
sandbox.stub(extCommands, "getYeomanUIPanel").callsFake((verifyEmptyState = true) => {
if (verifyEmptyState) {
throw new Error("yeomanUIPanel is in invalid state");
}
return Promise.resolve(yeomanUIPanelMock);
});

loggerWrapperMock.expects("getClassLogger");
// windowMock.expects("registerWebviewPanelSerializer").withArgs("yeomanui");
const runGeneratorSpy = sandbox.spy(yeomanUIPanelMock, "runGenerator");
const loadWebviewPanelSpy = sandbox.spy(yeomanUIPanelMock, "loadWebviewPanel");
const toggleOutputSpy = sandbox.spy(yeomanUIPanelMock, "toggleOutput");
const notifyGeneratorsChangeSpy = sandbox.spy(yeomanUIPanelMock, "notifyGeneratorsChange");

await extCommands["yeomanUIPanel_loadYeomanUI_Command"]();
await extCommands["yeomanUIPanel_toggleOutput_Command"]();
await extCommands["yeomanUIPanel_notifyGeneratorsChange_Command"]();
await extCommands["yeomanUIPanel_runGenerator_Command"]();

// yeomanUIPanel is undefined
const yeomanUIPanel_firstTime = await extCommands["getYeomanUIPanel"]();
// yeomanUIPanel should be already defined
const yeomanUIPanel_secondTime = await extCommands["getYeomanUIPanel"]();
expect(runGeneratorSpy.called).to.be.false;
expect(loadWebviewPanelSpy.called).to.be.false;
expect(toggleOutputSpy.called).to.be.true;
expect(notifyGeneratorsChangeSpy.called).to.be.true;
});

describe("getYeomanUIPanel", () => {
const extCommands = new ExtCommands(testContext);

expect(yeomanUIPanel_firstTime).to.be.equal(yeomanUIPanel_secondTime);
it("getYeomanUIPanel - open twice, no generator loaded", async () => {
extCommands["yeomanUIPanel"] = undefined;

loggerWrapperMock.expects("getClassLogger");

// yeomanUIPanel is undefined
const yeomanUIPanel_firstTime = await extCommands["getYeomanUIPanel"]();
// yeomanUIPanel should be already defined
const yeomanUIPanel_secondTime = await extCommands["getYeomanUIPanel"]();

expect(yeomanUIPanel_firstTime).to.be.equal(yeomanUIPanel_secondTime);
});

it("getYeomanUIPanel - there is generator running, answer 'Continue'", async () => {
const mockYeomanui = { yeomanui: { generatorName: "test" } };
extCommands["yeomanUIPanel"] = mockYeomanui;
windowMock.expects("showWarningMessage").resolves("Continue");
expect(await extCommands["getYeomanUIPanel"]()).be.equal(mockYeomanui);
});

it("getYeomanUIPanel - there is generator running, answer 'Cancel'", async () => {
const mockYeomanui = { yeomanui: { generatorName: "test" } };
extCommands["yeomanUIPanel"] = mockYeomanui;
windowMock.expects("showWarningMessage").resolves("Cancel");
try {
await extCommands["getYeomanUIPanel"]();
} catch (e) {
expect(e instanceof Error).to.be.true;
}
});

it("getYeomanUIPanel - there is generator running, canceled", async () => {
const mockYeomanui = { yeomanui: { generatorName: "test" } };
extCommands["yeomanUIPanel"] = mockYeomanui;
windowMock.expects("showWarningMessage").resolves(undefined);
try {
await extCommands["getYeomanUIPanel"]();
} catch (e) {
expect(e instanceof Error).to.be.true;
}
});

it("getYeomanUIPanel - skip verifing empty state", async () => {
const mockYeomanui = { yeomanui: { generatorName: "test" } };
extCommands["yeomanUIPanel"] = mockYeomanui;
windowMock.expects("showWarningMessage").never();
expect(await extCommands["getYeomanUIPanel"](false)).be.equal(mockYeomanui);
});

it("isInEmptyState - yeomanUIPanel is undefined", async () => {
extCommands["yeomanUIPanel"] = undefined;
expect(await extCommands["isInEmptyState"]()).to.be.true;
});
});

it("getExploreGensPanel", async () => {
Expand Down

0 comments on commit d28b1e1

Please sign in to comment.