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

fix(*): prevent conflicts in progress dashboard component #1288

Merged
merged 4 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ import { ProgressDashboardWidgetModule } from "../progress-dashboard-widget.modu
import { FontAwesomeTestingModule } from "@fortawesome/angular-fontawesome/testing";
import { ProgressDashboardConfig } from "./progress-dashboard-config";
import { MatDialog } from "@angular/material/dialog";
import { Subject } from "rxjs";
import { BehaviorSubject, Subject } from "rxjs";
import { take } from "rxjs/operators";
import { SessionService } from "../../../core/session/session-service/session.service";
import { SyncState } from "../../../core/session/session-states/sync-state.enum";
import { MockedTestingModule } from "../../../utils/mocked-testing.module";

describe("ProgressDashboardComponent", () => {
let component: ProgressDashboardComponent;
let fixture: ComponentFixture<ProgressDashboardComponent>;
let mockEntityMapper: jasmine.SpyObj<EntityMapperService>;
const mockDialog = jasmine.createSpyObj<MatDialog>("matDialog", ["open"]);
let mockSession: jasmine.SpyObj<SessionService>;
let mockSync: BehaviorSubject<SyncState>;

beforeEach(
waitForAsync(() => {
Expand All @@ -31,6 +35,8 @@ describe("ProgressDashboardComponent", () => {
]);
mockEntityMapper.load.and.resolveTo({ title: "test", parts: [] } as any);
mockEntityMapper.save.and.resolveTo();
mockSync = new BehaviorSubject(SyncState.UNSYNCED);
mockSession = jasmine.createSpyObj([], { syncState: mockSync });

TestBed.configureTestingModule({
imports: [
Expand All @@ -41,6 +47,7 @@ describe("ProgressDashboardComponent", () => {
providers: [
{ provide: EntityMapperService, useValue: mockEntityMapper },
{ provide: MatDialog, useValue: mockDialog },
{ provide: SessionService, useValue: mockSession },
{
provide: AlertService,
useValue: jasmine.createSpyObj([
Expand Down Expand Up @@ -76,8 +83,22 @@ describe("ProgressDashboardComponent", () => {
);
}));

it("should create a new progress dashboard config if no configuration could be found", fakeAsync(() => {
mockEntityMapper.load.and.rejectWith({ status: 404 });
it("should retry loading the config after sync has finished", fakeAsync(() => {
mockEntityMapper.load.and.rejectWith();
component.onInitFromDynamicConfig({ dashboardConfigId: "someId" });
component.ngOnInit();
tick();

const config = new ProgressDashboardConfig("someId");
mockEntityMapper.load.and.resolveTo(config);
mockSync.next(SyncState.COMPLETED);

expect(component.data).toEqual(config);
}));

it("should create a new progress dashboard config if no configuration could be found after initial sync", fakeAsync(() => {
mockEntityMapper.load.and.rejectWith();
mockSync.next(SyncState.COMPLETED);
const configID = "config-id";

component.onInitFromDynamicConfig({ dashboardConfigId: configID });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { LoggingService } from "../../../core/logging/logging.service";
import { MatDialog } from "@angular/material/dialog";
import { EditProgressDashboardComponent } from "../edit-progress-dashboard/edit-progress-dashboard.component";
import { DynamicComponent } from "../../../core/view/dynamic-components/dynamic-component.decorator";
import { SessionService } from "../../../core/session/session-service/session.service";
import { waitForChangeTo } from "../../../core/session/session-states/session-utils";
import { SyncState } from "../../../core/session/session-states/sync-state.enum";

@Component({
selector: "app-progress-dashboard",
Expand All @@ -21,35 +24,35 @@ export class ProgressDashboardComponent
constructor(
private entityMapper: EntityMapperService,
private loggingService: LoggingService,
private dialog: MatDialog
private dialog: MatDialog,
private sessionService: SessionService
) {}

onInitFromDynamicConfig(config: any) {
this.dashboardConfigId = config.dashboardConfigId;
}

ngOnInit() {
async ngOnInit() {
this.data = new ProgressDashboardConfig(this.dashboardConfigId);
this.entityMapper
this.loadConfigFromDatabase().catch(() =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to wait for the state to be synced altogether?

this.sessionService.syncState
.pipe(waitForChangeTo(SyncState.COMPLETED))
.toPromise()
.then(() => this.loadConfigFromDatabase())
.catch(() => this.createDefaultConfig())
);
}

private loadConfigFromDatabase() {
return this.entityMapper
.load(ProgressDashboardConfig, this.dashboardConfigId)
.then((config) => {
this.data = config;
})
.catch((e) => {
if (e.status === 404) {
this.loggingService.debug(
`ProgressDashboardConfig (${this.dashboardConfigId}) not found. Creating ...`
);
this.createDefaultConfig();
} else {
this.loggingService.warn(
`Error loading ProgressDashboardConfig (${this.dashboardConfigId}): ${e.message}`
);
}
});
.then((config) => (this.data = config));
}

private createDefaultConfig() {
this.loggingService.debug(
`ProgressDashboardConfig (${this.dashboardConfigId}) not found. Creating ...`
);
this.data.title = $localize`:The progress, e.g. of a certain activity:Progress of X`;
this.save();
}
Expand Down