SPB Git forge

spb/cancerindex

Public
37commits 1branches 0releases
2.9 MBsize
maindefault branch
10 days agolast push
TypeScript 97.2% SQL 1.5% CSS 0.6% JavaScript 0.5%
2.7 KB · 53 lines typescript
Raw Blame History
1import { describe, it, expect } from 'vitest';2import { latestYearPerScope, scopeYear, scopeWithoutYear } from '@/lib/rankings-util';34const row = (metric_slug: string, scope_key: string, id: number) => ({ id, metric_slug, scope_key });56describe('scopeYear / scopeWithoutYear', () => {7  it('extracts the year and strips it from the key', () => {8    expect(scopeYear('geo=WORLD|sex=all|age=all|year=2021|level=top')).toBe(2021);9    expect(scopeYear('geo=WORLD|sex=all|age=all|year=latest|level=top')).toBe(0);10    expect(scopeYear('')).toBe(0);11    expect(scopeWithoutYear('geo=WORLD|sex=all|age=all|year=2021|level=top')).toBe('geo=WORLD|sex=all|age=all|level=top');12    expect(scopeWithoutYear('geo=USA|level=all')).toBe('geo=USA|level=all');13  });14});1516describe('latestYearPerScope', () => {17  it('keeps one row per metric + scope: the latest year', () => {18    const rows = [19      row('mortality_count', 'geo=USA|sex=all|age=all|year=2019|level=top', 1),20      row('mortality_count', 'geo=USA|sex=all|age=all|year=2021|level=top', 2),21      row('mortality_count', 'geo=USA|sex=all|age=all|year=2020|level=top', 3),22    ];23    const r = latestYearPerScope(rows);24    expect(r.rows.map((x) => x.id)).toEqual([2]);25    expect(r.hidden).toBe(2);26  });27  it('treats different metrics, geographies and sexes as separate scopes', () => {28    const rows = [29      row('mortality_count', 'geo=USA|sex=all|age=all|year=2020|level=top', 1),30      row('incidence_count', 'geo=USA|sex=all|age=all|year=2020|level=top', 2),31      row('mortality_count', 'geo=WORLD|sex=all|age=all|year=2020|level=top', 3),32      row('mortality_count', 'geo=USA|sex=female|age=all|year=2020|level=top', 4),33      row('mortality_count', 'geo=USA|sex=all|age=all|year=2018|level=top', 5),34    ];35    const r = latestYearPerScope(rows);36    expect(r.rows.map((x) => x.id)).toEqual([1, 2, 3, 4]);37    expect(r.hidden).toBe(1);38  });39  it('keeps input order for surviving rows (first appearance of the scope)', () => {40    const rows = [row('a', 'geo=X|year=2019', 1), row('b', 'geo=X|year=2021', 2), row('a', 'geo=X|year=2021', 3)];41    expect(latestYearPerScope(rows).rows.map((x) => x.id)).toEqual([3, 2]);42  });43  it('keeps yearless ("latest") scopes as their own rows and prefers a dated year over none', () => {44    const rows = [row('active_trials', 'geo=WORLD|sex=all|age=all|year=latest|level=top', 1), row('mortality_count', 'geo=USA|year=latest', 2), row('mortality_count', 'geo=USA|year=2021', 3)];45    const r = latestYearPerScope(rows);46    expect(r.rows.map((x) => x.id)).toEqual([1, 3]);47    expect(r.hidden).toBe(1);48  });49  it('handles empty input', () => {50    expect(latestYearPerScope([])).toEqual({ rows: [], hidden: 0 });51  });52});53