import type { Post } from '@/entities/post'; import { filterByAuthor, toAuthorIds, toAvgBodyLength, toEmptyMessage, toNextSort, toSortingState, } from './postSearchParams'; const post = (id: number, userId: number, body: string): Post => ({ id, userId, title: `제목 ${id}`, body, }); const POSTS = [post(1, 2, '12345'), post(2, 1, '123'), post(3, 2, '1234567')]; describe('작성자 필터', () => { it('작성자를 고르지 않으면 전체 목록을 그대로 돌려준다', () => { // oxlint-disable-next-line unicorn/no-null -- '전체'는 URL에서 파라미터 제거(null)다 expect(filterByAuthor(POSTS, null)).toHaveLength(3); }); it('작성자를 고르면 그 작성자의 글만 남는다', () => { expect(filterByAuthor(POSTS, 2).map((item) => item.id)).toEqual([1, 3]); }); }); describe('정렬 순환', () => { it('정렬이 없는 상태에서 누르면 오름차순이 된다', () => { // oxlint-disable-next-line unicorn/no-null -- 정렬 해제 상태 expect(toNextSort(null)).toBe('asc'); }); it('오름차순에서 누르면 내림차순이 된다', () => { expect(toNextSort('asc')).toBe('desc'); }); it('내림차순에서 누르면 정렬이 해제된다', () => { expect(toNextSort('desc')).toBeNull(); }); it('정렬 해제 상태는 표에 빈 정렬로 전달된다', () => { // oxlint-disable-next-line unicorn/no-null -- 정렬 해제 상태 expect(toSortingState(null)).toEqual([]); }); it('내림차순은 표에 desc로 전달된다', () => { expect(toSortingState('desc')).toEqual([{ id: 'title', desc: true }]); }); }); describe('요약 파생값', () => { it('작성자 목록은 중복 없이 오름차순으로 모인다', () => { expect(toAuthorIds(POSTS)).toEqual([1, 2]); }); it('평균 본문 길이는 반올림한 정수로 나온다', () => { expect(toAvgBodyLength(POSTS)).toBe(5); }); it('글이 하나도 없으면 평균은 0이다', () => { expect(toAvgBodyLength([])).toBe(0); }); }); describe('빈 상태 안내 문구', () => { it('검색 결과 자체가 없으면 검색 결과가 없다고 알린다', () => { expect(toEmptyMessage(0)).toBe('검색 결과가 없어요.'); }); it('결과는 있는데 이 페이지가 비었으면 이전 페이지로 안내한다', () => { expect(toEmptyMessage(3)).toContain('이전 페이지'); }); });