All files / app/features/article/resolvers article.resolver.ts

93.75% Statements 15/16
100% Branches 6/6
66.66% Functions 2/3
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 311x 1x     1x 1x           1x       6x 6x 1x     5x 5x 3x     2x     1x    
import { ArticleService } from '../../../services/articles';
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, ResolveFn } from '@angular/router';
import { ArticleVersion } from '@drevo-web/shared';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
 
/**
 * Pure function for resolving article data from route params.
 * Extracted for testability without injection context.
 */
export function resolveArticle(
    articleService: ArticleService,
    route: ActivatedRouteSnapshot
): Observable<ArticleVersion | undefined> {
    const idParam = route.paramMap.get('id');
    if (!idParam) {
        return of(undefined);
    }
 
    const id = Number(idParam);
    if (isNaN(id) || id <= 0) {
        return of(undefined);
    }
 
    return articleService.getArticle(id).pipe(catchError(() => of(undefined)));
}
 
export const articleResolver: ResolveFn<ArticleVersion | undefined> = route =>
    resolveArticle(inject(ArticleService), route);