Skip to content

Commit 26e2a8e

Browse files
added tests for the typing indicators
1 parent c4123de commit 26e2a8e

File tree

6 files changed

+132
-2
lines changed

6 files changed

+132
-2
lines changed

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelList.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ extension ChatChannel: Identifiable {
9696
"\(cid.id)-\(lastMessageAt ?? createdAt)-\(lastActiveMembersCount)-\(mutedString)-\(unreadCount.messages)-\(typingUsersString)"
9797
}
9898

99-
public var lastActiveMembersCount: Int {
99+
private var lastActiveMembersCount: Int {
100100
lastActiveMembers.filter { member in
101101
member.isOnline
102102
}
103103
.count
104104
}
105105

106-
public var typingUsersString: String {
106+
private var typingUsersString: String {
107107
currentlyTypingUsers.map { user in
108108
user.id
109109
}

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
8465FDDD2747A14700AF091E /* CustomComposerAttachmentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8465FDDC2747A14700AF091E /* CustomComposerAttachmentView.swift */; };
155155
846608E3278C303800D3D7B3 /* TypingIndicatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846608E2278C303800D3D7B3 /* TypingIndicatorView.swift */; };
156156
846608E5278C865200D3D7B3 /* TypingIndicatorPlacement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846608E4278C865200D3D7B3 /* TypingIndicatorPlacement.swift */; };
157+
846608E7278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846608E6278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift */; };
158+
846608E9278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846608E8278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift */; };
157159
848399EA275FB3E9003075E4 /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = 848399E9275FB3E9003075E4 /* SnapshotTesting */; };
158160
848399EC275FB41B003075E4 /* ChatChannelListView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848399EB275FB41B003075E4 /* ChatChannelListView_Tests.swift */; };
159161
848399F227601231003075E4 /* ReactionsOverlayView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848399F127601231003075E4 /* ReactionsOverlayView_Tests.swift */; };
@@ -457,6 +459,8 @@
457459
8465FDDC2747A14700AF091E /* CustomComposerAttachmentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomComposerAttachmentView.swift; sourceTree = "<group>"; };
458460
846608E2278C303800D3D7B3 /* TypingIndicatorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypingIndicatorView.swift; sourceTree = "<group>"; };
459461
846608E4278C865200D3D7B3 /* TypingIndicatorPlacement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypingIndicatorPlacement.swift; sourceTree = "<group>"; };
462+
846608E6278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelExtensions_Tests.swift; sourceTree = "<group>"; };
463+
846608E8278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypingIndicatorView_Tests.swift; sourceTree = "<group>"; };
460464
848399EB275FB41B003075E4 /* ChatChannelListView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelListView_Tests.swift; sourceTree = "<group>"; };
461465
848399F127601231003075E4 /* ReactionsOverlayView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionsOverlayView_Tests.swift; sourceTree = "<group>"; };
462466
849CDD932768E0E1003C7A51 /* MessageActionsResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageActionsResolver.swift; sourceTree = "<group>"; };
@@ -1153,6 +1157,8 @@
11531157
842383DF2767394200888CFC /* ChatChannelDataSource_Tests.swift */,
11541158
842F0BB7276B3518002C400C /* QuotedMessageView_Tests.swift */,
11551159
84AB7B20277203EF00631A10 /* GalleryView_Tests.swift */,
1160+
846608E6278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift */,
1161+
846608E8278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift */,
11561162
84C94D4E2758FE59007FE2B9 /* ChatChannelTestHelpers.swift */,
11571163
);
11581164
path = ChatChannel;
@@ -1531,6 +1537,7 @@
15311537
84C94D68275A6AFD007FE2B9 /* ChannelHeaderLoader_Tests.swift in Sources */,
15321538
84C94D1A27579273007FE2B9 /* TestRequest.swift in Sources */,
15331539
84C94D0927578BF2007FE2B9 /* TestManagedObject.swift in Sources */,
1540+
846608E7278C95E700D3D7B3 /* ChatChannelExtensions_Tests.swift in Sources */,
15341541
84C94D3A27579BB0007FE2B9 /* AssertJSONEqual.swift in Sources */,
15351542
84C94D3927579BB0007FE2B9 /* TestDataModel.xcdatamodeld in Sources */,
15361543
84C94D4D2758FD5C007FE2B9 /* MessageComposerViewModel_Tests.swift in Sources */,
@@ -1570,6 +1577,7 @@
15701577
84C94D2C275796F7007FE2B9 /* RequestRecorderURLProtocol.swift in Sources */,
15711578
84C94CD627578B92007FE2B9 /* AttachmentUploadingState_Mock.swift in Sources */,
15721579
84C94D20275792EA007FE2B9 /* MockBackgroundTaskScheduler.swift in Sources */,
1580+
846608E9278C98CB00D3D7B3 /* TypingIndicatorView_Tests.swift in Sources */,
15731581
84C94CE227578B92007FE2B9 /* UserPayload.swift in Sources */,
15741582
84C94D3D27579BB0007FE2B9 /* TestDataModel2.xcdatamodeld in Sources */,
15751583
84C94CD327578B92007FE2B9 /* ChatMessageFileAttachment_Mock.swift in Sources */,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// Copyright © 2022 Stream.io Inc. All rights reserved.
3+
//
4+
5+
@testable import StreamChat
6+
@testable import StreamChatSwiftUI
7+
import XCTest
8+
9+
class ChatChannelExtensions_Tests: XCTestCase {
10+
11+
private var chatClient: ChatClient = {
12+
let client = ChatClient.mock()
13+
client.currentUserId = .unique
14+
return client
15+
}()
16+
17+
private var streamChat: StreamChat?
18+
19+
override func setUp() {
20+
super.setUp()
21+
streamChat = StreamChat(chatClient: chatClient)
22+
}
23+
24+
func test_typingIndicatorString_unknownValue() {
25+
// Given
26+
let channel = ChatChannel.mockDMChannel()
27+
28+
// When
29+
let typingIndicatorString = channel.typingIndicatorString
30+
31+
// Then
32+
XCTAssert(typingIndicatorString == "Someone is typing")
33+
}
34+
35+
func test_typingIndicatorString_singularValue() {
36+
// Given
37+
let typingUser: ChatChannelMember = ChatChannelMember.mock(
38+
id: .unique, name: "Martin"
39+
)
40+
let channel = ChatChannel.mockDMChannel(
41+
currentlyTypingUsers: Set(arrayLiteral: typingUser)
42+
)
43+
44+
// When
45+
let typingIndicatorString = channel.typingIndicatorString
46+
47+
// Then
48+
XCTAssert(typingIndicatorString == "Martin is typing")
49+
}
50+
51+
func test_typingIndicatorString_pluralValue() {
52+
// Given
53+
let typingUser1: ChatChannelMember = ChatChannelMember.mock(
54+
id: .unique, name: "Stefan"
55+
)
56+
let typingUser2: ChatChannelMember = ChatChannelMember.mock(
57+
id: .unique, name: "Martin"
58+
)
59+
let channel = ChatChannel.mockDMChannel(
60+
currentlyTypingUsers: Set(arrayLiteral: typingUser1, typingUser2)
61+
)
62+
63+
// When
64+
let typingIndicatorString = channel.typingIndicatorString
65+
66+
// Then
67+
XCTAssert(
68+
typingIndicatorString == "Stefan and 1 more is typing"
69+
|| typingIndicatorString == "Martin and 1 more is typing"
70+
) // Any of the names can appear first.
71+
}
72+
}

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelViewModel_Tests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,37 @@ class ChatChannelViewModel_Tests: XCTestCase {
215215
XCTAssert(viewModel.editedMessage == viewModel.messages[0])
216216
}
217217

218+
func test_chatChannelVM_regularMessageHeader() {
219+
// Given
220+
let channelController = makeChannelController()
221+
let viewModel = ChatChannelViewModel(channelController: channelController)
222+
223+
// When
224+
let headerType = viewModel.channelHeaderType
225+
226+
// Then
227+
XCTAssert(headerType == .regular)
228+
}
229+
230+
func test_chatChannelVM_typingIndicatorMessageHeader() {
231+
// Given
232+
let channelController = makeChannelController()
233+
let typingUser: ChatChannelMember = ChatChannelMember.mock(id: .unique)
234+
let viewModel = ChatChannelViewModel(channelController: channelController)
235+
236+
// When
237+
let channel: ChatChannel = .mockDMChannel(currentlyTypingUsers: Set(arrayLiteral: typingUser))
238+
channelController.simulate(
239+
channel: channel,
240+
change: .update(channel),
241+
typingUsers: Set(arrayLiteral: typingUser)
242+
)
243+
let headerType = viewModel.channelHeaderType
244+
245+
// Then
246+
XCTAssert(headerType == .typingIndicator)
247+
}
248+
218249
// MARK: - private
219250

220251
private func makeChannelController(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Copyright © 2022 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import SnapshotTesting
6+
@testable import StreamChatSwiftUI
7+
import XCTest
8+
9+
class TypingIndicatorView_Tests: XCTestCase {
10+
11+
func test_typingIndicatorView_snapshot() {
12+
// Given
13+
let view = TypingIndicatorView()
14+
.frame(width: 20, height: 16)
15+
16+
// Then
17+
assertSnapshot(matching: view, as: .image)
18+
}
19+
}
Loading

0 commit comments

Comments
 (0)