37 lines
2.1 KiB
TypeScript
37 lines
2.1 KiB
TypeScript
import { calculatePollResults, findUserAnswer, parsePollOptionLines, selectActivePoll } from '../src/webparts/dynamicPoll/PollCore';
|
|
import { IPoll, IPollAnswer } from '../src/webparts/dynamicPoll/PollTypes';
|
|
|
|
let passed: number = 0;
|
|
function assert(name: string, condition: boolean): void {
|
|
if (!condition) { throw new Error('FAILED: ' + name); }
|
|
passed++; console.log('OK:', name);
|
|
}
|
|
function poll(value: any): IPoll { return value as IPoll; } // tslint:disable-line:no-any
|
|
|
|
const now: Date = new Date('2026-08-23T12:00:00Z');
|
|
const selected: IPoll = selectActivePoll([
|
|
poll({ id: 1, question: 'old', options: ['A'], isActive: true,
|
|
startDate: new Date('2026-08-20T00:00:00Z'), endDate: new Date('2026-08-22T00:00:00Z') }),
|
|
poll({ id: 2, question: 'current', options: ['A', 'B'], isActive: true,
|
|
startDate: new Date('2026-08-23T00:00:00Z'), endDate: new Date('2026-08-24T00:00:00Z') }),
|
|
poll({ id: 3, question: 'future', options: ['A'], isActive: true,
|
|
startDate: new Date('2026-08-24T00:00:00Z') })
|
|
], now);
|
|
assert('current poll is selected by activation window', !!selected && selected.id === 2);
|
|
|
|
const answers: IPollAnswer[] = [
|
|
{ id: 1, userKey: 'one@contoso.local', answer: 'A' },
|
|
{ id: 2, userKey: 'two@contoso.local', answer: 'B' },
|
|
{ id: 3, userKey: 'three@contoso.local', answer: 'A' },
|
|
{ id: 4, userKey: 'legacy@contoso.local', answer: 'Removed option' }
|
|
];
|
|
const results = calculatePollResults(['A', 'B'], answers);
|
|
assert('votes are counted per configured option', results[0].votes === 2 && results[1].votes === 1);
|
|
assert('invalid legacy answer is excluded from percentage', results[0].percentage === 67 && results[1].percentage === 33);
|
|
assert('user lookup is case insensitive', findUserAnswer(answers, 'ONE@CONTOSO.LOCAL') === 'A');
|
|
assert('unknown user has no answer', findUserAnswer(answers, 'unknown@contoso.local') === '');
|
|
const parsedOptions: string[] = parsePollOptionLines(['Ja\r\nNein\n\n Anderer Termin ', 'ja']);
|
|
assert('multiline options are trimmed, split and deduplicated',
|
|
parsedOptions.length === 3 && parsedOptions[0] === 'Ja' && parsedOptions[2] === 'Anderer Termin');
|
|
console.log('DynamicPoll core tests passed:', passed);
|