1
|
|
|
import {dummyPoll} from './dummy'; |
2
|
|
|
import {buildVoteCard} from '../src/vote-card'; |
3
|
|
|
import voteCardJson from './json/vote_card.json'; |
4
|
|
|
import {saveVotes, choiceSection, progressBarText} from '../src/helpers/vote'; |
5
|
|
|
import {Votes} from '../src/helpers/interfaces'; |
6
|
|
|
|
7
|
|
|
test('test save voter', () => { |
8
|
|
|
const voter = {uid: 'users/103846892623842357554', name: 'Muhammad'}; |
9
|
|
|
const votes: Votes = { |
10
|
|
|
'0': [], |
11
|
|
|
'1': [], |
12
|
|
|
'2': [{uid: 'users/118239915905237561078', name: 'Yaskur'}], |
13
|
|
|
'3': [ |
14
|
|
|
{uid: 'users/123242424242323423423', name: 'Dyas'}, |
15
|
|
|
{uid: 'users/103846892623842357554', name: 'Muhammad'}, |
16
|
|
|
{uid: 'users/222423423523532523532', name: 'Ammar'}, |
17
|
|
|
], |
18
|
|
|
}; |
19
|
|
|
const voterResult = saveVotes(2, voter, votes); |
20
|
|
|
expect(voterResult).toStrictEqual({ |
21
|
|
|
'0': [], |
22
|
|
|
'1': [], |
23
|
|
|
'2': [ |
24
|
|
|
{uid: 'users/118239915905237561078', name: 'Yaskur'}, |
25
|
|
|
{uid: 'users/103846892623842357554', name: 'Muhammad'}, |
26
|
|
|
], |
27
|
|
|
'3': [ |
28
|
|
|
{uid: 'users/123242424242323423423', name: 'Dyas'}, |
29
|
|
|
{uid: 'users/222423423523532523532', name: 'Ammar'}, |
30
|
|
|
], |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
const voterResult2 = saveVotes(1, voter, votes); |
34
|
|
|
|
35
|
|
|
expect(voterResult2).toStrictEqual({ |
36
|
|
|
'0': [], |
37
|
|
|
'1': [{uid: 'users/103846892623842357554', name: 'Muhammad'}], |
38
|
|
|
'2': [ |
39
|
|
|
{uid: 'users/118239915905237561078', name: 'Yaskur'}, |
40
|
|
|
], |
41
|
|
|
'3': [ |
42
|
|
|
{uid: 'users/123242424242323423423', name: 'Dyas'}, |
43
|
|
|
{uid: 'users/222423423523532523532', name: 'Ammar'}, |
44
|
|
|
], |
45
|
|
|
}); |
46
|
|
|
}); |
47
|
|
|
test('test save voter anonymously', () => { |
48
|
|
|
const voter = {uid: 'users/103846892623842357554', name: 'Muhammad'}; |
49
|
|
|
const votes: Votes = { |
50
|
|
|
'0': [], |
51
|
|
|
'1': [], |
52
|
|
|
'2': [], |
53
|
|
|
'3': [], |
54
|
|
|
}; |
55
|
|
|
const voterResult = saveVotes(2, voter, votes, true); |
56
|
|
|
expect(voterResult).toStrictEqual({ |
57
|
|
|
'0': [], |
58
|
|
|
'1': [], |
59
|
|
|
'2': [ |
60
|
|
|
{uid: 'users/103846892623842357554'}, |
61
|
|
|
], |
62
|
|
|
'3': [], |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
const voterResult2 = saveVotes(4, voter, votes, true); |
66
|
|
|
|
67
|
|
|
expect(voterResult2).toStrictEqual({ |
68
|
|
|
'0': [], |
69
|
|
|
'1': [], |
70
|
|
|
'2': [], |
71
|
|
|
'3': [], |
72
|
|
|
'4': [{uid: 'users/103846892623842357554'}], |
73
|
|
|
}); |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
test('build progress bar text', () => { |
77
|
|
|
const progressBar = progressBarText(2, 4); |
78
|
|
|
|
79
|
|
|
expect(progressBar).toBe('██████████████████'); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
test('build choice section ', () => { |
83
|
|
|
const state = JSON.stringify(dummyPoll); |
84
|
|
|
const normalChoice = choiceSection(2, dummyPoll, 4, state, 'Muhammad Dyas Yaskur'); |
85
|
|
|
|
86
|
|
|
expect(normalChoice). |
87
|
|
|
toStrictEqual({ |
88
|
|
|
'collapsible': true, |
89
|
|
|
'uncollapsibleWidgetsCount': 1, |
90
|
|
|
'widgets': [ |
91
|
|
|
{ |
92
|
|
|
'decoratedText': { |
93
|
|
|
'topLabel': 'Added by Muhammad Dyas Yaskur', |
94
|
|
|
'bottomLabel': progressBarText(2, 4) + ' 2', |
95
|
|
|
'button': { |
96
|
|
|
'onClick': { |
97
|
|
|
'action': { |
98
|
|
|
'function': 'vote', |
99
|
|
|
'parameters': [ |
100
|
|
|
{ |
101
|
|
|
'key': 'state', |
102
|
|
|
'value': JSON.stringify(dummyPoll), |
103
|
|
|
}, {'key': 'index', 'value': '2'}], |
104
|
|
|
}, |
105
|
|
|
}, 'text': 'vote', |
106
|
|
|
}, |
107
|
|
|
'text': 'Coco Worm', |
108
|
|
|
}, |
109
|
|
|
}, {'textParagraph': {'text': 'Isa bin Maryam, Musa bin Imran'}}], |
110
|
|
|
}); |
111
|
|
|
|
112
|
|
|
dummyPoll.anon = true; |
113
|
|
|
const anonymousChoice = choiceSection(2, dummyPoll, 4, |
114
|
|
|
JSON.stringify(dummyPoll)); |
115
|
|
|
expect(anonymousChoice).toStrictEqual({ |
116
|
|
|
'widgets': [ |
117
|
|
|
{ |
118
|
|
|
'decoratedText': { |
119
|
|
|
'bottomLabel': progressBarText(2, 4) + ' 2', |
120
|
|
|
'button': { |
121
|
|
|
'onClick': { |
122
|
|
|
'action': { |
123
|
|
|
'function': 'vote', |
124
|
|
|
'parameters': [ |
125
|
|
|
{ |
126
|
|
|
'key': 'state', |
127
|
|
|
'value': JSON.stringify(dummyPoll), |
128
|
|
|
}, {'key': 'index', 'value': '2'}], |
129
|
|
|
}, |
130
|
|
|
}, 'text': 'vote', |
131
|
|
|
}, |
132
|
|
|
'text': 'Coco Worm', |
133
|
|
|
}, |
134
|
|
|
}], |
135
|
|
|
}); |
136
|
|
|
dummyPoll.anon = false; |
137
|
|
|
}); |
138
|
|
|
|
139
|
|
|
test('build vote card', () => { |
140
|
|
|
const pollCard = buildVoteCard(dummyPoll); |
141
|
|
|
|
142
|
|
|
expect(pollCard.card).toEqual(voteCardJson); |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
test('build vote card with long topic', () => { |
146
|
|
|
dummyPoll.topic = '12345678901234567890123456789012345678901234567890'; |
147
|
|
|
const pollCard = buildVoteCard(dummyPoll); |
148
|
|
|
|
149
|
|
|
expect(pollCard.card?.header).toBeUndefined(); |
150
|
|
|
// @ts-ignore: should not error |
151
|
|
|
expect(pollCard.card.sections[0].widgets[0].decoratedText.text).toBe(dummyPoll.topic); |
152
|
|
|
}); |
153
|
|
|
|