Skip to content

Commit

Permalink
Disable buttons when offline
Browse files Browse the repository at this point in the history
  • Loading branch information
pmachapman committed Apr 21, 2024
1 parent 4e75f2f commit 67b3118
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ <h1>{{ projectName }}</h1>
<a mat-raised-button color="primary" [appRouterLink]="'/serval-administration'">Back to Projects</a>

<h2>Pre-Translation Configuration</h2>
<mat-checkbox [checked]="preTranslate" (change)="onUpdatePreTranslate($event.checked)">
<mat-checkbox [checked]="preTranslate" (change)="onUpdatePreTranslate($event.checked)" [disabled]="!isOnline">
Pre-Translation Drafting Enabled
</mat-checkbox>

Expand All @@ -23,7 +23,12 @@ <h2>Downloads</h2>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let row">
<button mat-raised-button color="primary" (click)="downloadProject(row['id'], row['fileName'])">
<button
mat-raised-button
color="primary"
(click)="downloadProject(row['id'], row['fileName'])"
[disabled]="!isOnline"
>
Download
</button>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { createTestProjectProfile } from 'realtime-server/lib/esm/scriptureforge
import { BehaviorSubject } from 'rxjs';
import { mock, verify, when } from 'ts-mockito';
import { ActivatedProjectService } from 'xforge-common/activated-project.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
import { TestOnlineStatusModule } from 'xforge-common/test-online-status.module';
import { TestOnlineStatusService } from 'xforge-common/test-online-status.service';
import { TestRealtimeModule } from 'xforge-common/test-realtime.module';
import { configureTestingModule, TestTranslocoModule } from 'xforge-common/test-utils';
import { SFProjectProfileDoc } from '../core/models/sf-project-profile-doc';
Expand All @@ -22,39 +25,63 @@ const mockServalAdministrationService = mock(ServalAdministrationService);
describe('ServalProjectComponent', () => {
configureTestingModule(() => ({
imports: [
ServalProjectComponent,
HttpClientTestingModule,
NoopAnimationsModule,
TestTranslocoModule,
ServalProjectComponent,
TestOnlineStatusModule.forRoot(),
TestRealtimeModule.forRoot(SF_TYPE_REGISTRY),
HttpClientTestingModule
TestTranslocoModule
],
providers: [
{ provide: ActivatedProjectService, useMock: mockActivatedProjectService },
{ provide: ActivatedRoute, useMock: mockActivatedRoute },
{ provide: OnlineStatusService, useClass: TestOnlineStatusService },
{ provide: ServalAdministrationService, useMock: mockServalAdministrationService },
{ provide: SFProjectService, useMock: mockSFProjectService }
]
}));

it('should allow enabling pre-translation drafting', fakeAsync(() => {
const env = new TestEnvironment(false);
expect(env.preTranslateCheckbox.checked).toBeFalsy();
expect(env.preTranslateCheckbox.checked).toBe(false);
env.clickElement(env.preTranslateCheckbox);
expect(env.preTranslateCheckbox.checked).toBeTruthy();
expect(env.preTranslateCheckbox.checked).toBe(true);
verify(mockSFProjectService.onlineSetPreTranslate(env.mockProjectId, true)).once();
}));

it('should allow disabling pre-translation drafting', fakeAsync(() => {
const env = new TestEnvironment(true);
expect(env.preTranslateCheckbox.checked).toBeTruthy();
expect(env.preTranslateCheckbox.checked).toBe(true);
env.clickElement(env.preTranslateCheckbox);
expect(env.preTranslateCheckbox.checked).toBeFalsy();
expect(env.preTranslateCheckbox.checked).toBe(false);
verify(mockSFProjectService.onlineSetPreTranslate(env.mockProjectId, false)).once();
}));

it('should disable the pre-translation drafting checkbox when offline', fakeAsync(() => {
const env = new TestEnvironment(true);
env.onlineStatus = false;
expect(env.preTranslateCheckbox.disabled).toBe(true);
}));

it('should disable the download button when offline', fakeAsync(() => {
const env = new TestEnvironment(true);
env.onlineStatus = false;
expect(env.firstDownloadButton.innerText).toBe('Download');
expect(env.firstDownloadButton.disabled).toBe(true);
}));

it('should have a download button', fakeAsync(() => {
const env = new TestEnvironment(true);
expect(env.firstDownloadButton.innerText).toBe('Download');
expect(env.firstDownloadButton.disabled).toBe(false);
}));

class TestEnvironment {
readonly component: ServalProjectComponent;
readonly fixture: ComponentFixture<ServalProjectComponent>;
readonly testOnlineStatusService: TestOnlineStatusService = TestBed.inject(
OnlineStatusService
) as TestOnlineStatusService;

mockProjectId = 'project01';

Expand Down Expand Up @@ -106,6 +133,16 @@ describe('ServalProjectComponent', () => {
return this.fixture.nativeElement.querySelector('mat-checkbox input');
}

get firstDownloadButton(): HTMLInputElement {
return this.fixture.nativeElement.querySelector('td button');
}

set onlineStatus(hasConnection: boolean) {
this.testOnlineStatusService.setIsOnline(hasConnection);
tick();
this.fixture.detectChanges();
}

clickElement(button: HTMLElement): void {
button.click();
this.fixture.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { catchError, lastValueFrom, tap, throwError } from 'rxjs';
import { ActivatedProjectService } from 'xforge-common/activated-project.service';
import { DataLoadingComponent } from 'xforge-common/data-loading-component';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
import { UICommonModule } from 'xforge-common/ui-common.module';
import { filterNullish } from 'xforge-common/util/rxjs-util';
import { SFProjectService } from '../core/sf-project.service';
Expand Down Expand Up @@ -36,12 +37,17 @@ export class ServalProjectComponent extends DataLoadingComponent implements OnIn
constructor(
private readonly activatedProjectService: ActivatedProjectService,
noticeService: NoticeService,
private readonly onlineStatusService: OnlineStatusService,
private readonly projectService: SFProjectService,
private readonly servalAdministrationService: ServalAdministrationService
) {
super(noticeService);
}

get isOnline(): boolean {
return this.onlineStatusService.isOnline;
}

ngOnInit(): void {
this.subscribe(
this.activatedProjectService.projectDoc$.pipe(
Expand Down

0 comments on commit 67b3118

Please sign in to comment.