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