addons/vscode/__mocks__/vscode.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type * as vscode from 'vscode';
b69ab319
b69ab3110// don't want to mock vscode.Uri, so use library for it
b69ab3111import {URI} from 'vscode-uri';
b69ab3112export const Uri = URI;
b69ab3113
b69ab3114export const workspace = proxyMissingFieldsWithJestFn({
b69ab3115 workspaceFolders: undefined,
b69ab3116 getConfiguration: () => ({get: jest.fn()}),
b69ab3117});
b69ab3118export const scm = proxyMissingFieldsWithJestFn({
b69ab3119 createSourceControl: jest.fn(
b69ab3120 (): vscode.SourceControl => ({
b69ab3121 inputBox: {value: '', placeholder: '', enabled: true, visible: true},
b69ab3122 createResourceGroup: jest.fn(() => ({
b69ab3123 hideWhenEmpty: false,
b69ab3124 resourceStates: [],
b69ab3125 id: '',
b69ab3126 label: '',
b69ab3127 dispose: jest.fn(),
b69ab3128 })),
b69ab3129 id: '',
b69ab3130 dispose: jest.fn(),
b69ab3131 label: '',
b69ab3132 rootUri: Uri.file(''),
b69ab3133 }),
b69ab3134 ),
b69ab3135});
b69ab3136
b69ab3137export class ThemeColor {
b69ab3138 constructor(public id: string) {}
b69ab3139}
b69ab3140
b69ab3141export class Disposable implements vscode.Disposable {
b69ab3142 dispose = jest.fn();
b69ab3143}
b69ab3144
b69ab3145// to avoid manually writing jest.fn() for every API,
b69ab3146// assume fields that we don't provide are jest.fn() which return disposables
b69ab3147function proxyMissingFieldsWithJestFn<T extends object>(t: T): T {
b69ab3148 return new Proxy(t, {
b69ab3149 get: ((_: unknown, key: keyof T) => {
b69ab3150 if (Object.prototype.hasOwnProperty.call(t, key)) {
b69ab3151 return t[key];
b69ab3152 }
b69ab3153 // make sure we keep the jest.fn() we make so it's not remade each time
b69ab3154 t[key] = jest.fn().mockReturnValue(new Disposable()) as unknown as (typeof t)[keyof T];
b69ab3155 return t[key];
b69ab3156 }) as unknown as ProxyHandler<T>['get'],
b69ab3157 });
b69ab3158}
b69ab3159
b69ab3160interface Event<T> {
b69ab3161 (listener: (e: T) => unknown): unknown;
b69ab3162}
b69ab3163
b69ab3164export class EventEmitter<T> {
b69ab3165 event: Event<T> = () => undefined;
b69ab3166 // eslint-disable-next-line @typescript-eslint/no-empty-function
b69ab3167 fire(_data: T): void {}
b69ab3168 // eslint-disable-next-line @typescript-eslint/no-empty-function
b69ab3169 dispose(): void {}
b69ab3170}