refactor(downloads): use runtime availability capability

This commit is contained in:
4gray committed 2026-05-22 12:57:46 +03:00
1 parent abbce3f928
commit f9e95466e8
2 files changed
+24 -1

No files matched your search

@@ -1,8 +1,11 @@
import { signal, Signal, WritableSignal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import {
DownloadItem,
DownloadsService,
} from './downloads.service';
import { RuntimeCapabilitiesService } from './runtime-capabilities.service';
import { SettingsStore } from './settings-store.service';
type TestDownloadsService = {
downloads: WritableSignal<DownloadItem[]>;
@@ -27,6 +30,7 @@ describe('DownloadsService', () => {
afterEach(() => {
testWindow.electron = originalElectron;
TestBed.resetTestingModule();
jest.restoreAllMocks();
});
@@ -74,6 +78,23 @@ describe('DownloadsService', () => {
return service;
}
it('reports availability through the runtime capability', () => {
TestBed.configureTestingModule({
providers: [
DownloadsService,
{ provide: SettingsStore, useValue: {} },
{
provide: RuntimeCapabilitiesService,
useValue: { supportsDownloads: false },
},
],
});
const service = TestBed.inject(DownloadsService);
expect(service.isAvailable()).toBe(false);
});
it('tracks loading and loaded state around a successful download list request', async () => {
const item = createDownload(1);
const pending = createDeferred<DownloadItem[]>();
+3 -1
View File
@@ -1,4 +1,5 @@
import { computed, inject, Injectable, OnDestroy, signal } from '@angular/core';
import { RuntimeCapabilitiesService } from './runtime-capabilities.service';
import { SettingsStore } from './settings-store.service';
export type DownloadStatus =
@@ -31,6 +32,7 @@ export interface DownloadItem {
@Injectable({ providedIn: 'root' })
export class DownloadsService implements OnDestroy {
private readonly runtime = inject(RuntimeCapabilitiesService);
private readonly settingsStore = inject(SettingsStore);
private unsubscribe?: () => void;
private loadDownloadsRequestId = 0;
@@ -48,7 +50,7 @@ export class DownloadsService implements OnDestroy {
readonly hasLoadedDownloads = this._hasLoadedDownloads.asReadonly();
/** Whether the download feature is available (Electron only) */
readonly isAvailable = computed(() => !!window.electron?.downloadsGetList);
readonly isAvailable = computed(() => this.runtime.supportsDownloads);
/** Whether there are any downloads */
readonly hasDownloads = computed(() => this.downloads().length > 0);