Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderVertegaal committed May 16, 2024
1 parent 7746464 commit de7d4e6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
96 changes: 87 additions & 9 deletions frontend/src/app/sample/sample.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
import { ComponentFixture, TestBed } from "@angular/core/testing";

import { SampleComponent } from "./sample.component";
import { RouterModule } from "@angular/router";
import { ActivatedRoute, Router, RouterModule } from "@angular/router";
import { routes } from "../routes";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import {
HttpClientTestingModule,
HttpTestingController,
} from "@angular/common/http/testing";
import {
AethelDetail,
AethelDetailError,
LexicalPhrase,
} from "../shared/types";
import { By } from "@angular/platform-browser";
import { ProofPipe } from "../shared/pipes/proof.pipe";


describe("SampleComponent", () => {
fdescribe("SampleComponent", () => {
let component: SampleComponent;
let fixture: ComponentFixture<SampleComponent>;
let router: Router;
let controller: HttpTestingController;

const table = () => fixture.debugElement.query(By.css(".table"));

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
RouterModule.forRoot(routes)
imports: [HttpClientTestingModule, RouterModule.forRoot(routes)],
providers: [
{
provide: ActivatedRoute,
useValue: {
snapshot: {
params: {
sampleName: "cheese-tosti",
},
},
},
},
],
declarations: [SampleComponent]
declarations: [SampleComponent, ProofPipe],
}).compileComponents();

fixture = TestBed.createComponent(SampleComponent);
router = TestBed.inject(Router);
controller = TestBed.inject(HttpTestingController);
component = fixture.componentInstance;
fixture.detectChanges();
});
Expand All @@ -28,5 +52,59 @@ describe("SampleComponent", () => {
expect(component).toBeTruthy();
});

it("should construct a valid route", () => { });
it("should construct a valid route", () => {
const spy = spyOn(router, "navigate");
const items: LexicalPhrase["items"] = [
{
lemma: "test",
pos: "2",
pt: "2",
word: "testQuery",
},
];

component.routeToAethel(items);
expect(spy).toHaveBeenCalledOnceWith(["/æthel"], {
queryParams: { query: "testQuery" },
});
});

it("should handle a valid query param", () => {
const validResult: AethelDetail = {
error: null,
result: {
name: "name",
phrases: [],
sentence: "sentence",
subset: "testing",
term: "term",
},
};

const req = controller.expectOne(
"/api/aethel/sample?sample-name=cheese-tosti",
);

req.flush(validResult);
fixture.detectChanges();

expect(table()).toBeTruthy();
controller.verify();
});

it("should handle an invalid query param", () => {
const errorResult: AethelDetail = {
error: AethelDetailError.SAMPLE_NOT_FOUND,
result: null,
};

const req = controller.expectOne(
"/api/aethel/sample?sample-name=cheese-tosti",
);
req.flush(errorResult);
fixture.detectChanges();

expect(table()).toBeFalsy();
controller.verify();
});
});
2 changes: 1 addition & 1 deletion frontend/src/app/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ export interface AethelDetailResult {

export interface AethelDetail {
error: AethelDetailError | null;
result: AethelDetailResult;
result: AethelDetailResult | null;
}

0 comments on commit de7d4e6

Please sign in to comment.