import { describe, it, expect } from 'vitest'; import { normalizeWebcamFacets } from '../normalize-facets'; import { WEBCAM_FILTER_ANY } from '../facets'; const FACETS = [ { name: 'mf_age', title: 'Age', kind: 'range', options: [{ name: '19', title: '', quantity: 0 }, { name: '38', title: '', quantity: 0 }] }, { name: 'mf_country', title: 'Country', kind: 'select', options: [{ name: 'co', title: 'Colombia', quantity: 13 }, { name: '', title: '', quantity: 11 }, { name: 'us', title: 'USA', quantity: 2 }] }, { name: 'mf_eye_color', title: 'Eye Color', kind: 'select', options: [{ name: 'brown', title: 'Brown', quantity: 10 }] }, { name: 'mf_tag', title: 'Tag', kind: 'select', options: [{ name: 'milf', title: 'Milf', quantity: 3 }, { name: 'teen', title: 'Teen', quantity: 5 }] }, ]; describe('normalizeWebcamFacets', () => { it('снимает префикс провайдера и выкидывает технические фасеты', () => { expect(normalizeWebcamFacets(FACETS).map(item => item.name)).toEqual(['age', 'country']); }); it('отбрасывает опции с пустым значением', () => { const country = normalizeWebcamFacets(FACETS).find(item => item.name === 'country'); expect(country?.options.map(option => option.name)).toEqual([WEBCAM_FILTER_ANY, 'co', 'us']); }); it('добавляет селекту вариант «любой» с общим количеством, диапазону — нет', () => { const [age, country] = normalizeWebcamFacets(FACETS, 930); expect(age.options.map(option => option.name)).toEqual(['19', '38']); expect(country.options[0]).toEqual({ name: WEBCAM_FILTER_ANY, title: WEBCAM_FILTER_ANY, quantity: 930 }); }); it('скрывает селект с одним вариантом и слипшийся диапазон', () => { const degenerate = [ { name: 'mf_eye_color', kind: 'select', options: [{ name: 'brown', title: 'Brown', quantity: 1 }] }, { name: 'mf_age', kind: 'range', options: [{ name: '25', title: '', quantity: 0 }, { name: '25', title: '', quantity: 0 }] }, ]; expect(normalizeWebcamFacets(degenerate)).toEqual([]); }); it('раскладывает фильтры по группам: строка фильтров и модалка', () => { const groups = normalizeWebcamFacets([ ...FACETS, { name: 'mf_body_type', title: 'Body Type', kind: 'select', options: [{ name: 'thin', title: 'Thin', quantity: 6 }, { name: 'athletic', title: 'Athletic', quantity: 4 }] }, ]).map(item => [item.name, item.group.name]); expect(groups).toEqual([['age', 'quick'], ['country', 'quick'], ['body_type', 'appearance']]); }); it('пустой ответ провайдера не роняет нормализацию', () => { expect(normalizeWebcamFacets(null)).toEqual([]); expect(normalizeWebcamFacets(undefined)).toEqual([]); }); });