Skip to content

Commit

Permalink
Merge pull request #63 from GitGuardian/salomevoltz/fix-refresh-quota…
Browse files Browse the repository at this point in the history
…-button

fix(refresh-quota): Fix button refresh quota
  • Loading branch information
salome-voltz authored Nov 29, 2024
2 parents 8b89858 + 07f604d commit 831bf99
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ export function activate(context: ExtensionContext) {

//generic commands to open correct view on status bar click
registerOpenViewsCommands(context, outputChannel);
commands.registerCommand(
"gitguardian.refreshQuota",
ggshieldQuotaViewProvider.refresh
commands.registerCommand("gitguardian.refreshQuota", () =>
ggshieldQuotaViewProvider.refresh()
);

context.subscriptions.push(
Expand Down
2 changes: 1 addition & 1 deletion src/ggshield-webview/gitguardian-quota-webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class GitGuardianQuotaWebviewProvider
<p>Loading...</p>
</body>
</html>`;
} else if (this.quota !== undefined) {
} else if (this.quota !== 0 && this.isAuthenticated) {
computedHtml = `
<!DOCTYPE html>
<html lang="en">
Expand Down
88 changes: 88 additions & 0 deletions src/test/suite/ggshield-webview/gitguardian-quota-webview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as assert from "assert";
import { GitGuardianQuotaWebviewProvider } from "../../../ggshield-webview/gitguardian-quota-webview";
import { GGShieldConfiguration } from "../../../lib/ggshield-configuration";
import {
ExtensionContext,
Memento,
Uri,
WebviewOptions,
WebviewView,
} from "vscode";

suite("GitGuardianQuotaWebviewProvider", () => {
let provider: GitGuardianQuotaWebviewProvider;
let mockGGShieldConfig: Partial<GGShieldConfiguration>;
let mockWebviewView: Partial<WebviewView>;
let mockWorkspaceState: Memento & {
setKeysForSync(keys: readonly string[]): void;
};
let mockContext: Partial<ExtensionContext>;

setup(() => {
mockWorkspaceState = {
get: (key: string) => undefined,
update: (key: string, value: any) => {
key = value;
return Promise.resolve();
},
keys: () => [],
setKeysForSync: (keys: readonly string[]) => {},
};

mockContext = {
workspaceState: mockWorkspaceState,
};

provider = new GitGuardianQuotaWebviewProvider(
{} as GGShieldConfiguration,
Uri.parse("file:///mock"),
mockContext as ExtensionContext
);

mockWebviewView = {
webview: {
html: "",
onDidReceiveMessage: () => ({ dispose: () => {} }),
cspSource: "",
options: {} as WebviewOptions,
postMessage: (message: any) => Promise.resolve(true),
asWebviewUri: (uri: Uri) => uri,
},
onDidChangeVisibility: () => ({ dispose: () => {} }),
visible: false,
};
provider["_view"] = mockWebviewView as WebviewView;
});

test("should update the webview content when loading", () => {
provider["isLoading"] = true;
provider["updateWebViewContent"]();

assert.ok(provider["_view"]?.webview.html.includes("<p>Loading...</p>"));
});

test("should display the quota when authenticated", () => {
provider["isLoading"] = false;
provider["isAuthenticated"] = true;
provider["quota"] = 100;

provider["updateWebViewContent"]();

assert.ok(
provider["_view"]?.webview.html.includes("<p>Your current quota: 100</p>")
);
});

test("should display authentication prompt when unauthenticated", () => {
provider["isLoading"] = false;
provider["isAuthenticated"] = false;

provider["updateWebViewContent"]();

assert.ok(
provider["_view"]?.webview.html.includes(
"<p>Please authenticate to see your quota.</p>"
)
);
});
});

0 comments on commit 831bf99

Please sign in to comment.