The picker shipped as a capability of CampaignTable rather than a municípios feature, so every list that already uses the shared table got it: pass `columnVisibility` and the table mounts the menu and filters the columns. Persistence is a cookie, not localStorage, and the reason is a type rather than a preference: CampaignTableColumn.cell is a function, so the column array never crosses the server/client boundary and the client cannot rebuild the table without it. The server filters, so a hidden column does not reach the RSC payload either. `campaign_columns` carries a listId -> hidden ids map in a compact form with no JSON and no percent-encoding, because document.cookie and Next's cookies() disagree about decoding. Closing the menu is the commit. The first cut debounced at 400 ms, which batches a burst rather than a reading pace, so unchecking four columns while reading their labels was four full-route refreshes — on /campanha/liderancas each one re-pays the ~229 ms / 371-statement render already ledgered P1. The timer survives at 3 s as a safety net for a menu left open; the price is that the table behind the menu no longer updates per checkbox. `defaultVisible` is gone: it was true on 10 columns and false on none, and storing only the hidden set is what makes a newly shipped column visible to everyone who never touched the picker. In exchange `label` is required, which made `head` optional — 24 columns had a head that only restated the new label. Each surface declares one mandatory column, re-filtered on the server so a stale cookie cannot leave a row without its only linked cell. The bug review caught is this codebase's recurring one: the RSC payload of toggle A arrives inside the window of toggle B, and adopting the server there undid B and then committed the set without it. Reconciliation now runs only when no write is pending, pinned red against the previous code. Municipality column ids are typed against MunicipalityListColumnId now, so the id, the B22 description and the picker label are one key; the union member `nivel` was renamed `level` to match the id and the ?level= filter. The picker's own content comparison is sameIdSet, widened to string ids.
95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
parseCampaignHiddenColumns,
|
|
resolveVisibleColumns,
|
|
serializeCampaignHiddenColumns,
|
|
toggleHiddenColumn,
|
|
} from '@/lib/campaignColumnVisibility'
|
|
|
|
describe('parseCampaignHiddenColumns', () => {
|
|
it('reads one list and several lists', () => {
|
|
expect(parseCampaignHiddenColumns('municipios:kind~trend')).toEqual({
|
|
municipios: ['kind', 'trend'],
|
|
})
|
|
expect(parseCampaignHiddenColumns('municipios:kind|liderancas:organizations')).toEqual({
|
|
municipios: ['kind'],
|
|
liderancas: ['organizations'],
|
|
})
|
|
})
|
|
|
|
it('shows the full table when the cookie is absent, empty or malformed', () => {
|
|
expect(parseCampaignHiddenColumns(undefined)).toEqual({})
|
|
expect(parseCampaignHiddenColumns('')).toEqual({})
|
|
expect(parseCampaignHiddenColumns('garbage')).toEqual({})
|
|
expect(parseCampaignHiddenColumns(':kind')).toEqual({})
|
|
expect(parseCampaignHiddenColumns('municipios:')).toEqual({})
|
|
})
|
|
|
|
it('drops unknown lists and column ids that are not identifiers', () => {
|
|
expect(parseCampaignHiddenColumns('assessores:email|municipios:kind')).toEqual({
|
|
municipios: ['kind'],
|
|
})
|
|
expect(parseCampaignHiddenColumns('municipios:kind~<script>~trend')).toEqual({
|
|
municipios: ['kind', 'trend'],
|
|
})
|
|
})
|
|
|
|
it('dedupes and refuses an oversized cookie instead of trusting it', () => {
|
|
expect(parseCampaignHiddenColumns('municipios:kind~kind')).toEqual({ municipios: ['kind'] })
|
|
expect(parseCampaignHiddenColumns(`municipios:${'a'.repeat(4000)}`)).toEqual({})
|
|
})
|
|
})
|
|
|
|
describe('serializeCampaignHiddenColumns', () => {
|
|
it('round-trips through parse and omits lists with nothing hidden', () => {
|
|
const value = { municipios: ['kind', 'trend'], demandas: ['requester'] }
|
|
const serialized = serializeCampaignHiddenColumns(value)
|
|
|
|
expect(parseCampaignHiddenColumns(serialized)).toEqual(value)
|
|
expect(serializeCampaignHiddenColumns({ municipios: [] })).toBe('')
|
|
})
|
|
|
|
it('never emits a character that would break the cookie value', () => {
|
|
const serialized = serializeCampaignHiddenColumns({ municipios: ['kind', 'trend'] })
|
|
|
|
expect(serialized).not.toMatch(/[;,\s"\\]/)
|
|
})
|
|
})
|
|
|
|
describe('toggleHiddenColumn', () => {
|
|
it('hides, shows and never duplicates an id', () => {
|
|
const hidden = toggleHiddenColumn([], 'kind', false)
|
|
expect(hidden).toEqual(['kind'])
|
|
|
|
expect(toggleHiddenColumn(hidden, 'kind', false)).toEqual(['kind'])
|
|
expect(toggleHiddenColumn(hidden, 'kind', true)).toEqual([])
|
|
})
|
|
|
|
it('leaves the other columns of the list alone', () => {
|
|
expect(toggleHiddenColumn(['trend', 'kind'], 'kind', true)).toEqual(['trend'])
|
|
expect(toggleHiddenColumn(['trend'], 'kind', false)).toEqual(['trend', 'kind'])
|
|
})
|
|
})
|
|
|
|
describe('resolveVisibleColumns', () => {
|
|
const columns = [{ id: 'name', mandatory: true }, { id: 'kind' }, { id: 'trend' }]
|
|
|
|
it('drops the hidden columns and keeps the declared order', () => {
|
|
expect(resolveVisibleColumns(columns, ['trend']).map((column) => column.id)).toEqual([
|
|
'name',
|
|
'kind',
|
|
])
|
|
})
|
|
|
|
it('never drops a mandatory column, whatever the cookie says', () => {
|
|
expect(resolveVisibleColumns(columns, ['name', 'kind', 'trend']).map((c) => c.id)).toEqual([
|
|
'name',
|
|
])
|
|
})
|
|
|
|
it('returns every column when nothing is hidden', () => {
|
|
expect(resolveVisibleColumns(columns, undefined)).toHaveLength(3)
|
|
expect(resolveVisibleColumns(columns, [])).toHaveLength(3)
|
|
})
|
|
})
|