72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
|
|
import { ApiError, AuthError, NetworkError } from '@/core/api/types';
|
||
|
|
import {
|
||
|
|
isChapterListAuthError,
|
||
|
|
isChapterListEmptySuccess,
|
||
|
|
isChapterListNotFoundError,
|
||
|
|
normalizeChapterList,
|
||
|
|
shouldShowChapterListLoadError,
|
||
|
|
} from '@/features/memoir/chapter-list-response';
|
||
|
|
import type { Chapter } from '@/features/memoir/types';
|
||
|
|
|
||
|
|
describe('normalizeChapterList', () => {
|
||
|
|
it('returns empty array for nullish or non-array payloads', () => {
|
||
|
|
expect(normalizeChapterList(null)).toEqual([]);
|
||
|
|
expect(normalizeChapterList(undefined)).toEqual([]);
|
||
|
|
expect(normalizeChapterList({ items: [] })).toEqual([]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('passes through chapter arrays', () => {
|
||
|
|
const chapters = [{ id: 'ch-1' }] as Chapter[];
|
||
|
|
expect(normalizeChapterList(chapters)).toBe(chapters);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('isChapterListNotFoundError', () => {
|
||
|
|
it('detects ApiError 404', () => {
|
||
|
|
expect(isChapterListNotFoundError(new ApiError('missing', 404))).toBe(true);
|
||
|
|
expect(isChapterListNotFoundError(new ApiError('bad', 500))).toBe(false);
|
||
|
|
expect(isChapterListNotFoundError(new Error('other'))).toBe(false);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('isChapterListEmptySuccess', () => {
|
||
|
|
it('is true only for successful empty arrays', () => {
|
||
|
|
expect(isChapterListEmptySuccess(true, [])).toBe(true);
|
||
|
|
expect(isChapterListEmptySuccess(true, [{ id: 'x' } as never])).toBe(
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
expect(isChapterListEmptySuccess(false, [])).toBe(false);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('isChapterListAuthError', () => {
|
||
|
|
it('treats AuthError and 401/403 ApiError as auth errors', () => {
|
||
|
|
expect(isChapterListAuthError(new AuthError())).toBe(true);
|
||
|
|
expect(isChapterListAuthError(new ApiError('unauthorized', 401))).toBe(true);
|
||
|
|
expect(isChapterListAuthError(new ApiError('forbidden', 403))).toBe(true);
|
||
|
|
expect(isChapterListAuthError(new ApiError('server', 500))).toBe(false);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('shouldShowChapterListLoadError', () => {
|
||
|
|
it('hides load error for empty success, 404, and auth failures', () => {
|
||
|
|
expect(shouldShowChapterListLoadError(null, true, 0)).toBe(false);
|
||
|
|
expect(shouldShowChapterListLoadError(new ApiError('nope', 404), false, 0)).toBe(
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
expect(shouldShowChapterListLoadError(new AuthError(), false, 0)).toBe(false);
|
||
|
|
expect(
|
||
|
|
shouldShowChapterListLoadError(new ApiError('unauthorized', 401), false, 0),
|
||
|
|
).toBe(false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('shows load error for network and server failures', () => {
|
||
|
|
expect(
|
||
|
|
shouldShowChapterListLoadError(new NetworkError('offline'), false, 0),
|
||
|
|
).toBe(true);
|
||
|
|
expect(
|
||
|
|
shouldShowChapterListLoadError(new ApiError('boom', 500), false, 0),
|
||
|
|
).toBe(true);
|
||
|
|
});
|
||
|
|
});
|