Files
iptvnator/apps/web/src/app/settings/settings-tmdb-section.component.spec.ts
T
4grayandClaude Fable 5 1a6af75761 feat(settings): per-section pages with unsaved-changes bar (#1384)
* feat(settings): split settings into per-section pages with an unsaved-changes bar

Replace the single scrolling settings page with routed section pages
(/workspace/settings/:section): the context-panel rail links each section,
only the active section renders, and unknown or capability-gated sections
redirect to General. The shared form lives on the parent component, so
staged edits survive section switches; a floating unsaved-changes bar
(Save/Discard) replaces the always-visible footer Save button. Rail links
navigate with replaceUrl so Back still leaves settings in one step.

Along the way:
- delete the unreachable settings dialog mode and the dead
  AppPortalNavigationActionsService with both of its never-injected DI
  tokens (PORTAL_NAVIGATION_ACTIONS, PLAYLIST_PLAYER_ACTIONS)
- delete the scroll-spy directive and pendingScrollTarget plumbing
- revive the EPG panel's "Open EPG settings" empty-state button as a deep
  link to /workspace/settings/epg; the M3U player now reports
  m3u-needs-setup only when the channel has no programmes and no EPG
  source exists in settings or on the playlist itself
- load TMDB cache stats when the Metadata page opens (the section
  component now only exists while its page is open)
- add SETTINGS.UNSAVED_CHANGES / SETTINGS.DISCARD_CHANGES to all 19 locales

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(settings): confirm before leaving with unsaved changes

Add settingsUnsavedChangesGuard (canDeactivate on the :section route) with
a three-action dialog: save and leave, leave without saving, keep editing.
The guard only intercepts leaving the settings AREA — section switches
share the one settings form and pass unconditionally, so the dialog can
never nag while moving between pages. A failed save cancels the navigation
instead of silently dropping the edits it promised to keep; leaving
without saving also reverts the live theme preview. Save-and-leave is
disabled while the form is invalid, with a hint explaining why.

New SETTINGS.UNSAVED_DIALOG_* keys in all 19 locales.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(settings): stage cover size and EPG view mode; adapt e2e to section pages

Cover size and EPG view mode were the only two controls that persisted
eagerly on click, which made Discard (and leave-without-saving) unable to
revert them: hydrateFromStore() faithfully reloaded the just-persisted
edit. They now stage in the form like every other setting and reach the
store on Save. Review finding by Greptile (P1) and Codex.

E2E suites that walk through settings are updated for one-section-page
rendering (epg, backup-roundtrip, xtream-epg, remote-control) and for the
staged cover size (downloads asserts the dataset after Save); the EPG icon
fallback test saves before leaving settings so the new unsaved-changes
dialog does not block its navigation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-09 09:34:03 +02:00

152 lines
5.1 KiB
TypeScript

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { TmdbApiService, TmdbCacheService } from '@iptvnator/services';
import { SettingsTmdbSectionComponent } from './settings-tmdb-section.component';
const CACHE_SIZE_LABEL = '{{entries}} entries · {{size}}';
const CACHE_ERROR_LABEL = 'Could not read the cache';
describe('SettingsTmdbSectionComponent', () => {
let fixture: ComponentFixture<SettingsTmdbSectionComponent>;
let getStats: jest.Mock;
let clear: jest.Mock;
const createForm = () =>
new FormGroup({
tmdb: new FormGroup({
enabled: new FormControl(true),
apiKey: new FormControl(''),
}),
});
const queryByTestId = (testId: string): HTMLElement | null =>
fixture.nativeElement.querySelector(`[data-test-id="${testId}"]`);
const clearButton = () =>
queryByTestId('tmdb-clear-cache') as HTMLButtonElement;
/** Constructor kicks off the stats read; let it settle and render. */
const settle = async () => {
await fixture.whenStable();
fixture.detectChanges();
};
/**
* The section component only exists while its settings page is open, so
* "reopening the section" means recreating the component.
*/
const reopenSection = async () => {
fixture.destroy();
fixture = TestBed.createComponent(SettingsTmdbSectionComponent);
fixture.componentRef.setInput('form', createForm());
fixture.detectChanges();
await settle();
};
beforeEach(async () => {
getStats = jest.fn().mockResolvedValue({ entries: 42, bytes: 2048 });
clear = jest.fn().mockResolvedValue(42);
await TestBed.configureTestingModule({
imports: [
SettingsTmdbSectionComponent,
NoopAnimationsModule,
ReactiveFormsModule,
TranslateModule.forRoot(),
],
providers: [
{
provide: TmdbCacheService,
useValue: { getStats, clear },
},
{
provide: TmdbApiService,
useValue: { validateApiKey: jest.fn() },
},
],
}).compileComponents();
const translate = TestBed.inject(TranslateService);
translate.setTranslation(
'en',
{
SETTINGS: {
TMDB_CACHE_SIZE: CACHE_SIZE_LABEL,
TMDB_CACHE_ERROR: CACHE_ERROR_LABEL,
},
},
true
);
translate.use('en');
fixture = TestBed.createComponent(SettingsTmdbSectionComponent);
fixture.componentRef.setInput('form', createForm());
fixture.detectChanges();
});
it('sizes the cache as soon as the section page opens', async () => {
await settle();
expect(getStats).toHaveBeenCalledTimes(1);
expect(queryByTestId('tmdb-cache-size')?.textContent).toContain('42');
});
it('clears the cache and re-reads the size', async () => {
await settle();
getStats.mockResolvedValue({ entries: 0, bytes: 0 });
clearButton().click();
await fixture.whenStable();
fixture.detectChanges();
expect(clear).toHaveBeenCalledTimes(1);
expect(getStats).toHaveBeenCalledTimes(2);
// Nothing left to clear — the button stops offering it
expect(clearButton().disabled).toBe(true);
});
it('surfaces a failed clear instead of claiming an empty cache', async () => {
await settle();
clear.mockResolvedValue(null);
clearButton().click();
await fixture.whenStable();
fixture.detectChanges();
expect(queryByTestId('tmdb-cache-size')?.textContent).toContain(
CACHE_ERROR_LABEL
);
// Still actionable: the rows are there, the user can retry
expect(clearButton().disabled).toBe(false);
});
it('surfaces a failed size read the same way', async () => {
getStats.mockReset();
getStats.mockResolvedValue(null);
await reopenSection();
expect(queryByTestId('tmdb-cache-size')?.textContent).toContain(
CACHE_ERROR_LABEL
);
});
it('retries the size read next time the section is opened', async () => {
getStats.mockReset();
getStats.mockResolvedValueOnce(null);
getStats.mockResolvedValue({ entries: 42, bytes: 2048 });
await reopenSection();
expect(queryByTestId('tmdb-cache-size')?.textContent).toContain(
CACHE_ERROR_LABEL
);
// Otherwise a single transient failure sticks until the destructive
// Clear button shifts it — reopening the page must retry instead
await reopenSection();
expect(getStats).toHaveBeenCalledTimes(2);
expect(queryByTestId('tmdb-cache-size')?.textContent).toContain('42');
});
});