import { renderToStaticMarkup } from 'react-dom/server'
import { MemoryRouter } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { describe, expect, it } from 'vitest'
import ChartFrame from './ChartFrame'
import BoxWaffleChart from '../stance/BoxWaffleChart'
import DumbbellRows, { type DumbbellRow } from '../stance/DumbbellRows'
import { CountBars } from '../stance/FitDisplay'
import { Dots } from '../stance/ChartPanel'
import StoryCountyMap, { type PlaceCount } from '../stance/StoryCountyMap'
import StoryStatesMap from '../home/StoryStatesMap'
import StoryDotMap from '../stance/StoryDotMap'
import TimelineChart from '../stance/TimelineChart'
import LegislationMap from '../stance/LegislationMap'
import type { DotsSpec } from '../../utils/stanceGame'
// Test-only fixtures: shapes the API returns, never served by the app.
const square = (x0: number, y0: number, x1: number, y1: number) => [[x0, y0], [x1, y0], [x1, y1], [x0, y1], [x0, y0]]
const topo = {
type: 'Topology',
arcs: [square(0, 0, 480, 600), square(490, 0, 970, 600), square(490, 0, 730, 600), square(740, 0, 970, 600)],
objects: {
states: { type: 'GeometryCollection', geometries: [{ type: 'Polygon', id: '01', arcs: [[0]], properties: { name: 'Alabama' } }, { type: 'Polygon', id: '12', arcs: [[1]], properties: { name: 'Florida' } }] },
counties: { type: 'GeometryCollection', geometries: [{ type: 'Polygon', id: '12001', arcs: [[2]], properties: { name: 'Alachua' } }, { type: 'Polygon', id: '12003', arcs: [[3]], properties: { name: 'Baker' } }] },
},
}
const withClient = (el: JSX.Element, seed: (qc: QueryClient) => void = () => {}) => {
const qc = new QueryClient()
qc.setQueryData(['us-atlas-states-albers'], topo)
qc.setQueryData(['us-atlas-counties-albers'], topo)
seed(qc)
return renderToStaticMarkup(
{el}
,
)
}
/** A rendered chart has exactly the frame's title and a non-empty legend. */
function expectFramed(html: string) {
expect(html).toContain('data-chart-frame')
const title = /data-chart-title[^>]*>([^<]{4,})/.exec(html)
expect(title, 'a title').not.toBeNull()
const legend = /data-chart-legend[^>]*>([\s\S]*?)<\/div><\/div>|data-chart-legend[^>]*>([\s\S]*?)<\/div>/.exec(html)
expect(legend, 'a legend').not.toBeNull()
expect(((legend?.[1] ?? legend?.[2]) ?? '').replace(/<[^>]+>/g, '').trim().length).toBeGreaterThan(2)
}
const place = (id: string, name: string, meetings: number, extra: Partial = {}): PlaceCount => ({
jurisdiction_id: id,
jurisdiction_name: name,
jurisdiction_type: 'county',
geoid: id,
latitude: null,
longitude: null,
meetings,
speakers_for: 0,
speakers_against: 0,
speakers_question: 0,
speakers_neutral: 0,
...extra,
})
describe('every map and chart has a title and a legend (#1409)', () => {
it('ChartFrame renders both, and the types require them', () => {
const html = renderToStaticMarkup(
,
)
expectFramed(html)
// @ts-expect-error title and legend are required
renderToStaticMarkup(x)
})
it('the county map keeps its title and key with no side panel (home, chrome off)', () => {
const byPlace = [place('12001', 'Alachua County', 9, { speakers_against: 4, speakers_for: 1 }), place('12003', 'Baker County', 3)]
for (const chrome of [true, false]) {
const html = withClient( {}} chrome={chrome} subject="short-term rentals" />)
expect(html).toContain('stance-map-svg')
expectFramed(html)
}
})
it('the national map and the dot map', () => {
const byState = [{ state_code: 'AL', state: 'Alabama', meetings: 9 }, { state_code: 'FL', state: 'Florida', meetings: 4 }]
const html = withClient( `/x?state=${c}`} subject="short-term rentals" />)
expect(html).toContain('