Skip to content

Commit

Permalink
refactor: Remove deprecated code (#1477)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon <[email protected]>
  • Loading branch information
Schottkyc137 and TheSlimvReal authored Oct 20, 2022
1 parent 5501a49 commit 677c7b6
Show file tree
Hide file tree
Showing 22 changed files with 119 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing";

import { ChildBlockComponent } from "./child-block.component";
import { RouterTestingModule } from "@angular/router/testing";
import { of } from "rxjs";
import { SchoolBlockComponent } from "../../schools/school-block/school-block.component";
import { ChildrenService } from "../children.service";
import { Child } from "../model/child";
Expand All @@ -12,22 +11,18 @@ describe("ChildBlockComponent", () => {
let fixture: ComponentFixture<ChildBlockComponent>;
let mockChildrenService: jasmine.SpyObj<ChildrenService>;

beforeEach(
waitForAsync(() => {
mockChildrenService = jasmine.createSpyObj("mockChildrenService", [
"getChild",
]);
mockChildrenService.getChild.and.returnValue(of(new Child("")));
beforeEach(waitForAsync(() => {
mockChildrenService = jasmine.createSpyObj("mockChildrenService", [
"getChild",
]);
mockChildrenService.getChild.and.resolveTo(new Child(""));

TestBed.configureTestingModule({
declarations: [SchoolBlockComponent, ChildBlockComponent],
imports: [RouterTestingModule],
providers: [
{ provide: ChildrenService, useValue: mockChildrenService },
],
}).compileComponents();
})
);
TestBed.configureTestingModule({
declarations: [SchoolBlockComponent, ChildBlockComponent],
imports: [RouterTestingModule],
providers: [{ provide: ChildrenService, useValue: mockChildrenService }],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ChildBlockComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import {
SimpleChange,
SimpleChanges,
} from "@angular/core";
import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy";
import { OnInitDynamicComponent } from "../../../core/view/dynamic-components/on-init-dynamic-component.interface";
import { ChildrenService } from "../children.service";
import { Child } from "../model/child";
import { DynamicComponent } from "../../../core/view/dynamic-components/dynamic-component.decorator";

@UntilDestroy()
@DynamicComponent("ChildBlock")
@Component({
selector: "app-child-block",
Expand All @@ -31,14 +29,9 @@ export class ChildBlockComponent implements OnInitDynamicComponent, OnChanges {

constructor(@Optional() private childrenService: ChildrenService) {}

ngOnChanges(changes: SimpleChanges) {
async ngOnChanges(changes: SimpleChanges) {
if (changes.hasOwnProperty("entityId")) {
this.childrenService
.getChild(this.entityId)
.pipe(untilDestroyed(this))
.subscribe((child) => {
this.entity = child;
});
this.entity = await this.childrenService.getChild(this.entityId);
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/app/child-dev-project/children/children.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { genders } from "./model/genders";
import { DatabaseTestingModule } from "../../utils/database-testing.module";
import { sortByAttribute } from "../../utils/utils";
import { expectEntitiesToMatch } from "../../utils/expect-entity-data.spec";
import { lastValueFrom } from "rxjs";

describe("ChildrenService", () => {
let service: ChildrenService;
Expand Down Expand Up @@ -41,10 +42,10 @@ describe("ChildrenService", () => {
});

it("should list newly saved children", async () => {
const childrenBefore = await service.getChildren().toPromise();
const childrenBefore = await lastValueFrom(service.getChildren());
const child = new Child("10");
await entityMapper.save<Child>(child);
const childrenAfter = await service.getChildren().toPromise();
const childrenAfter = await lastValueFrom(service.getChildren());

let find = childrenBefore.find((c) => c.getId() === child.getId());
expect(find).toBeUndefined();
Expand All @@ -59,14 +60,14 @@ describe("ChildrenService", () => {
const child = new Child("10");
let error;
try {
await service.getChild(child.getId()).toPromise();
await service.getChild(child.getId());
} catch (err) {
error = err;
}
expect(error).toBeDefined();

await entityMapper.save<Child>(child);
const childAfter = await service.getChild(child.getId()).toPromise();
const childAfter = await service.getChild(child.getId());
expect(childAfter).toBeDefined();
expect(childAfter).toHaveId(child.getId());
});
Expand Down Expand Up @@ -109,12 +110,12 @@ describe("ChildrenService", () => {

it("should load a single child and add school info", async () => {
// no active relation
const child2 = await service.getChild("2").toPromise();
const child2 = await service.getChild("2");
expect(child2.schoolClass).toBeUndefined();
expect(child2.schoolId).toBeUndefined();

// one active relation
let child1 = await service.getChild("1").toPromise();
let child1 = await service.getChild("1");
expect(child1.schoolClass).toBe("2");
expect(child1.schoolId).toBe("1");

Expand All @@ -125,7 +126,7 @@ describe("ChildrenService", () => {
newRelation.schoolId = "2";
newRelation.schoolClass = "3";
await entityMapper.save(newRelation);
child1 = await service.getChild(child1.getId()).toPromise();
child1 = await service.getChild(child1.getId());
expect(child1.schoolClass).toBe("3");
expect(child1.schoolId).toBe("2");

Expand All @@ -135,13 +136,13 @@ describe("ChildrenService", () => {
noStartDate.schoolId = "2";
noStartDate.schoolClass = "4";
await entityMapper.save(noStartDate);
child1 = await service.getChild(child1.getId()).toPromise();
child1 = await service.getChild(child1.getId());
expect(child1.schoolClass).toBe("4");
expect(child1.schoolId).toBe("2");
});

it("should load all children with school info", async () => {
const children = await service.getChildren().toPromise();
const children = await lastValueFrom(service.getChildren());
const child1 = children.find((child) => child.getId() === "1");
expect(child1.schoolClass).toBe("2");
expect(child1.schoolId).toBe("1");
Expand Down
18 changes: 7 additions & 11 deletions src/app/child-dev-project/children/children.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ChildPhotoService } from "./child-photo-service/child-photo.service";
import moment, { Moment } from "moment";
import { LoggingService } from "../../core/logging/logging.service";
import { DatabaseIndexingService } from "../../core/entity/database-indexing/database-indexing.service";
import { Entity } from "../../core/entity/model/entity";

@Injectable()
export class ChildrenService {
Expand Down Expand Up @@ -57,17 +56,14 @@ export class ChildrenService {
* returns an observable which retrieves a single child and loads its photo
* @param id id of child
*/
getChild(id: string): Observable<Child> {
const promise = this.entityMapper
.load<Child>(Child, id)
.then((loadedChild) => {
return this.queryLatestRelation(id).then((currentSchoolInfo) => {
loadedChild.schoolClass = currentSchoolInfo?.schoolClass;
loadedChild.schoolId = currentSchoolInfo?.schoolId;
return loadedChild;
});
getChild(id: string): Promise<Child> {
return this.entityMapper.load<Child>(Child, id).then((loadedChild) => {
return this.queryLatestRelation(id).then((currentSchoolInfo) => {
loadedChild.schoolClass = currentSchoolInfo?.schoolClass;
loadedChild.schoolId = currentSchoolInfo?.schoolId;
return loadedChild;
});
return from(promise);
});
}

private createChildSchoolRelationIndex(): Promise<any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ describe("HealthCheckupComponent", () => {
let mockChildrenService: jasmine.SpyObj<ChildrenService>;
const child = new Child();

beforeEach(
waitForAsync(() => {
mockChildrenService = jasmine.createSpyObj([
"getChild",
"getHealthChecksOfChild",
]);
mockChildrenService.getChild.and.returnValue(of(child));
mockChildrenService.getHealthChecksOfChild.and.returnValue(of([]));
beforeEach(waitForAsync(() => {
mockChildrenService = jasmine.createSpyObj([
"getChild",
"getHealthChecksOfChild",
]);
mockChildrenService.getChild.and.resolveTo(child);
mockChildrenService.getHealthChecksOfChild.and.returnValue(of([]));

TestBed.configureTestingModule({
imports: [ChildrenModule, MockedTestingModule.withState()],
providers: [
{ provide: ChildrenService, useValue: mockChildrenService },
],
}).compileComponents();
})
);
TestBed.configureTestingModule({
imports: [ChildrenModule, MockedTestingModule.withState()],
providers: [{ provide: ChildrenService, useValue: mockChildrenService }],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HealthCheckupComponent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NoteDetailsComponent } from "./note-details.component";
import { Note } from "../model/note";
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { of } from "rxjs";
import { EMPTY } from "rxjs";
import { ChildrenService } from "../../children/children.service";
import { NotesModule } from "../notes.module";
import { Child } from "../../children/model/child";
Expand Down Expand Up @@ -44,9 +44,9 @@ describe("NoteDetailsComponent", () => {
const mockChildrenService = jasmine.createSpyObj("mockChildrenService", [
"getChild",
]);
mockChildrenService.getChild.and.returnValue(of(new Child("")));
mockChildrenService.getChild.and.resolveTo(new Child(""));

const dialogRefMock = { beforeClosed: () => of(), close: () => {} };
const dialogRefMock = { beforeClosed: () => EMPTY, close: () => {} };

TestBed.configureTestingModule({
imports: [
Expand Down
3 changes: 2 additions & 1 deletion src/app/core/admin/services/child-photo-update.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EntityMapperService } from "../../entity/entity-mapper.service";
import { HttpClient } from "@angular/common/http";
import { Child } from "../../../child-dev-project/children/model/child";
import { ChildPhotoService } from "../../../child-dev-project/children/child-photo-service/child-photo.service";
import { firstValueFrom } from "rxjs";

/**
* Utility service to automatically detect and update filenames for Child entities' photos.
Expand Down Expand Up @@ -56,7 +57,7 @@ export class ChildPhotoUpdateService {

private async checkIfFileExists(filename): Promise<boolean> {
try {
await this.httpClient.get(filename).toPromise();
await firstValueFrom(this.httpClient.get(filename));
return true;
} catch (e) {
return e.status === 200;
Expand Down
5 changes: 2 additions & 3 deletions src/app/core/config/config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { fakeAsync, TestBed, tick } from "@angular/core/testing";
import { ConfigService } from "./config.service";
import { EntityMapperService } from "../entity/entity-mapper.service";
import { Config } from "./config";
import { Subject } from "rxjs";
import { firstValueFrom, Subject } from "rxjs";
import { UpdatedEntity } from "../entity/model/entity-update";
import { take } from "rxjs/operators";

describe("ConfigService", () => {
let service: ConfigService;
Expand Down Expand Up @@ -41,7 +40,7 @@ describe("ConfigService", () => {

it("should emit the config once it is loaded", fakeAsync(() => {
entityMapper.load.and.rejectWith("No config found");
const configLoaded = service.configUpdates.pipe(take(1)).toPromise();
const configLoaded = firstValueFrom(service.configUpdates);

service.loadConfig();
tick();
Expand Down
6 changes: 4 additions & 2 deletions src/app/core/database/pouch-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import PouchDB from "pouchdb-browser";
import memory from "pouchdb-adapter-memory";
import { PerformanceAnalysisLogging } from "../../utils/performance-analysis-logging";
import { Injectable } from "@angular/core";
import { Observable, Subject } from "rxjs";
import { firstValueFrom, Observable, Subject } from "rxjs";
import { filter } from "rxjs/operators";

/**
Expand Down Expand Up @@ -97,7 +97,9 @@ export class PouchDatabase extends Database {
}

async getPouchDBOnceReady(): Promise<PouchDB.Database> {
await this.databaseInitialized.toPromise();
await firstValueFrom(this.databaseInitialized, {
defaultValue: this.pouchDB,
});
return this.pouchDB;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Database } from "../../database/database";
import { EntitySchemaService } from "../schema/entity-schema.service";
import { expectObservable } from "../../../utils/test-utils/observable-utils";
import { fakeAsync, tick } from "@angular/core/testing";
import { take } from "rxjs/operators";
import { firstValueFrom } from "rxjs";

describe("DatabaseIndexingService", () => {
let service: DatabaseIndexingService;
Expand Down Expand Up @@ -139,17 +139,13 @@ describe("DatabaseIndexingService", () => {
};

await service.createIndex(testDesignDoc);
let registeredIndices = await service.indicesRegistered
.pipe(take(1))
.toPromise();
let registeredIndices = await firstValueFrom(service.indicesRegistered);
expect(registeredIndices).toEqual([
jasmine.objectContaining({ details: "test-index" }),
]);

await service.createIndex(testDesignDoc);
registeredIndices = await service.indicesRegistered
.pipe(take(1))
.toPromise();
registeredIndices = await firstValueFrom(service.indicesRegistered);
expect(registeredIndices).toEqual([
jasmine.objectContaining({ details: "test-index" }),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { Injectable } from "@angular/core";
import { Database, QueryOptions } from "../../database/database";
import { BehaviorSubject, Observable } from "rxjs";
import { BehaviorSubject, firstValueFrom, Observable } from "rxjs";
import { BackgroundProcessState } from "../../sync-status/background-process-state.interface";
import { Entity, EntityConstructor } from "../model/entity";
import { EntitySchemaService } from "../schema/entity-schema.service";
Expand Down Expand Up @@ -176,8 +176,10 @@ export class DatabaseIndexingService {
return;
}

await this._indicesRegistered
.pipe(first((processes) => relevantIndexIsReady(processes, indexName)))
.toPromise();
await firstValueFrom(
this._indicesRegistered.pipe(
first((processes) => relevantIndexIsReady(processes, indexName))
)
);
}
}
Loading

0 comments on commit 677c7b6

Please sign in to comment.