| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type * as vscode from 'vscode'; |
| 9 | |
| 10 | // don't want to mock vscode.Uri, so use library for it |
| 11 | import {URI} from 'vscode-uri'; |
| 12 | export const Uri = URI; |
| 13 | |
| 14 | export const workspace = proxyMissingFieldsWithJestFn({ |
| 15 | workspaceFolders: undefined, |
| 16 | getConfiguration: () => ({get: jest.fn()}), |
| 17 | }); |
| 18 | export const scm = proxyMissingFieldsWithJestFn({ |
| 19 | createSourceControl: jest.fn( |
| 20 | (): vscode.SourceControl => ({ |
| 21 | inputBox: {value: '', placeholder: '', enabled: true, visible: true}, |
| 22 | createResourceGroup: jest.fn(() => ({ |
| 23 | hideWhenEmpty: false, |
| 24 | resourceStates: [], |
| 25 | id: '', |
| 26 | label: '', |
| 27 | dispose: jest.fn(), |
| 28 | })), |
| 29 | id: '', |
| 30 | dispose: jest.fn(), |
| 31 | label: '', |
| 32 | rootUri: Uri.file(''), |
| 33 | }), |
| 34 | ), |
| 35 | }); |
| 36 | |
| 37 | export class ThemeColor { |
| 38 | constructor(public id: string) {} |
| 39 | } |
| 40 | |
| 41 | export class Disposable implements vscode.Disposable { |
| 42 | dispose = jest.fn(); |
| 43 | } |
| 44 | |
| 45 | // to avoid manually writing jest.fn() for every API, |
| 46 | // assume fields that we don't provide are jest.fn() which return disposables |
| 47 | function proxyMissingFieldsWithJestFn<T extends object>(t: T): T { |
| 48 | return new Proxy(t, { |
| 49 | get: ((_: unknown, key: keyof T) => { |
| 50 | if (Object.prototype.hasOwnProperty.call(t, key)) { |
| 51 | return t[key]; |
| 52 | } |
| 53 | // make sure we keep the jest.fn() we make so it's not remade each time |
| 54 | t[key] = jest.fn().mockReturnValue(new Disposable()) as unknown as (typeof t)[keyof T]; |
| 55 | return t[key]; |
| 56 | }) as unknown as ProxyHandler<T>['get'], |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | interface Event<T> { |
| 61 | (listener: (e: T) => unknown): unknown; |
| 62 | } |
| 63 | |
| 64 | export class EventEmitter<T> { |
| 65 | event: Event<T> = () => undefined; |
| 66 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 67 | fire(_data: T): void {} |
| 68 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 69 | dispose(): void {} |
| 70 | } |
| 71 | |