Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderVertegaal committed Aug 13, 2024
1 parent e7099d4 commit 703f350
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
18 changes: 14 additions & 4 deletions frontend/src/app/aethel/aethel.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { ReactiveFormsModule } from "@angular/forms";
import { ActivatedRoute, Router, RouterModule } from "@angular/router";
import { routes } from "../routes";
import { of } from "rxjs";
import { AethelApiService } from "../shared/services/aethel-api.service";

describe("AethelComponent", () => {
let component: AethelComponent;
let fixture: ComponentFixture<AethelComponent>;
let apiService: AethelApiService;
let httpController: HttpTestingController;
let route: ActivatedRoute;
let router: Router;
Expand All @@ -28,6 +30,7 @@ describe("AethelComponent", () => {
route = TestBed.inject(ActivatedRoute);
router = TestBed.inject(Router);
fixture = TestBed.createComponent(AethelComponent);
apiService = TestBed.inject(AethelApiService);
component = fixture.componentInstance;
fixture.detectChanges();
});
Expand All @@ -43,17 +46,24 @@ describe("AethelComponent", () => {
httpController.expectNone("/api/aethel");
});

it("should request data when there is a query parameter on init", () => {
route.queryParams = of({ query: "test" });
it("should insert data into the form when there is a 'word' query parameter", () => {
route.queryParams = of({ word: "test" });
component.ngOnInit();
expect(component.form.controls.aethelInput.value).toBe("test");
httpController.expectOne("/api/aethel/?query=test");
});

it("should pass query param data to the API service", () => {
apiService.input$.subscribe(input => {
expect(input.word).toBe("test3");
});
route.queryParams = of({ word: "test3" });
component.ngOnInit();
});

it("should react to form submissions", () => {
const navigatorSpy = spyOn(router, "navigateByUrl");
component.form.controls.aethelInput.setValue("test-two");
component.submit();
expect(navigatorSpy).toHaveBeenCalledWith("/?query=test-two");
expect(navigatorSpy).toHaveBeenCalledWith("/?word=test-two");
});
});
47 changes: 35 additions & 12 deletions frontend/src/app/sample/sample.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ import { By } from "@angular/platform-browser";
import { ProofPipe } from "../shared/pipes/proof.pipe";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";

const fakePhrase: LexicalPhrase = {
type: "cheese->tosti",
items: [
{
word: "cheeses",
lemma: "tostis",
pos: "TOSTI",
pt: "CHEESE",
},
],
};

describe("SampleComponent", () => {
let component: SampleComponent;
let fixture: ComponentFixture<SampleComponent>;
Expand All @@ -27,7 +39,11 @@ describe("SampleComponent", () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [SampleComponent, ProofPipe],
imports: [HttpClientTestingModule, FontAwesomeModule, RouterModule.forRoot(routes)],
imports: [
HttpClientTestingModule,
FontAwesomeModule,
RouterModule.forRoot(routes),
],
providers: [
{
provide: ActivatedRoute,
Expand All @@ -53,20 +69,27 @@ describe("SampleComponent", () => {
expect(component).toBeTruthy();
});

it("should construct a valid route", () => {
it("should construct a valid route for word search", () => {
const spy = spyOn(router, "navigate");
const items: LexicalPhrase["items"] = [
{
lemma: "test",
pos: "2",
pt: "2",
word: "testQuery",
},
];
component.searchAethel(fakePhrase, 'word');
expect(spy).toHaveBeenCalledOnceWith(["/aethel"], {
queryParams: { word: "cheeses" },
});
});

component.routeToAethel(items);
it("should construct a valid route for type search", () => {
const spy = spyOn(router, "navigate");
component.searchAethel(fakePhrase, 'type');
expect(spy).toHaveBeenCalledOnceWith(["/aethel"], {
queryParams: { type: "cheese->tosti" },
});
});

it("should construct a valid route for word and type search", () => {
const spy = spyOn(router, "navigate");
component.searchAethel(fakePhrase, 'word-and-type');
expect(spy).toHaveBeenCalledOnceWith(["/aethel"], {
queryParams: { query: "testQuery" },
queryParams: { word: "cheeses", type: "cheese->tosti" },
});
});

Expand Down
9 changes: 3 additions & 6 deletions frontend/src/app/shared/services/aethel-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ export class AethelApiService
"Content-Type": "application/json",
});


let params = new HttpParams()

let params = new HttpParams();
if (input.word) {
params = params.set('word', input.word)
params = params.set("word", input.word);
}

if (input.type) {
params = params.set('type', input.type)
params = params.set("type", input.type);
}

return this.http
Expand Down

0 comments on commit 703f350

Please sign in to comment.