This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { getUserVotes, updateFeatureVote, getVotingStats } from '../voting';
|
||||
import { resetMemoryStorage } from '../storage/memory';
|
||||
import { FeatureRequest } from '../../../types/gen/types.gen';
|
||||
|
||||
const OPT_A = FeatureRequest.STATISTICS;
|
||||
const OPT_B = FeatureRequest.UI;
|
||||
|
||||
beforeEach(() => {
|
||||
resetMemoryStorage();
|
||||
});
|
||||
|
||||
test('přidání hlasu a přečtení přes getUserVotes', async () => {
|
||||
await updateFeatureVote('jannovak', OPT_A, true);
|
||||
const votes = await getUserVotes('jannovak');
|
||||
expect(votes).toContain(OPT_A);
|
||||
});
|
||||
|
||||
test('opakované přidání stejného hlasu vyhodí chybu', async () => {
|
||||
await updateFeatureVote('jannovak', OPT_A, true);
|
||||
await expect(updateFeatureVote('jannovak', OPT_A, true))
|
||||
.rejects.toThrow('Pro tuto možnost jste již hlasovali');
|
||||
});
|
||||
|
||||
test('překročení limitu 4 hlasů vyhodí chybu', async () => {
|
||||
const options = Object.values(FeatureRequest);
|
||||
for (let i = 0; i < 4; i++) {
|
||||
await updateFeatureVote('jannovak', options[i], true);
|
||||
}
|
||||
await expect(updateFeatureVote('jannovak', options[4], true))
|
||||
.rejects.toThrow('maximálně 4 možnosti');
|
||||
});
|
||||
|
||||
test('odebrání hlasu funguje správně', async () => {
|
||||
await updateFeatureVote('jannovak', OPT_A, true);
|
||||
await updateFeatureVote('jannovak', OPT_A, false);
|
||||
const votes = await getUserVotes('jannovak');
|
||||
expect(votes).not.toContain(OPT_A);
|
||||
});
|
||||
|
||||
test('odebrání posledního hlasu odstraní login ze storage', async () => {
|
||||
await updateFeatureVote('jannovak', OPT_A, true);
|
||||
const data = await updateFeatureVote('jannovak', OPT_A, false);
|
||||
expect('jannovak' in data).toBe(false);
|
||||
});
|
||||
|
||||
test('getVotingStats vrátí prázdný objekt, pokud nikdo nehlasoval', async () => {
|
||||
const stats = await getVotingStats();
|
||||
expect(stats).toEqual({});
|
||||
});
|
||||
|
||||
test('getVotingStats správně agreguje hlasy více uživatelů', async () => {
|
||||
await updateFeatureVote('jannovak', OPT_A, true);
|
||||
await updateFeatureVote('jannovak', OPT_B, true);
|
||||
await updateFeatureVote('petrfree', OPT_A, true);
|
||||
const stats = await getVotingStats();
|
||||
expect(stats[OPT_A]).toBe(2);
|
||||
expect(stats[OPT_B]).toBe(1);
|
||||
});
|
||||
|
||||
test('getUserVotes vrátí prázdné pole pro uživatele bez hlasů', async () => {
|
||||
const votes = await getUserVotes('neexistujici');
|
||||
expect(votes).toEqual([]);
|
||||
});
|
||||
Reference in New Issue
Block a user