Skip to content

Commit

Permalink
Add Thread List Item test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nuno-vieira committed Oct 14, 2024
1 parent accd20f commit 47a8578
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public struct ChatThreadListItem: View {
var parentMessageText: String
if thread.parentMessage.isDeleted {
parentMessageText = L10n.Message.deletedMessagePlaceholder
} else if let threadTitle = thread.title {
parentMessageText = threadTitle
} else {
let formatter = MessagePreviewFormatter()
parentMessageText = formatter.formatContent(for: thread.parentMessage)
Expand Down
5 changes: 5 additions & 0 deletions StreamChatSwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@
ADE0F5622CB8556F0053B8B9 /* ChatThreadListFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE0F5612CB8556F0053B8B9 /* ChatThreadListFooterView.swift */; };
ADE0F5642CB9609E0053B8B9 /* ChatThreadListHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE0F5632CB9609E0053B8B9 /* ChatThreadListHeaderView.swift */; };
ADE0F5662CB962470053B8B9 /* ActionBannerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE0F5652CB962470053B8B9 /* ActionBannerView.swift */; };
ADE442F22CBDAAC40066CDF7 /* ChatThreadListItemView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE442F12CBDAAC40066CDF7 /* ChatThreadListItemView_Tests.swift */; };
C14A465B284665B100EF498E /* SDKIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = C14A465A284665B100EF498E /* SDKIdentifier.swift */; };
E3A1C01C282BAC66002D1E26 /* Sentry in Frameworks */ = {isa = PBXBuildFile; productRef = E3A1C01B282BAC66002D1E26 /* Sentry */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -1109,6 +1110,7 @@
ADE0F5612CB8556F0053B8B9 /* ChatThreadListFooterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListFooterView.swift; sourceTree = "<group>"; };
ADE0F5632CB9609E0053B8B9 /* ChatThreadListHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListHeaderView.swift; sourceTree = "<group>"; };
ADE0F5652CB962470053B8B9 /* ActionBannerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionBannerView.swift; sourceTree = "<group>"; };
ADE442F12CBDAAC40066CDF7 /* ChatThreadListItemView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListItemView_Tests.swift; sourceTree = "<group>"; };
C14A465A284665B100EF498E /* SDKIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SDKIdentifier.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -2072,6 +2074,7 @@
84E6EC24279AEE9F0017207B /* StreamChatTestCase.swift */,
84C94C7D27567CC2007FE2B9 /* ChatChannelList */,
84C94D472758BDB2007FE2B9 /* ChatChannel */,
ADE442EC2CBDAA320066CDF7 /* ChatThreadList */,
4F6D83522C108D470098C298 /* CommonViews */,
84C94D52275A135F007FE2B9 /* Utils */,
);
Expand Down Expand Up @@ -2260,6 +2263,7 @@
path = ChatThreadList;
sourceTree = "<group>";
};
ADE442F12CBDAAC40066CDF7 /* ChatThreadListItemView_Tests.swift */,
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -2978,6 +2982,7 @@
84CC3732290B0A4000689B73 /* StreamChatModel.xcdatamodeld in Sources */,
84E04790284A444E00BAFA17 /* CDNClient_Mock.swift in Sources */,
84E04796284A444E00BAFA17 /* EventBatcherMock.swift in Sources */,
ADE442F22CBDAAC40066CDF7 /* ChatThreadListItemView_Tests.swift in Sources */,
84E57C5B28103822002213C1 /* TestDataModel.xcdatamodeld in Sources */,
84E04792284A444E00BAFA17 /* MockBackgroundTaskScheduler.swift in Sources */,
84C94D1327578BF2007FE2B9 /* XCTestCase+MockJSON.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// Copyright © 2024 Stream.io Inc. All rights reserved.
//

import SnapshotTesting
@testable import StreamChat
@testable import StreamChatSwiftUI
@testable import StreamChatTestTools
import StreamSwiftTestHelpers
import XCTest

final class ChatThreadListItemView_Tests: StreamChatTestCase {
var mockThread: ChatThread!

var mockYoda = ChatUser.mock(id: .unique, name: "Yoda", imageURL: nil)
var currentUser: ChatUser!

override func setUp() {
super.setUp()
let circleImage = UIImage.circleImage
streamChat?.utils.channelHeaderLoader.placeholder1 = circleImage
streamChat?.utils.channelHeaderLoader.placeholder2 = circleImage
streamChat?.utils.channelHeaderLoader.placeholder3 = circleImage
streamChat?.utils.channelHeaderLoader.placeholder4 = circleImage

currentUser = ChatUser.mock(id: StreamChatTestCase.currentUserId, name: "Vader", imageURL: nil)

mockThread = .mock(
parentMessage: .mock(text: "Parent Message", author: mockYoda),
channel: .mock(cid: .unique, name: "Star Wars Channel"),
createdBy: currentUser,
replyCount: 3,
participantCount: 2,
threadParticipants: [
.mock(user: mockYoda),
.mock(user: currentUser)
],
lastMessageAt: .unique,
createdAt: .unique,
updatedAt: .unique,
title: nil,
latestReplies: [
.mock(text: "First Message", author: mockYoda),
.mock(text: "Second Message", author: currentUser),
.mock(text: "Third Message", author: mockYoda)
],
reads: [],
extraData: [:]
)
}

func test_threadListItem_default() throws {
let view = ChatThreadListItem(thread: mockThread)
.frame(width: defaultScreenSize.width)

assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
}

func test_threadListItem_withUnreads() throws {
let thread = mockThread
.with(reads: [.mock(user: currentUser, lastReadAt: .unique, unreadMessagesCount: 4)])

let view = ChatThreadListItem(thread: thread)
.frame(width: defaultScreenSize.width)

assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
}

func test_threadListItem_withTitle() throws {
let thread = mockThread
.with(title: "Thread title")

let view = ChatThreadListItem(thread: thread)
.frame(width: defaultScreenSize.width)

assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
}

func test_threadListItem_withParentMessageDeleted() throws {
let thread = mockThread
.with(parentMessage: .mock(text: "Parent Message", deletedAt: .unique))

let view = ChatThreadListItem(thread: thread)
.frame(width: defaultScreenSize.width)

assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
}

func test_threadListItem_withLastReplyDeleted() throws {
let thread = mockThread
.with(latestReplies: [
.mock(text: "First Message", author: mockYoda),
.mock(text: "Second Message", author: currentUser),
.mock(text: "Third Message", author: mockYoda, deletedAt: .unique)
])

let view = ChatThreadListItem(thread: thread)
.frame(width: defaultScreenSize.width)

assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
}

func test_threadListItem_whenAttachments() throws {
let thread = mockThread
.with(
parentMessage: .mock(text: "", attachments: [.dummy(type: .giphy)]),
latestReplies: [
.mock(text: "", author: mockYoda, attachments: [.dummy(type: .audio)])
]
)

let view = ChatThreadListItem(thread: thread)
.frame(width: defaultScreenSize.width)

assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
}

func test_threadListItem_whenAttachmentIsPoll() throws {
let thread = mockThread
.with(
parentMessage: .mock(text: "", poll: .mock(name: "Who is better?")),
latestReplies: [
.mock(text: "", author: mockYoda, poll: .mock(name: "Who is worse?"))
]
)

let view = ChatThreadListItem(thread: thread)
.frame(width: defaultScreenSize.width)

assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 47a8578

Please sign in to comment.