Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mocking of modeling events #3542

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions extensions/ql-vscode/test/__mocks__/appMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { App, EnvironmentContext } from "../../src/common/app";
import { AppMode } from "../../src/common/app";
import type { AppEvent, AppEventEmitter } from "../../src/common/events";
import type { Memento } from "../../src/common/memento";
import type { Disposable } from "../../src/common/disposable-object";
import { createMockLogger } from "./loggerMock";
import { createMockMemento } from "../mock-memento";
import { testCredentialsWithStub } from "../factories/authentication";
Expand Down Expand Up @@ -54,29 +53,25 @@ export function createMockApp({

class MockAppEventEmitter<T> implements AppEventEmitter<T> {
public event: AppEvent<T>;
private listeners: Array<(event: T) => void> = [];

constructor() {
this.event = () => {
return new MockAppEvent();
this.event = (listener) => {
this.listeners.push(listener);
return {
dispose: () => {
this.listeners = this.listeners.filter((l) => l !== listener);
},
};
};
}

public fire(): void {
// no-op
public fire(event: T): void {
this.listeners.forEach((listener) => listener(event));
}

public dispose() {
// no-op
}
}

class MockAppEvent implements Disposable {
public fire(): void {
// no-op
}

public dispose() {
// no-op
this.listeners = [];
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { CodeQLCliServer } from "../../../../src/codeql-cli/cli";
import type { App } from "../../../../src/common/app";
import type { BaseLogger } from "../../../../src/common/logging";
import type { NotificationLogger } from "../../../../src/common/logging";
import { QueryLanguage } from "../../../../src/common/query-language";
import type { DatabaseItem } from "../../../../src/databases/local-databases";
import type { ModelEvaluationRun } from "../../../../src/model-editor/model-evaluation-run";
import { ModelEvaluator } from "../../../../src/model-editor/model-evaluator";
import type { ModelingEvents } from "../../../../src/model-editor/modeling-events";
import { ModelingEvents } from "../../../../src/model-editor/modeling-events";
import type { ModelingStore } from "../../../../src/model-editor/modeling-store";
import type { ExtensionPack } from "../../../../src/model-editor/shared/extension-pack";
import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager";
import { createMockApp } from "../../../__mocks__/appMock";
import { createMockLogger } from "../../../__mocks__/loggerMock";
import { createMockModelingEvents } from "../../../__mocks__/model-editor/modelingEventsMock";
import { createMockModelingStore } from "../../../__mocks__/model-editor/modelingStoreMock";
import { mockedObject } from "../../../mocked-object";

describe("Model Evaluator", () => {
let modelEvaluator: ModelEvaluator;
let logger: BaseLogger;
let logger: NotificationLogger;
let app: App;
let cliServer: CodeQLCliServer;
let modelingStore: ModelingStore;
Expand All @@ -30,13 +30,13 @@ describe("Model Evaluator", () => {

beforeEach(() => {
logger = createMockLogger();
app = mockedObject<App>({ logger });
app = createMockApp({ logger });
cliServer = mockedObject<CodeQLCliServer>({});
getModelEvaluationRunMock = jest.fn();
modelingStore = createMockModelingStore({
getModelEvaluationRun: getModelEvaluationRunMock,
});
modelingEvents = createMockModelingEvents();
modelingEvents = new ModelingEvents(app);
variantAnalysisManager = mockedObject<VariantAnalysisManager>({
cancelVariantAnalysis: jest.fn(),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type { Uri, Webview, WebviewView } from "vscode";
import { EventEmitter } from "vscode";
import type { ModelConfigListener } from "../../../../../src/config";
import { MethodModelingViewProvider } from "../../../../../src/model-editor/method-modeling/method-modeling-view-provider";
import { createMockApp } from "../../../../__mocks__/appMock";
import { createMockModelingEvents } from "../../../../__mocks__/model-editor/modelingEventsMock";
import { createMockModelingStore } from "../../../../__mocks__/model-editor/modelingStoreMock";
import { mockedObject } from "../../../../mocked-object";
import type { FromMethodModelingMessage } from "../../../../../src/common/interface-types";
import { DisposableObject } from "../../../../../src/common/disposable-object";
import type { ModelingEvents } from "../../../../../src/model-editor/modeling-events";
import { ModelingEvents } from "../../../../../src/model-editor/modeling-events";
import type {
DbModelingState,
ModelingStore,
Expand All @@ -29,10 +27,6 @@ describe("method modeling view provider", () => {
ModelingStore["getSelectedMethodDetails"]
>;

// Modeling events
let selectedMethodChangedEventEmitter: ModelingEvents["onSelectedMethodChangedEventEmitter"];
let dbOpenedEventEmitter: ModelingEvents["onDbOpenedEventEmitter"];

// View provider
let viewProvider: MethodModelingViewProvider;
let onDidReceiveMessage: (msg: FromMethodModelingMessage) => Promise<void>;
Expand All @@ -48,12 +42,7 @@ describe("method modeling view provider", () => {
getSelectedMethodDetails,
});

selectedMethodChangedEventEmitter = new EventEmitter();
dbOpenedEventEmitter = new EventEmitter();
const modelingEvents = createMockModelingEvents({
onSelectedMethodChanged: selectedMethodChangedEventEmitter.event,
onDbOpened: dbOpenedEventEmitter.event,
});
const modelingEvents = new ModelingEvents(app);

const modelConfigListener = mockedObject<ModelConfigListener>({
showTypeModels: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TreeView } from "vscode";
import { EventEmitter, window } from "vscode";
import { window } from "vscode";
import type { CodeQLCliServer } from "../../../../../src/codeql-cli/cli";
import type { Method } from "../../../../../src/model-editor/method";
import { MethodsUsagePanel } from "../../../../../src/model-editor/methods-usage/methods-usage-panel";
Expand All @@ -13,8 +13,8 @@ import type { ModelingStore } from "../../../../../src/model-editor/modeling-sto
import { createMockModelingStore } from "../../../../__mocks__/model-editor/modelingStoreMock";
import type { ModeledMethod } from "../../../../../src/model-editor/modeled-method";
import { Mode } from "../../../../../src/model-editor/shared/mode";
import { createMockModelingEvents } from "../../../../__mocks__/model-editor/modelingEventsMock";
import type { ModelingEvents } from "../../../../../src/model-editor/modeling-events";
import { ModelingEvents } from "../../../../../src/model-editor/modeling-events";
import { createMockApp } from "../../../../__mocks__/appMock";

describe("MethodsUsagePanel", () => {
const mockCliServer = mockedObject<CodeQLCliServer>({});
Expand All @@ -36,7 +36,8 @@ describe("MethodsUsagePanel", () => {
jest.spyOn(window, "createTreeView").mockReturnValue(mockTreeView);

const modelingStore = createMockModelingStore();
const modelingEvents = createMockModelingEvents();
const app = createMockApp({});
const modelingEvents = new ModelingEvents(app);

const panel = new MethodsUsagePanel(
modelingStore,
Expand Down Expand Up @@ -66,8 +67,6 @@ describe("MethodsUsagePanel", () => {
const modeledMethods: Record<string, ModeledMethod[]> = {};
const modifiedMethodSignatures: Set<string> = new Set();
const usage = createUsage();
const selectedMethodChangedEmitter: ModelingEvents["onSelectedMethodChangedEventEmitter"] =
new EventEmitter();

beforeEach(() => {
mockTreeView = mockedObject<TreeView<unknown>>({
Expand All @@ -76,9 +75,7 @@ describe("MethodsUsagePanel", () => {
jest.spyOn(window, "createTreeView").mockReturnValue(mockTreeView);

modelingStore = createMockModelingStore();
modelingEvents = createMockModelingEvents({
onSelectedMethodChanged: selectedMethodChangedEmitter.event,
});
modelingEvents = new ModelingEvents(createMockApp({}));
});

it("should reveal the correct item in the tree view", async () => {
Expand All @@ -101,15 +98,15 @@ describe("MethodsUsagePanel", () => {
modifiedMethodSignatures,
);

selectedMethodChangedEmitter.fire({
databaseItem: dbItem,
modelingEvents.fireSelectedMethodChangedEvent(
dbItem,
method,
usage,
modeledMethods: modeledMethods[method.signature],
isModified: modifiedMethodSignatures.has(method.signature),
isInProgress: false,
processedByAutoModel: false,
});
modeledMethods[method.signature],
modifiedMethodSignatures.has(method.signature),
false,
false,
);

expect(mockTreeView.reveal).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -136,15 +133,15 @@ describe("MethodsUsagePanel", () => {
modifiedMethodSignatures,
);

selectedMethodChangedEmitter.fire({
databaseItem: dbItem,
modelingEvents.fireSelectedMethodChangedEvent(
dbItem,
method,
usage,
modeledMethods: modeledMethods[method.signature],
isModified: modifiedMethodSignatures.has(method.signature),
isInProgress: false,
processedByAutoModel: false,
});
modeledMethods[method.signature],
modifiedMethodSignatures.has(method.signature),
false,
false,
);

expect(mockTreeView.reveal).not.toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import type { QueryRunner } from "../../../../src/query-server";
import type { ExtensionPack } from "../../../../src/model-editor/shared/extension-pack";
import { createMockModelingStore } from "../../../__mocks__/model-editor/modelingStoreMock";
import type { ModelConfigListener } from "../../../../src/config";
import { createMockModelingEvents } from "../../../__mocks__/model-editor/modelingEventsMock";
import { QueryLanguage } from "../../../../src/common/query-language";
import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager";
import type { DatabaseFetcher } from "../../../../src/databases/database-fetcher";
import { ModelingEvents } from "../../../../src/model-editor/modeling-events";

describe("ModelEditorView", () => {
const app = createMockApp({});
const modelingStore = createMockModelingStore();
const modelingEvents = createMockModelingEvents();
const modelingEvents = new ModelingEvents(app);
const modelConfig = mockedObject<ModelConfigListener>({
onDidChangeConfiguration: jest.fn(),
});
Expand Down
Loading