mirror of
https://github.com/4gray/iptvnator.git
synced 2026-10-14 05:06:16 -08:00
refactor: delete all playlists feature
- Updated confirmation dialog messages to provide detailed consequences of deleting playlists. - Added backup hints and progress messages for better user experience during playlist deletion. - Ensured consistency in language across all supported translations (de, el, en, es, fr, it, ja, ko, nl, pl, pt, ru, tr, zh, zhtw). Entire-Checkpoint: c6e522b4276c
This commit is contained in:
1 parent
d7114c1580
commit
fc3db71e85
27 files changed
+1140
-233
No files matched your search
@@ -8,6 +8,7 @@ import {
|
||||
import { MatCardModule } from '@angular/material/card';
|
||||
import { MatCheckboxModule } from '@angular/material/checkbox';
|
||||
import { MatDividerModule } from '@angular/material/divider';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatListModule } from '@angular/material/list';
|
||||
@@ -17,14 +18,20 @@ import { MatTooltipModule } from '@angular/material/tooltip';
|
||||
import { Router } from '@angular/router';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { provideMockStore } from '@ngrx/store/testing';
|
||||
import { MockStore, provideMockStore } from '@ngrx/store/testing';
|
||||
import { TranslateModule, TranslateService } from '@ngx-translate/core';
|
||||
import { EpgService } from '@iptvnator/epg/data-access';
|
||||
import { MockModule, MockProvider } from 'ng-mocks';
|
||||
import { DialogService } from 'components';
|
||||
import { DataService, PlaylistBackupService, PlaylistsService } from 'services';
|
||||
import {
|
||||
DatabaseService,
|
||||
DataService,
|
||||
PlaylistBackupService,
|
||||
PlaylistsService,
|
||||
} from 'services';
|
||||
import {
|
||||
Language,
|
||||
PlaylistMeta,
|
||||
StartupBehavior,
|
||||
StreamFormat,
|
||||
Theme,
|
||||
@@ -34,12 +41,16 @@ import { SettingsComponent } from './settings.component';
|
||||
|
||||
import { signal } from '@angular/core';
|
||||
import { NgxIndexedDBService } from 'ngx-indexed-db';
|
||||
import { from, of, Subject } from 'rxjs';
|
||||
import { from, of } from 'rxjs';
|
||||
import { ElectronServiceStub } from '../services/electron.service.stub';
|
||||
import { SettingsStore } from '../services/settings-store.service';
|
||||
import { SettingsService } from '../services/settings.service';
|
||||
import { SettingsContextService } from '@iptvnator/workspace/shell/util';
|
||||
import { PlaylistActions } from 'm3u-state';
|
||||
import {
|
||||
PlaylistActions,
|
||||
selectAllPlaylistsMeta,
|
||||
selectIsEpgAvailable,
|
||||
} from 'm3u-state';
|
||||
|
||||
class MatSnackBarStub {
|
||||
open = jest.fn();
|
||||
@@ -110,8 +121,22 @@ describe('SettingsComponent', () => {
|
||||
let playlistsService: PlaylistsService;
|
||||
let playlistBackupService: PlaylistBackupService;
|
||||
let store: Store;
|
||||
let mockStore: MockStore;
|
||||
let databaseService: DatabaseService;
|
||||
let snackBar: MatSnackBarStub;
|
||||
const originalElectron = window.electron;
|
||||
const importDate = '2026-04-21T00:00:00.000Z';
|
||||
|
||||
const createPlaylistMeta = (
|
||||
overrides: Partial<PlaylistMeta> = {}
|
||||
): PlaylistMeta => ({
|
||||
_id: overrides._id ?? 'playlist-id',
|
||||
title: overrides.title ?? 'Playlist',
|
||||
count: overrides.count ?? 10,
|
||||
importDate: overrides.importDate ?? importDate,
|
||||
autoRefresh: overrides.autoRefresh ?? false,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
beforeEach(waitForAsync(() => {
|
||||
TestBed.configureTestingModule({
|
||||
@@ -124,6 +149,9 @@ describe('SettingsComponent', () => {
|
||||
MockProvider(DialogService, {
|
||||
openConfirmDialog: jest.fn(),
|
||||
}),
|
||||
MockProvider(MatDialog, {
|
||||
open: jest.fn(),
|
||||
}),
|
||||
{ provide: SettingsService, useClass: MockSettingsService },
|
||||
{ provide: MatSnackBar, useClass: MatSnackBarStub },
|
||||
{ provide: DataService, useClass: ElectronServiceStub },
|
||||
@@ -131,7 +159,12 @@ describe('SettingsComponent', () => {
|
||||
provide: Router,
|
||||
useClass: MockRouter,
|
||||
},
|
||||
provideMockStore(),
|
||||
provideMockStore({
|
||||
selectors: [
|
||||
{ selector: selectAllPlaylistsMeta, value: [] },
|
||||
{ selector: selectIsEpgAvailable, value: false },
|
||||
],
|
||||
}),
|
||||
{
|
||||
provide: NgxIndexedDBService,
|
||||
useValue: {},
|
||||
@@ -140,6 +173,12 @@ describe('SettingsComponent', () => {
|
||||
getAllData: jest.fn().mockReturnValue(of([])),
|
||||
removeAll: jest.fn(),
|
||||
}),
|
||||
MockProvider(DatabaseService, {
|
||||
createOperationId: jest
|
||||
.fn()
|
||||
.mockReturnValue('delete-all-op'),
|
||||
deleteAllPlaylists: jest.fn().mockResolvedValue(true),
|
||||
}),
|
||||
MockProvider(PlaylistBackupService, {
|
||||
exportBackup: jest.fn().mockResolvedValue({
|
||||
defaultFileName:
|
||||
@@ -209,9 +248,15 @@ describe('SettingsComponent', () => {
|
||||
playlistsService = TestBed.inject(PlaylistsService);
|
||||
playlistBackupService = TestBed.inject(PlaylistBackupService);
|
||||
store = TestBed.inject(Store);
|
||||
mockStore = TestBed.inject(MockStore);
|
||||
databaseService = TestBed.inject(DatabaseService);
|
||||
snackBar = TestBed.inject(MatSnackBar) as unknown as MatSnackBarStub;
|
||||
|
||||
component = fixture.componentInstance;
|
||||
component.checkAppVersion = jest.fn();
|
||||
component.fetchLocalIpAddresses = jest
|
||||
.fn()
|
||||
.mockResolvedValue(undefined);
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
@@ -219,6 +264,12 @@ describe('SettingsComponent', () => {
|
||||
window.electron = originalElectron;
|
||||
});
|
||||
|
||||
function setPlaylists(playlists: PlaylistMeta[]): void {
|
||||
mockStore.overrideSelector(selectAllPlaylistsMeta, playlists);
|
||||
mockStore.refreshState();
|
||||
fixture.detectChanges();
|
||||
}
|
||||
|
||||
it('should create and init component', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
@@ -397,30 +448,129 @@ describe('SettingsComponent', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('removes all playlists with a busy state and dispatches store cleanup after success', async () => {
|
||||
const removal$ = new Subject<void>();
|
||||
const dispatchSpy = jest.spyOn(store, 'dispatch');
|
||||
it('disables the global wipe action when there are no playlists', () => {
|
||||
setPlaylists([]);
|
||||
|
||||
(dialogService.openConfirmDialog as jest.Mock).mockImplementation(
|
||||
({ onConfirm }: { onConfirm: () => Promise<void> }) => {
|
||||
void onConfirm();
|
||||
}
|
||||
);
|
||||
(playlistsService.removeAll as jest.Mock).mockReturnValue(
|
||||
removal$.asObservable()
|
||||
);
|
||||
jest.spyOn(translate, 'instant').mockImplementation((key) => key);
|
||||
const deleteButton = (
|
||||
fixture.nativeElement as HTMLElement
|
||||
).querySelector('.danger-zone__button') as HTMLButtonElement | null;
|
||||
|
||||
expect(component.canRemoveAllPlaylists()).toBe(false);
|
||||
expect(deleteButton?.disabled).toBe(true);
|
||||
});
|
||||
|
||||
it('opens the dedicated delete-all dialog with the current playlist type summary', () => {
|
||||
setPlaylists([
|
||||
createPlaylistMeta({ _id: 'm3u-1' }),
|
||||
createPlaylistMeta({
|
||||
_id: 'xtream-1',
|
||||
serverUrl: 'http://xtream.example',
|
||||
}),
|
||||
createPlaylistMeta({
|
||||
_id: 'stalker-1',
|
||||
macAddress: '00:11:22:33:44:55',
|
||||
}),
|
||||
]);
|
||||
|
||||
const openSpy = jest
|
||||
.spyOn((component as any).matDialog, 'open')
|
||||
.mockReturnValue({
|
||||
afterClosed: () => of(false),
|
||||
} as any);
|
||||
|
||||
component.removeAll();
|
||||
|
||||
expect(component.isRemovingAllPlaylists()).toBe(true);
|
||||
expect(playlistsService.removeAll).toHaveBeenCalled();
|
||||
expect(component.playlistDeleteSummary()).toEqual({
|
||||
total: 3,
|
||||
m3u: 1,
|
||||
xtream: 1,
|
||||
stalker: 1,
|
||||
});
|
||||
expect(openSpy).toHaveBeenCalledWith(
|
||||
expect.any(Function),
|
||||
expect.objectContaining({
|
||||
data: {
|
||||
summary: {
|
||||
total: 3,
|
||||
m3u: 1,
|
||||
xtream: 1,
|
||||
stalker: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
removal$.next();
|
||||
removal$.complete();
|
||||
it('tracks Electron delete-all progress and dispatches store cleanup after success', async () => {
|
||||
setPlaylists([
|
||||
createPlaylistMeta({ _id: 'm3u-1' }),
|
||||
createPlaylistMeta({
|
||||
_id: 'xtream-1',
|
||||
serverUrl: 'http://xtream.example',
|
||||
}),
|
||||
]);
|
||||
|
||||
const dispatchSpy = jest.spyOn(store, 'dispatch');
|
||||
(databaseService.deleteAllPlaylists as jest.Mock).mockClear();
|
||||
jest.spyOn((component as any).matDialog, 'open').mockReturnValue({
|
||||
afterClosed: () => of(true),
|
||||
} as any);
|
||||
jest.spyOn(translate, 'instant').mockImplementation(
|
||||
(key: string, params?: Record<string, number>) => {
|
||||
if (key === 'SETTINGS.REMOVE_ALL_PROGRESS') {
|
||||
return `${params?.current}/${params?.total}`;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
);
|
||||
jest.spyOn(
|
||||
component as any,
|
||||
'waitForUiFeedbackFrame'
|
||||
).mockResolvedValue(undefined);
|
||||
|
||||
let resolveDelete: (value: boolean) => void = () => undefined;
|
||||
(databaseService.deleteAllPlaylists as jest.Mock).mockImplementation(
|
||||
({
|
||||
onEvent,
|
||||
}: {
|
||||
onEvent?: (event: {
|
||||
operation: string;
|
||||
status: string;
|
||||
current?: number;
|
||||
total?: number;
|
||||
}) => void;
|
||||
}) => {
|
||||
onEvent?.({
|
||||
operation: 'delete-all-playlists',
|
||||
status: 'progress',
|
||||
current: 3,
|
||||
total: 7,
|
||||
});
|
||||
|
||||
return new Promise<boolean>((resolve) => {
|
||||
resolveDelete = resolve;
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
component.removeAll();
|
||||
await Promise.resolve();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.isRemovingAllPlaylists()).toBe(true);
|
||||
expect(component.removeAllProgressLabel()).toBe('3/7');
|
||||
expect(databaseService.deleteAllPlaylists).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operationId: 'delete-all-op',
|
||||
onEvent: expect.any(Function),
|
||||
})
|
||||
);
|
||||
|
||||
resolveDelete(true);
|
||||
await fixture.whenStable();
|
||||
|
||||
expect(component.isRemovingAllPlaylists()).toBe(false);
|
||||
expect(component.removeAllProgress()).toBeNull();
|
||||
expect(dispatchSpy).toHaveBeenCalledWith(
|
||||
PlaylistActions.removeAllPlaylists()
|
||||
);
|
||||
@@ -436,6 +586,50 @@ describe('SettingsComponent', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to PlaylistsService.removeAll outside Electron', async () => {
|
||||
fixture.destroy();
|
||||
window.electron = undefined as unknown as typeof window.electron;
|
||||
|
||||
const browserFixture = TestBed.createComponent(SettingsComponent);
|
||||
const browserComponent = browserFixture.componentInstance;
|
||||
browserComponent.checkAppVersion = jest.fn();
|
||||
browserComponent.fetchLocalIpAddresses = jest
|
||||
.fn()
|
||||
.mockResolvedValue(undefined);
|
||||
browserFixture.detectChanges();
|
||||
|
||||
mockStore.overrideSelector(selectAllPlaylistsMeta, [
|
||||
createPlaylistMeta({ _id: 'browser-m3u' }),
|
||||
]);
|
||||
mockStore.refreshState();
|
||||
browserFixture.detectChanges();
|
||||
|
||||
jest.spyOn((browserComponent as any).matDialog, 'open').mockReturnValue(
|
||||
{
|
||||
afterClosed: () => of(true),
|
||||
} as any
|
||||
);
|
||||
jest.spyOn(
|
||||
browserComponent as any,
|
||||
'waitForUiFeedbackFrame'
|
||||
).mockResolvedValue(undefined);
|
||||
(databaseService.deleteAllPlaylists as jest.Mock).mockClear();
|
||||
(playlistsService.removeAll as jest.Mock).mockClear();
|
||||
(playlistsService.removeAll as jest.Mock).mockReturnValue(
|
||||
of(undefined)
|
||||
);
|
||||
const dispatchSpy = jest.spyOn(store, 'dispatch');
|
||||
|
||||
browserComponent.removeAll();
|
||||
await browserFixture.whenStable();
|
||||
|
||||
expect(playlistsService.removeAll).toHaveBeenCalled();
|
||||
expect(databaseService.deleteAllPlaylists).not.toHaveBeenCalled();
|
||||
expect(dispatchSpy).toHaveBeenCalledWith(
|
||||
PlaylistActions.removeAllPlaylists()
|
||||
);
|
||||
});
|
||||
|
||||
it('shows the save confirmation snackbar at the bottom center with the settings offset class', () => {
|
||||
jest.spyOn(translate, 'instant').mockReturnValue('Settings saved');
|
||||
|
||||
@@ -493,23 +687,19 @@ describe('SettingsComponent', () => {
|
||||
});
|
||||
|
||||
it('shows an export busy state until the backup file has been written', async () => {
|
||||
let resolveExport: (
|
||||
value: {
|
||||
defaultFileName: string;
|
||||
json: string;
|
||||
manifest: {
|
||||
kind: string;
|
||||
version: number;
|
||||
exportedAt: string;
|
||||
includeSecrets: boolean;
|
||||
playlists: never[];
|
||||
};
|
||||
}
|
||||
) => void = () => undefined;
|
||||
let resolveExport: (value: {
|
||||
defaultFileName: string;
|
||||
json: string;
|
||||
manifest: {
|
||||
kind: string;
|
||||
version: number;
|
||||
exportedAt: string;
|
||||
includeSecrets: boolean;
|
||||
playlists: never[];
|
||||
};
|
||||
}) => void = () => undefined;
|
||||
|
||||
(
|
||||
playlistBackupService.exportBackup as jest.Mock
|
||||
).mockReturnValueOnce(
|
||||
(playlistBackupService.exportBackup as jest.Mock).mockReturnValueOnce(
|
||||
new Promise((resolve) => {
|
||||
resolveExport = resolve;
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user