Introduces a shared CSS custom property token layer and applies it across all five views (library, upload, detail, login, app shell). Each view now has intentional loading, empty, and error states. - styles.css: 13 design tokens on :root; shimmer skeleton animation - Library: 150ms-debounced skeleton loading, empty state with /upload link, error card with retry, card hover lift, broken-image fallback - Upload: token-styled drop-zone, Uploading… spinner, 4s success banner, distinct validation vs. network error messages - Detail: image skeleton, network error card (separate from 404 not-found card), Owner actions panel, danger tag error styling, broken-image fallback - Login: vertically centred surface card, danger field/server errors, Signing in… disabled button - App shell: 48px fixed header, app name left, sign-out right, no reflow on auth state change - All 24 ESLint errors resolved (including pre-existing auth spec issues); ng build and ng lint pass clean Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
123 lines
5.3 KiB
TypeScript
123 lines
5.3 KiB
TypeScript
import { TestBed } from '@angular/core/testing';
|
|
import { provideRouter, Router } from '@angular/router';
|
|
import { provideHttpClient } from '@angular/common/http';
|
|
import { provideHttpClientTesting } from '@angular/common/http/testing';
|
|
import { UploadComponent } from './upload.component';
|
|
import { routes } from '../app.routes';
|
|
|
|
describe('UploadComponent', () => {
|
|
let component: UploadComponent;
|
|
|
|
beforeEach(async () => {
|
|
await TestBed.configureTestingModule({
|
|
imports: [UploadComponent],
|
|
providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter(routes)],
|
|
}).compileComponents();
|
|
});
|
|
|
|
it('should normalise tag input: lowercases and splits on comma/space', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
component.tagInput = 'CAT, Funny reaction';
|
|
expect(component.parseTagInput(component.tagInput)).toEqual(['cat', 'funny', 'reaction']);
|
|
});
|
|
|
|
it('should split on commas', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
expect(fixture.componentInstance.parseTagInput('a,b,c')).toEqual(['a', 'b', 'c']);
|
|
});
|
|
|
|
it('should filter empty tokens', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
expect(fixture.componentInstance.parseTagInput(' ,, cat ,,')).toEqual(['cat']);
|
|
});
|
|
|
|
it('on duplicate response: shows toast and navigates to detail', async () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
const router = TestBed.inject(Router);
|
|
spyOn(router, 'navigate');
|
|
await component.handleUploadResponse({ id: 'abc', duplicate: true } as Parameters<typeof component.handleUploadResponse>[0]);
|
|
expect(component.toastMessage).toContain('library');
|
|
expect(router.navigate).toHaveBeenCalledWith(['/images', 'abc']);
|
|
});
|
|
|
|
it('on success response: shows success toast and navigates to detail', async () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
const router = TestBed.inject(Router);
|
|
spyOn(router, 'navigate');
|
|
await component.handleUploadResponse({ id: 'xyz', duplicate: false } as Parameters<typeof component.handleUploadResponse>[0]);
|
|
expect(component.toastMessage).toBeTruthy();
|
|
expect(router.navigate).toHaveBeenCalledWith(['/images', 'xyz']);
|
|
});
|
|
|
|
it('on error response: shows inline error, no navigation', async () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
const router = TestBed.inject(Router);
|
|
spyOn(router, 'navigate');
|
|
component.handleUploadError({ status: 422, error: { detail: 'bad file', code: 'invalid_mime_type' } });
|
|
expect(component.errorMessage).toBeTruthy();
|
|
expect(router.navigate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// New polish tests
|
|
|
|
it('submit button is disabled when no file is selected', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
fixture.detectChanges();
|
|
const btn = (fixture.nativeElement as HTMLElement).querySelector('button[type="submit"]') as HTMLButtonElement;
|
|
expect(btn.disabled).toBeTrue();
|
|
});
|
|
|
|
it('submit button shows "Uploading…" label while uploading is true', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
component.uploading = true;
|
|
fixture.detectChanges();
|
|
const btn = (fixture.nativeElement as HTMLElement).querySelector('button[type="submit"]') as HTMLButtonElement;
|
|
expect(btn.textContent).toContain('Uploading');
|
|
expect(btn.disabled).toBeTrue();
|
|
});
|
|
|
|
it('showSuccess banner is hidden initially', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
fixture.detectChanges();
|
|
expect((fixture.nativeElement as HTMLElement).querySelector('.success-banner')).toBeNull();
|
|
});
|
|
|
|
it('showSuccess banner is visible when showSuccess is true', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
component.showSuccess = true;
|
|
component.uploadedFilename = 'photo.jpg';
|
|
fixture.detectChanges();
|
|
const banner = (fixture.nativeElement as HTMLElement).querySelector('.success-banner');
|
|
expect(banner).not.toBeNull();
|
|
expect(banner!.textContent).toContain('photo.jpg');
|
|
});
|
|
|
|
it('shows validation error message for 422 response', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
fixture.detectChanges();
|
|
component.handleUploadError({ status: 422, error: { detail: 'Unsupported file type', code: 'invalid_mime_type' } });
|
|
fixture.detectChanges();
|
|
const err = (fixture.nativeElement as HTMLElement).querySelector('.error');
|
|
expect(err!.textContent).toContain('Unsupported file type');
|
|
});
|
|
|
|
it('shows generic error message for network error', () => {
|
|
const fixture = TestBed.createComponent(UploadComponent);
|
|
component = fixture.componentInstance;
|
|
fixture.detectChanges();
|
|
component.handleUploadError({ status: 500, error: null });
|
|
fixture.detectChanges();
|
|
const err = (fixture.nativeElement as HTMLElement).querySelector('.error');
|
|
expect(err!.textContent).toBeTruthy();
|
|
});
|
|
});
|