15
15
*/
16
16
package io.getstream.feeds.android.client.internal.state
17
17
18
- import io.getstream.feeds.android.client.api.model.PaginationData
19
- import io.getstream.feeds.android.client.api.model.PaginationResult
20
18
import io.getstream.feeds.android.client.api.model.PollVoteData
21
19
import io.getstream.feeds.android.client.api.model.QueryConfiguration
22
20
import io.getstream.feeds.android.client.api.state.query.PollVotesFilterField
23
21
import io.getstream.feeds.android.client.api.state.query.PollVotesQuery
24
22
import io.getstream.feeds.android.client.api.state.query.PollVotesSort
23
+ import io.getstream.feeds.android.client.internal.test.TestData.defaultPaginationResult
25
24
import io.getstream.feeds.android.client.internal.test.TestData.pollVoteData
26
25
import kotlinx.coroutines.test.runTest
27
26
import org.junit.Assert.assertEquals
@@ -41,11 +40,7 @@ internal class PollVoteListStateImplTest {
41
40
@Test
42
41
fun `on queryMorePollVotes, then update votes and pagination` () = runTest {
43
42
val votes = listOf (pollVoteData(), pollVoteData(" vote-2" , " poll-1" , " option-2" , " user-2" ))
44
- val paginationResult =
45
- PaginationResult (
46
- models = votes,
47
- pagination = PaginationData (next = " next-cursor" , previous = null ),
48
- )
43
+ val paginationResult = defaultPaginationResult(votes)
49
44
val queryConfig =
50
45
QueryConfiguration <PollVotesFilterField , PollVotesSort >(
51
46
filter = null ,
@@ -63,11 +58,7 @@ internal class PollVoteListStateImplTest {
63
58
fun `on pollVoteUpdated, then update specific vote` () = runTest {
64
59
val initialVotes =
65
60
listOf (pollVoteData(), pollVoteData(" vote-2" , " poll-1" , " option-2" , " user-2" ))
66
- val paginationResult =
67
- PaginationResult (
68
- models = initialVotes,
69
- pagination = PaginationData (next = " next-cursor" , previous = null ),
70
- )
61
+ val paginationResult = defaultPaginationResult(initialVotes)
71
62
val queryConfig =
72
63
QueryConfiguration <PollVotesFilterField , PollVotesSort >(
73
64
filter = null ,
@@ -87,11 +78,7 @@ internal class PollVoteListStateImplTest {
87
78
fun `on pollVoteRemoved, then remove specific vote` () = runTest {
88
79
val initialVotes =
89
80
listOf (pollVoteData(), pollVoteData(" vote-2" , " poll-1" , " option-2" , " user-2" ))
90
- val paginationResult =
91
- PaginationResult (
92
- models = initialVotes,
93
- pagination = PaginationData (next = " next-cursor" , previous = null ),
94
- )
81
+ val paginationResult = defaultPaginationResult(initialVotes)
95
82
val queryConfig =
96
83
QueryConfiguration <PollVotesFilterField , PollVotesSort >(
97
84
filter = null ,
@@ -109,11 +96,7 @@ internal class PollVoteListStateImplTest {
109
96
fun `on pollVoteUpdated with non-existent vote, then keep unchanged` () = runTest {
110
97
val initialVotes =
111
98
listOf (pollVoteData(), pollVoteData(" vote-2" , " poll-1" , " option-2" , " user-2" ))
112
- val paginationResult =
113
- PaginationResult (
114
- models = initialVotes,
115
- pagination = PaginationData (next = " next-cursor" , previous = null ),
116
- )
99
+ val paginationResult = defaultPaginationResult(initialVotes)
117
100
val queryConfig =
118
101
QueryConfiguration <PollVotesFilterField , PollVotesSort >(
119
102
filter = null ,
@@ -126,4 +109,41 @@ internal class PollVoteListStateImplTest {
126
109
127
110
assertEquals(initialVotes, pollVoteListState.votes.value)
128
111
}
112
+
113
+ @Test
114
+ fun `on pollVoteAdded with new vote, then add vote to list in sorted order` () = runTest {
115
+ val initial =
116
+ listOf (pollVoteData(" vote-1" ), pollVoteData(" vote-3" , " poll-1" , " option-3" , " user-3" ))
117
+ val pagination = defaultPaginationResult(initial)
118
+ val queryConfig =
119
+ QueryConfiguration <PollVotesFilterField , PollVotesSort >(
120
+ filter = null ,
121
+ sort = PollVotesSort .Default ,
122
+ )
123
+ pollVoteListState.onQueryMorePollVotes(pagination, queryConfig)
124
+
125
+ val newVote = pollVoteData(" vote-2" , " poll-1" , " option-2" , " user-2" )
126
+ pollVoteListState.pollVoteAdded(newVote)
127
+
128
+ val expectedVotes = listOf (initial[0 ], newVote, initial[1 ])
129
+ assertEquals(expectedVotes, pollVoteListState.votes.value)
130
+ }
131
+
132
+ @Test
133
+ fun `on pollVoteAdded with existing vote, then update existing vote` () = runTest {
134
+ val initial =
135
+ pollVoteData(" vote-1" , " poll-1" , " option-1" , " user-1" , answerText = " Original" )
136
+ val pagination = defaultPaginationResult(listOf (initial))
137
+ val queryConfig =
138
+ QueryConfiguration <PollVotesFilterField , PollVotesSort >(
139
+ filter = null ,
140
+ sort = PollVotesSort .Default ,
141
+ )
142
+ pollVoteListState.onQueryMorePollVotes(pagination, queryConfig)
143
+
144
+ val updated = pollVoteData(" vote-1" , " poll-1" , " option-1" , " user-1" , answerText = " Updated" )
145
+ pollVoteListState.pollVoteAdded(updated)
146
+
147
+ assertEquals(listOf (updated), pollVoteListState.votes.value)
148
+ }
129
149
}
0 commit comments