Files
decision/web/tests/unit/votefold.test.ts
T

69 lines
2.5 KiB
TypeScript

// The vote unfolded under the article, rendered as the server would (svelte/server): what
// it reads, letter for letter. A space at the start of a block is lost (Svelte trims it):
// « 0 contredont 1 simulé » (review of M6b).
import { render } from 'svelte/server';
import { describe, expect, it } from 'vitest';
import VoteFold from '../../src/lib/components/VoteFold.svelte';
import type { DocState } from '../../src/lib/doc.svelte';
import type { Proposal, Tally, Unit } from '../../src/lib/types';
function proposal(tally: Tally, tallySimulated: Tally): Proposal {
return {
id: 'p1',
unitId: 'u1',
kind: 'text',
inertiaTarget: null,
title: 'Logiciel auditable',
body: '',
status: 'open',
origin: 'seed',
label: null,
author: null,
mine: false,
revision: 1,
createdAt: '2026-11-03T00:00:00Z',
updatedAt: '2026-11-03T00:00:00Z',
submittedAt: null,
enteredAt: null,
confirmedAt: null,
seedMinimal: false,
tally,
tallyPending: { for: 0, against: 0 },
tallySimulated,
evaluation: null,
votedAtInertia: 3,
blocked: false,
votable: true,
objections: 0,
myVote: null,
myVoteState: null,
myVoteStale: false,
myVoteChain: false,
myBallot: null,
translations: {},
};
}
function fold(p: Proposal): string {
const unit: Unit = { id: 'u1', code: 'E9', section: null, position: 1, inertia: 3, inertiaChanges: [], inForce: null, proposals: [p] };
const doc = { view: null, original: false, articleTitle: () => '' } as unknown as DocState;
const noop = () => {};
const { body } = render(VoteFold, {
props: { doc, proposal: p, unit, onopen: noop, onsettings: noop, ondetails: noop, onanswer: noop, onsign: noop },
});
const line = /<p class="tally[^"]*">([\s\S]*?)<\/p>/.exec(body)?.[1];
if (line === undefined) throw new Error(`no line of totals in ${body}`);
// The text read: tags and the markers of the server rendering left out.
return line.replace(/<!--[\s\S]*?-->/g, '').replace(/<[^>]+>/g, '');
}
describe('the totals of the vote unfolded', () => {
it('say the simulated votes after a space', () => {
expect(fold(proposal({ for: 0, against: 1 }, { for: 0, against: 1 }))).toBe('0 pour · 1 contre dont 1 simulé');
expect(fold(proposal({ for: 2, against: 1 }, { for: 2, against: 1 }))).toBe('2 pour · 1 contre dont 3 simulés');
});
it('say nothing more without a simulated vote', () => {
expect(fold(proposal({ for: 4, against: 2 }, { for: 0, against: 0 }))).toBe('4 pour · 2 contre');
});
});