Skip to content

Commit

Permalink
Fixed unit tests regression
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Borzenko committed Nov 15, 2024
1 parent 8809ce0 commit 01e7833
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 234 deletions.
2 changes: 2 additions & 0 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { LoginService } from "./auth/login/login.service";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { NotificationIndicatorComponent } from "./components/notification-indicator/notification-indicator.component";
import { AngularSvgIconModule, SvgIconRegistryService } from "angular-svg-icon";
import { MatIconModule } from "@angular/material/icon";

describe("AppComponent", () => {
let component: AppComponent;
Expand All @@ -46,6 +47,7 @@ describe("AppComponent", () => {
FormsModule,
HttpClientTestingModule,
RouterModule,
MatIconModule,
AngularSvgIconModule.forRoot(),
],
declarations: [
Expand Down
2 changes: 2 additions & 0 deletions src/app/components/app-header/app-header.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { AngularSvgIconModule } from "angular-svg-icon";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { LoginMethod } from "src/app/app-config.model";
import { MatIconModule } from "@angular/material/icon";

describe("AppHeaderComponent", () => {
let component: AppHeaderComponent;
Expand All @@ -55,6 +56,7 @@ describe("AppHeaderComponent", () => {
AngularSvgIconModule.forRoot(),
HttpClientTestingModule,
RouterModule,
MatIconModule,
],
declarations: [AppHeaderComponent, NotificationIndicatorComponent],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,9 @@ import { MatIconModule } from "@angular/material/icon";
import { MatMenuModule } from "@angular/material/menu";
import { DataComponent } from "./data.component";

import {
emitClickOnElementByDataTestId,
findElementByDataTestId,
getElementByDataTestId,
} from "src/app/common/base-test.helpers.spec";
import { emitClickOnElementByDataTestId, findElementByDataTestId } from "src/app/common/base-test.helpers.spec";
import { DatasetSubscriptionsService } from "../../dataset.subscriptions.service";
import {
mockDataUpdate,
mockMetadataDerivedUpdate,
mockOverviewDataUpdate,
mockOverviewDataUpdateNullable,
mockSqlErrorUpdate,
} from "../data-tabs.mock";
import { mockMetadataDerivedUpdate, mockOverviewDataUpdate, mockOverviewDataUpdateNullable } from "../data-tabs.mock";
import { RouterTestingModule } from "@angular/router/testing";
import { Location } from "@angular/common";
import { FormsModule } from "@angular/forms";
Expand All @@ -33,27 +23,16 @@ import { SqlEditorComponent } from "src/app/shared/editor/components/sql-editor/
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { HttpClientModule } from "@angular/common/http";
import { Apollo } from "apollo-angular";
import { ToastrModule, ToastrService } from "ngx-toastr";
import { Clipboard } from "@angular/cdk/clipboard";
import { QueryExplainerService } from "src/app/query-explainer/query-explainer.service";
import { of } from "rxjs";
import { mockQueryExplainerResponse } from "src/app/query-explainer/query-explainer.mocks";
import { FileUploadService } from "src/app/services/file-upload.service";
import { mockUploadPrepareResponse } from "src/app/api/mock/upload-file.mock";
import { ToastrModule } from "ngx-toastr";
import { SavedQueriesSectionComponent } from "./saved-queries-section/saved-queries-section.component";
import { QueryAndResultSectionsComponent } from "./query-and-result-sections/query-and-result-sections.component";
import { DatasetModule } from "../../dataset.module";

describe("DataComponent", () => {
let component: DataComponent;
let fixture: ComponentFixture<DataComponent>;
let datasetSubsService: DatasetSubscriptionsService;
let location: Location;
let ngbModalService: NgbModal;
let clipboard: Clipboard;
let toastrService: ToastrService;
let queryExplainerService: QueryExplainerService;
let fileUploadService: FileUploadService;

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -71,7 +50,6 @@ describe("DataComponent", () => {
CdkAccordionModule,
HttpClientModule,
ToastrModule.forRoot(),
//DatasetModule,
],
declarations: [
DataComponent,
Expand All @@ -86,38 +64,17 @@ describe("DataComponent", () => {
datasetSubsService = TestBed.inject(DatasetSubscriptionsService);
location = TestBed.inject(Location);
ngbModalService = TestBed.inject(NgbModal);
clipboard = TestBed.inject(Clipboard);
toastrService = TestBed.inject(ToastrService);
component = fixture.componentInstance;
component.datasetBasics = mockDatasetBasicsDerivedFragment;
component.sqlLoading = false;
component.sqlRequestCode = "";
spyOn(location, "getState").and.returnValue({ start: 0, end: 100 });
datasetSubsService.emitSqlQueryDataChanged(mockDataUpdate);
queryExplainerService = TestBed.inject(QueryExplainerService);
fileUploadService = TestBed.inject(FileUploadService);
});

it("should create", () => {
expect(component).toBeTruthy();
});

// it("should check that the progress bar for the editor disappears", fakeAsync(() => {
// fixture.detectChanges();

// expect(component.editorLoaded).toBeFalse();
// const progressBarElementBefore = findElementByDataTestId(fixture, "editor-progress-bar");
// expect(progressBarElementBefore).toBeDefined();

// component.editorLoaded = true;
// tick();
// fixture.detectChanges();

// const progressBarElementAfter = findElementByDataTestId(fixture, "editor-progress-bar");
// expect(progressBarElementAfter).toBeUndefined();
// flush();
// }));

it("should check run sql button", fakeAsync(() => {
const runSQLRequestEmitSpy = spyOn(component.runSQLRequestEmit, "emit");
tick();
Expand All @@ -130,57 +87,6 @@ describe("DataComponent", () => {
flush();
}));

// it("should check #ngOninit", () => {
// expect(component.currentData).toEqual([]);
// component.ngOnInit();
// expect(component.sqlRequestCode).toEqual(
// `select\n *\nfrom '${mockDatasetBasicsDerivedFragment.alias}'\nwhere offset>=0 and offset<=100\norder by offset desc`,
// );
// });

// it("should check successful query result update", fakeAsync(() => {
// tick();
// fixture.detectChanges();
// expect(component.currentData).toEqual(mockDataUpdate.content);
// flush();
// }));

// it("should check invalid SQL result update", fakeAsync(() => {
// tick();
// fixture.detectChanges();
// datasetSubsService.emitSqlErrorOccurred(mockSqlErrorUpdate);
// tick();
// fixture.detectChanges();
// const runSqlButton = findElementByDataTestId(fixture, "runSqlQueryButton") as HTMLButtonElement;
// const elem = getElementByDataTestId(fixture, "sql-error-message");
// expect(runSqlButton.disabled).toBe(false);
// expect(elem.textContent).toEqual(mockSqlErrorUpdate.error);
// flush();
// }));

// it("should calculate sql request params", () => {
// datasetSubsService.emitSqlQueryDataChanged(mockDataUpdate);
// fixture.detectChanges();

// const sqlReq = spyOn(component.runSQLRequestEmit, "emit");
// const limit = 1;
// const params = {
// query: component.sqlRequestCode,
// skip: component.currentData.length,
// limit: limit,
// };

// component.loadMore(limit);
// datasetSubsService.emitSqlQueryDataChanged(mockDataUpdate);
// expect(sqlReq).toHaveBeenCalledWith(params);

// component.loadMore(limit);
// params.skip = component.currentData.length;

// const secondCallParams = sqlReq.calls.allArgs()[1];
// expect(secondCallParams).toEqual([params]);
// });

it("should check schema column names", fakeAsync(() => {
datasetSubsService.emitOverviewChanged({
schema: mockMetadataDerivedUpdate.schema,
Expand Down Expand Up @@ -211,36 +117,4 @@ describe("DataComponent", () => {
} as OverviewUpdate);
expect(ngbModalServiceSpy).toHaveBeenCalledTimes(1);
});

// it("should check click on `Share query` button", fakeAsync(() => {
// const toastServiceSpy = spyOn(toastrService, "success");
// const clipboardCopySpy = spyOn(clipboard, "copy");
// tick();
// fixture.detectChanges();

// emitClickOnElementByDataTestId(fixture, "shareSqlQueryButton");
// expect(toastServiceSpy).toHaveBeenCalledWith("Copied url to clipboard");
// expect(clipboardCopySpy).toHaveBeenCalledTimes(1);
// flush();
// }));

// it("should check click on `Copy as curl command` button", () => {
// const toastServiceSpy = spyOn(toastrService, "success");
// const clipboardCopySpy = spyOn(clipboard, "copy");
// component.copyCurlCommand();
// expect(toastServiceSpy).toHaveBeenCalledWith("Copied command to clipboard");
// expect(clipboardCopySpy).toHaveBeenCalledTimes(1);
// });

// it("should check click on `Verify query result` button", () => {
// const processQuerySpy = spyOn(queryExplainerService, "processQueryWithProof").and.returnValue(
// of(mockQueryExplainerResponse),
// );
// const uploadFilePrepareSpy = spyOn(fileUploadService, "uploadFilePrepare").and.returnValue(
// of(mockUploadPrepareResponse),
// );
// component.verifyQueryResult();
// expect(processQuerySpy).toHaveBeenCalledTimes(1);
// expect(uploadFilePrepareSpy).toHaveBeenCalledTimes(1);
// });
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ChangeDetectionStrategy, Component, EventEmitter, inject, Input, OnInit, Output } from "@angular/core";
import { Location } from "@angular/common";
import { map, Observable } from "rxjs";
import { Observable } from "rxjs";
import AppValues from "src/app/common/app.values";
import { DatasetFlowType, DatasetKind, OffsetInterval } from "../../../api/kamu.graphql.interface";
import { DataSqlErrorUpdate, OverviewUpdate } from "src/app/dataset-view/dataset.subscriptions.interface";
import { OverviewUpdate } from "src/app/dataset-view/dataset.subscriptions.interface";
import { DatasetRequestBySql } from "../../../interface/dataset.interface";
import { DatasetSubscriptionsService } from "../../dataset.subscriptions.service";
import { BaseComponent } from "src/app/common/base.component";
Expand Down Expand Up @@ -46,10 +46,10 @@ export class DataComponent extends BaseComponent implements OnInit {

public ngOnInit(): void {
this.overviewUpdate$ = this.datasetSubsService.overviewChanges;
this.sqlErrorMarker$ = this.sqlQueryService.sqlErrorOccurrences.pipe(
map((data: DataSqlErrorUpdate) => data.error),
);
this.sqlQueryResponse$ = this.sqlQueryService.sqlQueryResponseChanges;
// this.sqlErrorMarker$ = this.sqlQueryService.sqlErrorOccurrences.pipe(
// map((data: DataSqlErrorUpdate) => data.error),
// );
// this.sqlQueryResponse$ = this.sqlQueryService.sqlQueryResponseChanges;
this.buildSqlRequestCode();
this.runSQLRequest({ query: this.sqlRequestCode });
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { ComponentFixture, fakeAsync, flush, TestBed, tick } from "@angular/core/testing";
import { QueryAndResultSectionsComponent } from "./query-and-result-sections.component";
import { CdkAccordionModule } from "@angular/cdk/accordion";
import { ActivatedRoute } from "@angular/router";
import { Apollo } from "apollo-angular";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { ToastrModule } from "ngx-toastr";
import { ToastrModule, ToastrService } from "ngx-toastr";
import { MatMenuModule } from "@angular/material/menu";
import { RequestTimerComponent } from "../request-timer/request-timer.component";
import { SqlEditorComponent } from "src/app/shared/editor/components/sql-editor/sql-editor.component";
import { EditorModule } from "src/app/shared/editor/editor.module";
import { MatProgressBarModule } from "@angular/material/progress-bar";
import { MatIconModule } from "@angular/material/icon";
import { SharedTestModule } from "src/app/common/shared-test.module";
import {
emitClickOnElementByDataTestId,
findElementByDataTestId,
getElementByDataTestId,
} from "src/app/common/base-test.helpers.spec";
import { mockSqlErrorUpdate } from "../../data-tabs.mock";
import { Clipboard } from "@angular/cdk/clipboard";
import { QueryExplainerService } from "src/app/query-explainer/query-explainer.service";
import { of } from "rxjs";
import { mockUploadPrepareResponse } from "src/app/api/mock/upload-file.mock";
import { mockQueryExplainerResponse } from "src/app/query-explainer/query-explainer.mocks";
import { FileUploadService } from "src/app/services/file-upload.service";

describe("QueryAndResultSectionsComponent", () => {
let component: QueryAndResultSectionsComponent;
let fixture: ComponentFixture<QueryAndResultSectionsComponent>;
let clipboard: Clipboard;
let toastrService: ToastrService;
let queryExplainerService: QueryExplainerService;
let fileUploadService: FileUploadService;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -34,12 +49,98 @@ describe("QueryAndResultSectionsComponent", () => {
});
fixture = TestBed.createComponent(QueryAndResultSectionsComponent);
component = fixture.componentInstance;
clipboard = TestBed.inject(Clipboard);
toastrService = TestBed.inject(ToastrService);
queryExplainerService = TestBed.inject(QueryExplainerService);
fileUploadService = TestBed.inject(FileUploadService);
component.sqlLoading = false;
component.sqlRequestCode = "";
fixture.detectChanges();
component.sqlRequestCode = "select * from 'mock-dataset'";
});

it("should create", () => {
expect(component).toBeTruthy();
});

it("should check that the progress bar for the editor disappears", fakeAsync(() => {
component.editorLoaded = false;
fixture.detectChanges();

expect(component.editorLoaded).toBeFalse();
const progressBarElementBefore = findElementByDataTestId(fixture, "editor-progress-bar");
expect(progressBarElementBefore).toBeDefined();

component.editorLoaded = true;
tick();
fixture.detectChanges();

const progressBarElementAfter = findElementByDataTestId(fixture, "editor-progress-bar");
expect(progressBarElementAfter).toBeUndefined();
flush();
}));

it("should check invalid SQL result update", fakeAsync(() => {
component.sqlRequestCode = "select * from 'mock-dataset'";
component.sqlError = mockSqlErrorUpdate.error;
tick();
fixture.detectChanges();
tick();
fixture.detectChanges();
const runSqlButton = findElementByDataTestId(fixture, "runSqlQueryButton") as HTMLButtonElement;
const elem = getElementByDataTestId(fixture, "sql-error-message");
expect(runSqlButton.disabled).toBe(false);
expect(elem.textContent).toEqual(mockSqlErrorUpdate.error);
flush();
}));

it("should calculate sql request params", () => {
fixture.detectChanges();
const sqlReq = spyOn(component.runSQLRequestEmit, "emit");
const limit = 1;
const params = {
query: component.sqlRequestCode,
skip: component.currentData.length,
limit: limit,
};

component.loadMore(limit);
expect(sqlReq).toHaveBeenCalledWith(params);

component.loadMore(limit);
params.skip = component.currentData.length;

const secondCallParams = sqlReq.calls.allArgs()[1];
expect(secondCallParams).toEqual([params]);
});

it("should check click on `Share query` button", fakeAsync(() => {
const toastServiceSpy = spyOn(toastrService, "success");
const clipboardCopySpy = spyOn(clipboard, "copy");
tick();
fixture.detectChanges();

emitClickOnElementByDataTestId(fixture, "shareSqlQueryButton");
expect(toastServiceSpy).toHaveBeenCalledWith("Copied url to clipboard");
expect(clipboardCopySpy).toHaveBeenCalledTimes(1);
flush();
}));

it("should check click on `Copy as curl command` button", () => {
const toastServiceSpy = spyOn(toastrService, "success");
const clipboardCopySpy = spyOn(clipboard, "copy");
component.copyCurlCommand();
expect(toastServiceSpy).toHaveBeenCalledWith("Copied command to clipboard");
expect(clipboardCopySpy).toHaveBeenCalledTimes(1);
});

it("should check click on `Verify query result` button", () => {
const processQuerySpy = spyOn(queryExplainerService, "processQueryWithProof").and.returnValue(
of(mockQueryExplainerResponse),
);
const uploadFilePrepareSpy = spyOn(fileUploadService, "uploadFilePrepare").and.returnValue(
of(mockUploadPrepareResponse),
);
component.verifyQueryResult();
expect(processQuerySpy).toHaveBeenCalledTimes(1);
expect(uploadFilePrepareSpy).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit 01e7833

Please sign in to comment.