Skip to content

Commit 58d8f90

Browse files
committed
Fix latest voter logic incorrect
1 parent 21e57a6 commit 58d8f90

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListItem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ extension ChatChannel {
360360
private func pollMessageText(for previewMessage: ChatMessage) -> String? {
361361
guard let poll = previewMessage.poll, !previewMessage.isDeleted else { return nil }
362362
var components = ["📊"]
363-
if let latestVoter = poll.latestVotesByOption.first?.latestVotes.first?.user {
364-
if previewMessage.isSentByCurrentUser {
363+
if let latestVoter = poll.latestVotes.last?.user {
364+
if latestVoter.id == membership?.id {
365365
components.append(L10n.Channel.Item.pollYouVoted)
366366
} else {
367367
components.append(L10n.Channel.Item.pollSomeoneVoted(latestVoter.name ?? latestVoter.id))

StreamChatSwiftUITests/Tests/ChatChannelList/ChatChannelListItemView_Tests.swift

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,54 @@ final class ChatChannelListItemView_Tests: StreamChatTestCase {
153153
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
154154
}
155155

156+
func test_channelListItem_pollMessage_youVoted() throws {
157+
// Given
158+
let currentUserId = UserId.unique
159+
let message = try mockPollMessage(isSentByCurrentUser: false, latestVotes: [
160+
.unique,
161+
.unique,
162+
.mock(pollId: .unique, optionId: .unique, user: .mock(id: currentUserId))
163+
])
164+
let channel = ChatChannel.mock(cid: .unique, membership: .mock(id: currentUserId), latestMessages: [message])
165+
166+
// When
167+
let view = ChatChannelListItem(
168+
channel: channel,
169+
channelName: "Test",
170+
avatar: .circleImage,
171+
onlineIndicatorShown: true,
172+
onItemTap: { _ in }
173+
)
174+
.frame(width: defaultScreenSize.width)
175+
176+
// Then
177+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
178+
}
179+
180+
func test_channelListItem_pollMessage_someoneVoted() throws {
181+
// Given
182+
let currentUserId = UserId.unique
183+
let message = try mockPollMessage(isSentByCurrentUser: false, latestVotes: [
184+
.unique,
185+
.mock(pollId: .unique, optionId: .unique, user: .mock(id: currentUserId)),
186+
.mock(pollId: .unique, optionId: .unique, user: .mock(id: .unique, name: "Steve Jobs"))
187+
])
188+
let channel = ChatChannel.mock(cid: .unique, membership: .mock(id: currentUserId), latestMessages: [message])
189+
190+
// When
191+
let view = ChatChannelListItem(
192+
channel: channel,
193+
channelName: "Test",
194+
avatar: .circleImage,
195+
onlineIndicatorShown: true,
196+
onItemTap: { _ in }
197+
)
198+
.frame(width: defaultScreenSize.width)
199+
200+
// Then
201+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
202+
}
203+
156204
// MARK: - private
157205

158206
private func mockAudioMessage(text: String, isSentByCurrentUser: Bool) throws -> ChatMessage {
@@ -271,7 +319,7 @@ final class ChatChannelListItemView_Tests: StreamChatTestCase {
271319
)
272320
}
273321

274-
private func mockPollMessage(isSentByCurrentUser: Bool) throws -> ChatMessage {
322+
private func mockPollMessage(isSentByCurrentUser: Bool, latestVotes: [PollVote] = []) throws -> ChatMessage {
275323
.mock(
276324
id: .unique,
277325
cid: .unique,
@@ -281,7 +329,11 @@ final class ChatChannelListItemView_Tests: StreamChatTestCase {
281329
createdAt: Date(timeIntervalSince1970: 100),
282330
localState: nil,
283331
isSentByCurrentUser: isSentByCurrentUser,
284-
poll: .mock(optionCount: 1, voteCountForOption: { _ in 0 })
332+
poll: .mock(
333+
name: "Test poll",
334+
createdBy: .mock(id: "test", name: "test"),
335+
latestVotes: latestVotes
336+
)
285337
)
286338
}
287339
}
Loading
Loading

0 commit comments

Comments
 (0)