|
| 1 | +import * as assert from "assert"; |
| 2 | +import { GitGuardianQuotaWebviewProvider } from "../../../ggshield-webview/gitguardian-quota-webview"; |
| 3 | +import { GGShieldConfiguration } from "../../../lib/ggshield-configuration"; |
| 4 | +import { |
| 5 | + ExtensionContext, |
| 6 | + Memento, |
| 7 | + Uri, |
| 8 | + WebviewOptions, |
| 9 | + WebviewView, |
| 10 | +} from "vscode"; |
| 11 | + |
| 12 | +suite("GitGuardianQuotaWebviewProvider", () => { |
| 13 | + let provider: GitGuardianQuotaWebviewProvider; |
| 14 | + let mockGGShieldConfig: Partial<GGShieldConfiguration>; |
| 15 | + let mockWebviewView: Partial<WebviewView>; |
| 16 | + let mockWorkspaceState: Memento & { |
| 17 | + setKeysForSync(keys: readonly string[]): void; |
| 18 | + }; |
| 19 | + let mockContext: Partial<ExtensionContext>; |
| 20 | + |
| 21 | + setup(() => { |
| 22 | + mockWorkspaceState = { |
| 23 | + get: (key: string) => undefined, |
| 24 | + update: (key: string, value: any) => { |
| 25 | + key = value; |
| 26 | + return Promise.resolve(); |
| 27 | + }, |
| 28 | + keys: () => [], |
| 29 | + setKeysForSync: (keys: readonly string[]) => {}, |
| 30 | + }; |
| 31 | + |
| 32 | + mockContext = { |
| 33 | + workspaceState: mockWorkspaceState, |
| 34 | + }; |
| 35 | + |
| 36 | + provider = new GitGuardianQuotaWebviewProvider( |
| 37 | + {} as GGShieldConfiguration, |
| 38 | + Uri.parse("file:///mock"), |
| 39 | + mockContext as ExtensionContext |
| 40 | + ); |
| 41 | + |
| 42 | + mockWebviewView = { |
| 43 | + webview: { |
| 44 | + html: "", |
| 45 | + onDidReceiveMessage: () => ({ dispose: () => {} }), |
| 46 | + cspSource: "", |
| 47 | + options: {} as WebviewOptions, |
| 48 | + postMessage: (message: any) => Promise.resolve(true), |
| 49 | + asWebviewUri: (uri: Uri) => uri, |
| 50 | + }, |
| 51 | + onDidChangeVisibility: () => ({ dispose: () => {} }), |
| 52 | + visible: false, |
| 53 | + }; |
| 54 | + provider["_view"] = mockWebviewView as WebviewView; |
| 55 | + }); |
| 56 | + |
| 57 | + test("should update the webview content when loading", () => { |
| 58 | + provider["isLoading"] = true; |
| 59 | + provider["updateWebViewContent"](); |
| 60 | + |
| 61 | + assert.ok(provider["_view"]?.webview.html.includes("<p>Loading...</p>")); |
| 62 | + }); |
| 63 | + |
| 64 | + test("should display the quota when authenticated", () => { |
| 65 | + provider["isLoading"] = false; |
| 66 | + provider["isAuthenticated"] = true; |
| 67 | + provider["quota"] = 100; |
| 68 | + |
| 69 | + provider["updateWebViewContent"](); |
| 70 | + |
| 71 | + assert.ok( |
| 72 | + provider["_view"]?.webview.html.includes("<p>Your current quota: 100</p>") |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + test("should display authentication prompt when unauthenticated", () => { |
| 77 | + provider["isLoading"] = false; |
| 78 | + provider["isAuthenticated"] = false; |
| 79 | + |
| 80 | + provider["updateWebViewContent"](); |
| 81 | + |
| 82 | + assert.ok( |
| 83 | + provider["_view"]?.webview.html.includes( |
| 84 | + "<p>Please authenticate to see your quota.</p>" |
| 85 | + ) |
| 86 | + ); |
| 87 | + }); |
| 88 | +}); |
0 commit comments